Rup*_*pal 3 .net c# inheritance extension-methods new-operator
我们可以在扩展方法和继承之间建立一些关系吗?
或者是一种类似于new在C#中使用-keyword 的扩展方法?
两个问题都没有.扩展方法实际上是一种方法,它将扩展操作的对象作为第一个参数.
该new关键字用于为类的实例分配资源.扩展方法对实例进行操作,但不能作为新替换,因为它需要一个实例(或null该类型)作为第一个参数.
考虑:
public static class StringExtensions
{
public static string Reverse(this string _this)
{
// logic to reverse the string
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过两种方式调用此静态方法:
// as an extension method:
string s = "hello world";
string t = s.Reverse();
// as a static method invocation:
string t = StringExtensions.Reverse(s);
Run Code Online (Sandbox Code Playgroud)
在任何一种情况下,编译器都会更改MSIL中的调用以映射第二个调用.编译之后,如果没有this-keyword,你就无法识别它的静态对应的扩展方法.
总结一下
new关键字那样实例化类(但在方法中你当然可以实例化类).null,否则该类的方法将引发a NullReferenceException.这是因为实例只是静态方法中的第一个参数.string如上所述),这是通过继承无法实现的(不能从密封类派生).编辑:
Tigran和Floran暗示这个问题是关于新的修饰符,我补充说它甚至可能是关于新的通用约束.
扩展方法具有不与所述的任一含义关系new关键字.然而,这里有一些关于彼此的想法.我希望它不会让事情更加混乱.如果是这样,请坚持"编辑"上方的部分;)
在新修饰符上:
关于新的通用约束:
扩展方法本身可以是通用的,并且参数(即它所操作的允许类)可以受到新通用约束的限制:
// this ext. method will only operate on classes that inherit
// from ICollectible and have a public parameterless constructor
public static int CalculateTotal<T>(this T _collectible)
where T : ICollectible, new()
{
// calculate total
}
Run Code Online (Sandbox Code Playgroud)