[英]Is it possible to use a static class to group extension methods?
Adding extension methods is simple via:添加扩展方法很简单:
public static class MyStringExtensions
{
public static string MyMethodOne(this string aSource)
{
return aSource += "My Method One";
}
public static string MyMethodTwo(this string aSource)
{
return aSource += "My Method Two";
}
}
and callable as:并可调用为:
"some string".MyMethodOne();
"some string".MyMethodTwo();
However I have a large number of method and want to "group them" using a static class container:但是,我有大量方法并希望使用静态类容器“将它们分组”:
public static class MyStringExtensionsGroup
{
public static string MethodOne(string aSource)
{
return aSource += "My Method One";
}
public static string MethodTwo(string aSource)
{
return aSource += "My Method Two";
}
}
So assuming I somehow expose My : MyStringExtensionsGroup
as a string extension I could end up calling things with the following convention (note period after My
):因此,假设我以某种方式将
My : MyStringExtensionsGroup
作为字符串扩展名公开, My : MyStringExtensionsGroup
最终可以使用以下约定调用事物(注意My
之后的句点):
"some string".My.MethodOne();
"some string".My.MethodTwo();
and say I had another grouping called '''MyOtherGroup''' I could then do:并说我有另一个名为 '''MyOtherGroup''' 的分组,然后我可以这样做:
"some string".MyOtherGroup.MethodOne();
"some string".MyOtherGroup.MethodTwo();
Is any "grouping" of extension methods possible with what C# currently provides? C#当前提供的扩展方法是否可以“分组”?
Extensions methods can't be nested , however with a lot of mindless fiddling you could create your own fluent syntax .扩展方法不能嵌套,但是通过大量无意识的摆弄,您可以创建自己的流畅语法。
Extensions扩展
public static class MyStringExtensionsGroup
{
public static string MethodOne(this Group1 group) => group.Source + "My Method One";
public static string MethodTwo(this Group2 group) => group.Source + "My Method Two";
public static Group1 Group1(this string source) => new Group1(source);
public static Group2 Group2(this string source) => new Group2(source);
}
Groups团体
public class Group1
{
public string Source { get; }
public Group1(string source) => Source = source;
}
public class Group2
{
public string Source { get; }
public Group2(string source) => Source = source;
}
Or you could just stuff your methods in the class, which is neater或者你可以把你的方法塞进班级里,这样更整洁
public class Group1
{
public string Source { get; }
public Group1(string source) => Source = source;
public string MethodOne() => Source + "My Method One";
public string MethodTwo() => Source + "My Method Two";
}
public class Group2
{
public string Source { get; }
public Group2(string source) => Source = source;
public string MethodOne() => Source + "My Method One";
public string MethodTwo() => Source + "My Method Two";
}
Either way the usage is the same无论哪种方式,用法都是一样的
Usage用法
var someString = "asd";
Console.WriteLine(someString.Group1().MethodOne());
Console.WriteLine(someString.Group2().MethodTwo());
Note : There are other ways and structures that can do this, however this will get you started if you want to go down this rabbit hole注意:还有其他方法和结构可以做到这一点,但是如果你想深入这个兔子洞,这会让你开始
In summary, I wouldn't do this at all :)总之,我根本不会这样做:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.