我刚刚注意到,在字符串上,它们有很多扩展方法,我想我从来没有在字符串上注意到。
有些就像
IsEmpty() // Seems to be equivalent to String.IsNullOrEmpty()
AsInt() // seems to be equivalent to Convert.ToInt32(string); - does it throw exception as well?我只是想知道,他们是否在钩子下使用相同的代码,而这些代码只是为了减少输入,还是正在进行更多的操作?
有些人似乎已经失踪了,尽管
String.IsNullOrWhiteSpace()编辑
对不起,当我说缺少String.IsNullOrWhiteSpace()时,我发现没有扩展方法。我有这样的方法,就是我写我上面写的东西。
这似乎也是不标准的框架,所以我试图找出他们从哪里来?
我不确定是重发者加了这些,还是我有其他参考资料。我想我从未导入过任何扩展插件。
当我单击IsEmpty()上的definition
我明白了
#region Assembly System.Web.WebPages.dll, v4.0.30319
// c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll
#endregion
using System;
using System.Runtime.CompilerServices;
namespace System.Web.WebPages
{
// Summary:
// Provides utility methods for converting string values to other data types.
public static class StringExtensions
{
// Summary:
// Converts a string to a strongly typed value of the specified data type.
//
// Parameters:
// value:
// The value to convert.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value);
//
// Summary:
// Converts a string to the specified data type and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value, TValue defaultValue);
//
// Summary:
// Converts a string to a Boolean (true/false) value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static bool AsBool(this string value);
//
// Summary:
// Converts a string to a Boolean (true/false) value and specifies a default
// value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// false.
//
// Returns:
// The converted value.
public static bool AsBool(this string value, bool defaultValue);
//
// Summary:
// Converts a string to a System.DateTime value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value);
//
// Summary:
// Converts a string to a System.DateTime value and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// the minimum time value on the system.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value, DateTime defaultValue);
//
// Summary:
// Converts a string to a System.Decimal number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value);
//
// Summary:
// Converts a string to a System.Decimal number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or invalid.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value, decimal defaultValue);
//
// Summary:
// Converts a string to a System.Single number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static float AsFloat(this string value);
//
// Summary:
// Converts a string to a System.Single number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Returns:
// The converted value.
public static float AsFloat(this string value, float defaultValue);
//
// Summary:
// Converts a string to an integer.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static int AsInt(this string value);
//
// Summary:
// Converts a string to an integer and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or is an invalid value.
//
// Returns:
// The converted value.
public static int AsInt(this string value, int defaultValue);
//
// Summary:
// Checks whether a string can be converted to the specified data type.
//
// Parameters:
// value:
// The value to test.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool Is<TValue>(this string value);
//
// Summary:
// Checks whether a string can be converted to the Boolean (true/false) type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsBool(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.DateTime type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDateTime(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Decimal type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDecimal(this string value);
//
// Summary:
// Checks whether a string value is null or empty.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value is null or is a zero-length string (""); otherwise, false.
public static bool IsEmpty(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Single type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsFloat(this string value);
//
// Summary:
// Checks whether a string can be converted to an integer.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsInt(this string value);
}
}发布于 2011-02-14 17:43:22
这些不是“标准”扩展方法--它们很可能是由其他人在您的项目中添加的。这意味着我们无法知道代码在幕后做什么,但是您应该能够自己找出答案。
在Visual中,您应该能够导航到任何一种方法的定义--它将显示该方法在哪个程序集中,或者如果可能的话直接转到源代码。
编辑:给出注释,看起来它们是来自MVC's StringExtensions class的扩展方法。据我所知,这违反了命名方面的各种不良做法--尤其是在方法名称中使用特定于语言的名称,而不是CLR类型名称。(例如,AsFloat应该是AsSingle。)我还认为它应该是"To“而不是"As”,因为它提供了一个完整的转换,而不仅仅是返回原始值的视图。一切都是骗人的。
发布于 2011-02-14 17:45:00
有些人似乎已经失踪了,尽管
String.IsNullOrWhiteSpace()
那是在Fx4中介绍的,你运行3.5吗?
发布于 2011-02-14 17:49:47
很长一段时间以来,我对每个字符串都使用这种扩展方法.它很容易使用,quick....make扩展方法是您的日常习惯,您可以而且应该这样做,您将看到编写代码的差异很大。
创建一个库并在应用程序中使用它.
public static bool IsNullOrEmpty(this string testString)
{
return string.IsNullOrEmpty(testString);
}https://stackoverflow.com/questions/4995350
复制相似问题