如何编写与 base class 一起使用的扩展方法

How to write extension method that work with base class

想法是使用扩展方法来扩展我的功能。

所以不要有这样的东西:

 return Add(Add(storage.GetFirst(), 3), 7);

我想要这样的东西:

return storage.GetFirst().Add(3).Add(7);

扩展方法的问题在于它们必须在静态 class 中是静态的。 这是我想做的简化示例。

 public class Storage
    {
        public int GetFirst()
        {
            return 100;
        }
        public int GetAll(int x, int y)
        {

            // ... 
            return x + y;
        }
    }
    public abstract class MyBase
    {
        protected Storage storage;
        protected MyBase()
        {
            storage = new Storage();
        }

        public int Add(int what, int howMuch)
        {
            return storage.GetAll(what, howMuch);
        }

    }

    public class MyClass : MyBase
    {
        public int method1()
        {
            return Add(Add(storage.GetFirst(), 3), 7);

            //I want have something like this:
            // return storage.GetFirst().Add(3).Add(7);
        }
    }

当然 classes 存储、MyBase 和 MyClass 不能是静态的。逻辑被简化为具有干净简单的示例,因此 classes 之间的关系必须保持不变。 我想要做的是让 Add 方法成为扩展方法,但保留其他所有内容 "more less same".

这可能吗?怎么做?

有一个静态 class 用于扩展并使用类似的东西:

internal static T Map<T> (this int source, Func<int, int, T> function, int extraParam) {
    function (source, extraParam);
}

然后你可以使你的添加方法类似于:

storage.GetFirst ().Map (Add, 3).Map (Add, 7);

另一种解决方案是让您的存储 class 实际存储一些东西:

public class Storage
{
    private int currentValue;

    public Storage GetFirst()
    {
        this.currentValue = 100;
        return this;
    }

    public Storage Add(int toAdd)
    {
        this.currentValue += toAdd;
        return this;
    }

    public int GetResult()
    {
        return this.currentValue;
    }
}

这样你的电话将是:

int result = new Storage().GetFirst().Add(3).Add(5).GetResult();

无需修改任何代码,就可以实现您想要做的事情。但是您当前的设计很难做到。您应该在 Storage class 中有一些存储空间。尽管名称如此,Storage class 中没有存储空间。

public class StorageValue
{
    public StorageValue(Storage storage)
    {
        this.Storage = storage;
    }

    public StorageValue(Storage storage, int value)
    {
        this.Storage = storage;
        this.Value = value;
    }

    public Storage Storage { get; private set; }
    public int Value { get; private set; }

    public StorageValue GetFirst()
    {
        return new StorageValue(Storage, Storage.GetFirst());
    }

    public StorageValue Add(int value)
    {
        return new StorageValue(Storage, Storage.GetAll(Value, value));
    }

    public int GetValue()
    {
        return Value;
    }
}

public static class StorageExtensions
{
    public static StorageValue ToStorageValue(this Storage storage)
    {
        return new StorageValue(storage);
    }
}

这两个 classes,你可以这样调用方法

public class MyClass : MyBase
{
    public int method1()
    {
        return storage
            .ToStorageValue()
            .GetFirst()
            .Add(3)
            .Add(7)
            .GetValue(); 
    }
}

如果您希望AddGetFirst成为扩展方法,您现在可以使用StorageValue class来实现。但是在 StorageValue class 本身更有意义。

也就是说,@Manuel Zelenka 的回答与我的相似,看起来更好。您可以改编其中任何一个。