0

假设:

class BaseClass
{
    public string MyProperty { get; set; }
    public NestedClass_ NestedClass;
    internal class NestedClass_ : BaseClass
    {
        public string MyNestedProperty { get; set; }
    }
}

现在,让我们这样说:

var baseClass = new BaseClass();
baseClass.NestedClass.MyNestedProperty = "Value applied from nested class";

baseClass.MyProperty更改时可以做些什么来应用相同的值baseClass.NestedClass.MyNestedProperty吗?

而相反的情况呢:

var baseClass = new BaseClass();
baseClass.MyProperty = "Value applied from base class";

baseClass.NestedClass.MyNestedProperty更改时可以做些什么来应用相同的值baseClass.MyProperty吗?

4

1 回答 1

0

找到了方法...

class BaseClass
{
    private string _myProperty;

    public string MyProperty
    {
        get { return _myProperty; }
        set
        {
            _nestedClass.MyNestedProperty = value;
            _myProperty = value;
        }
    }

    private NestedClass_ _nestedClass = new NestedClass_();

    private class NestedClass_
    {
        public string MyNestedProperty { get; set; }
    }

    public string MyNestedProperty
    {
        get { return _nestedClass.MyNestedProperty; }
        set
        {
            MyProperty = value;
            _nestedClass.MyNestedProperty = value;
        }
    }
}
于 2012-11-21T17:53:46.283 回答