C# 可以使用静态类对扩展方法进行分组吗?

C# 可以使用静态类对扩展方法进行分组吗?,c#,C#,通过以下方式添加扩展方法非常简单: public static class MyStringExtensions { public static string MyMethodOne(this string aSource) { return aSource += "My Method One"; } public static string MyMethodTwo(this string aSource) { retur

通过以下方式添加扩展方法非常简单:

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";
    }
}
可调用为:

"some string".MyMethodOne();
"some string".MyMethodTwo();
但是,我有很多方法,希望使用静态类容器对它们进行分组:

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";
    }
}
因此,假设我以某种方式将My:MyStringExtensionsGroup公开为字符串扩展名,我可能会在我的

假设我有另一个叫做“MyOtherGroup”的小组,我可以这样做:

"some string".MyOtherGroup.MethodOne();
"some string".MyOtherGroup.MethodTwo();
C当前提供的扩展方法可以进行任何分组吗?

扩展方法不能嵌套,但是通过大量无意识的修改,您可以创建自己流畅的语法

扩展

团体

或者你可以把你的方法放在课堂上,这样更整洁

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";
}
无论哪种方式,用法都是一样的

用法

注意:还有其他的方法和结构可以做到这一点,但是如果你想进入这个兔子洞,这将让你开始
总之,我不会这么做:

如果不创建额外的对象中间实例,以一种简单的方式是不可能的。我假设您有意不将此用于MethodOne?@mjwills MethodOne不是扩展方法-很酷-只是澄清一下你明白这一点,因为你期待一些string.MyOtherGroup.MethodOne似乎有些奇怪;在这种情况下,我永远不会工作。谢谢你去写它的努力!你是一个更好的[人类]“比我更糟。@John完全否认我试图逃避我自己的工作:这是邪恶的。@RobertHarvey是的,如果我的代码审查失败,就会有一张便条,上面写着“不要再这样做了:P@TheEdge您可以创建一个实例类,甚至是一个纯静态类,因为它不需要构造函数,然后您可以对所有实用程序方法进行分组然而,在一起你就失去了流畅的延伸感。或者,如果这种业务逻辑,有时最好是驻留在可注入的服务中,那么您要做什么取决于您需要什么。扩展方法不错,但我不赞成为所有东西创建一个扩展方法。如果你想讨论你的情况的利弊,你可以在SoftwareEngineering网站上发帖,这些孩子不介意谈论好的设计模式
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);
}
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;
}
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";
}
var someString = "asd";
Console.WriteLine(someString.Group1().MethodOne());
Console.WriteLine(someString.Group2().MethodTwo());