我想创建功能, 函数名是extend(),它应该在字符串之后,bool,control.like默认函数ToString()
"123".extend();
false.extend();
textbox1.extend();
extend();
extend()函数可以检查输入类型
if input is string ? ToUpperCase , Substring and Replace the string
if input is bool ? checking the bool
if input is Control ? check control type , change text,color
if input is List ? to update global list
获取属性名称以执行某些操作
string SaveString ="";
SaveString.extend();
if(propertyname(object) =="SaveString"){
}
如何创建这样的功能?谢谢
答案 0 :(得分:5)
namespace System
{
public static class ObjectExtension
{
public static string Extend(this object input)
{
// Do something to input object.
// For example, you can have different implementation based on its type.
if (input is string)
{
}
else if (input is bool)
{
}
}
}
}
在对象上使用扩展方法时会有没有性能损失,因为它是编译器功能,请参阅C# Extension Method for Object