扩展类中的成员方法 可扩展dll中的方法 例子如下:
public static class Extend
{
//扩展方法
public static float Multiply(this ExtendTest test)
{
// return test.floatValue * test.intValue; (error)
return test.floatValue * 1;
}
}
public class ExtendTest
{
private int intValue;
public float floatValue;
public float Add()
{
return intValue + floatValue;
}
public void Test()
{
// Multiply(); (error)
this.Multiply();
}
}
扩展方法使用 首先扩展方法必须是在静态类 并且声明时必须使用静态以及public声明
扩展方法第一个参数使用this 修饰 指定你要扩展的类
但是扩展方法内只能访问类中public成员
扩展方法必须使用对象来调用,在内部使用的时候必须要this.
并且扩展方法受命名空间影响