2

想象一个如下的类..这是提供给我的一个类..我无法更改它的来源..

public class MyClass
{
    object _Object { get; set; }

    public void FuncA1() { _Object = new object(); }
    public void FuncA2() { _Object = new List<object>(); }

    public int FuncB1() { _Object = 0; return 0; }
    public int FuncB2() { _Object = 123; return 123; }

    public string FuncC1() { _Object = null; return null; }
    public string FuncC2() { _Object = "Hello"; return "Hello"; }
}

我试图为这个类创建一个包装器,这样我就可以将它的许多功能分组到类别中。

MyWrapper.Voids.FuncA1();
MyWrapper.Voids.FuncA2();
MyWrapper.Integers.FuncB1();
MyWrapper.Integers.FuncB2();
MyWrapper.Strings.FuncC1();
MyWrapper.Strings.FuncC2();

对于这种情况,我能想到的唯一解决方案是像这样设计包装器:

public class MyWrapper
{
    MyClass _Instance { get; set; }
    public _Void Voids { get; private set; }
    public _Integer Integers { get; private set; }
    public _String Strings { get; private set; }

    public class _Void
    {
        MyWrapper _Parent { get; set; }

        public void FuncA1() { _Parent._Instance.FuncA1(); }
        public int FuncA2() { return _Parent._Instance.FuncA2(); }
    }
    public class _Integer
    {
        ...
    }
    public class _String
    {
        ...
    }

    public MyWrapper()
    {
        _Instance = new MyClass();
        Voids = new _Voids(this);
        Integers = new _Integer(this);
        Strings = new _String(this);
    }
}

该解决方案有效,但存在许多问题: - 内部类被迫公开,这允许用户实例化它们.. - 我被迫在子类中维护父对象的引用..

有没有更好的方法来做到这一点?

编辑:最初发布的代码有点令人困惑,因为它把注意力从核心问题上转移到了一个函数是否会导致异常的问题上,如果它们都在同一个对象上工作..

注意:这不是实际代码..我将这个示例拼凑在一起以展示我正在尝试做的事情..在对象周围创建一个包装器(我无法更改原始对象的代码)并将函数分组为类别..

最终编辑:遵循 Juharr 的建议..这是我为完成我想要的事情所做的事情..为了改善他人..

public interface IVoid
{
    void FuncA1();
    void FuncA2();
}
public interface IInteger
{
    int FuncB1();
    int FuncB2();
}
public class MyWrapper
{
    public MyClass Instance { get; private set; }
    public IVoid Voids { get; private set; }
    public IInteger Integers { get; private set; }

    private abstract class MyBase
    {
        protected MyWrapper Parent { get; set; }
        protected MyClass Instance { get { return Parent.Instance; } }
        public MyBase(MyWrapper oParent) { Parent = oParent; }
    }
    private class MyVoid : MyBase, IVoid
    {
        public MyVoids (MyWrapper oParent) : base(oParent) { }
        public void FuncA1() { Instance.FuncA1(); }
        public void FuncA2() { Instance.FuncA2(); }
    }
    private class MyInteger : MyBase, IInteger
    {
        public MyInteger (MyWrapper oParent) : base(oParent) { }
        public int FuncB1() { return Instance.FuncB1(); }
        public int FuncB2() { return Instance.FuncB2(); }
    }

    public MyWrapper()
    {
        Instance = new MyClass();
        Voids = new MyVoid(this);
        Integers = new MyInteger(this);
    }
}
4

1 回答 1

3

您可以改为编写公共接口。那么你的内部类不必公开。所以像这样。

public interface IIntger
{
    void Set(int iValue);
    int Get();
}

public class MyWrapper
{
    MyClass _Instance { get; set; }
    public IInteger Integer { get; private set; }

    private class _Integer : IInteger
    {
        MyWrapper _Parent { get; set; }

        public void Set(int iValue) { _Parent._Instance.IntegerSet(iValue); }
        public int Get() { return _Parent._Instance.IntegerGet(); }
    }

    public MyWrapper()
    {
        _Instance = new MyClass();
        Integer = new _Integer(this);
    }
}

编辑:

要回答问题的第二部分,您将需要对父类的引用或对要包装的类的引用。所以你可以有这个。

public class MyWrapper
{
    public IInteger Integer { get; private set; }

    private class _Integer : IInteger
    {
        MyClass _Instance { get; set; }
        public _Integer(MyClass myClass) { _Instance = myClass; }
        public void Set(int iValue) { _Instance.IntegerSet(iValue); }
        public int Get() { return _Instance.IntegerGet(); }
    }

    public MyWrapper(MyClass instance)
    {
        Integer = new _Integer(instance);
    }
}
于 2013-01-05T20:07:01.337 回答