【原】C#两个类互相包含类对象可以吗?

在阅读大牛代码的时候发现了这个问题,之所以标题以问号开头,是因为我还没搞懂这个问题。权当记录,以希望以后搞懂。

部分图片转贴吧:http://tieba.baidu.com/p/6015910029

比如这样是不行的,图来自贴吧:

【原】C#两个类互相包含类对象可以吗?

 

 

 诉求是在不添加属性、方法和改变访问权限的基础上互相访问私有字段。

 路人甲:

1 C# 友元方法有啊。我记得是修改[assembly: InternalsVisibleTo("类名")],这个在assemblyInfo.cs中,之前我使用过。
2 通过反射可以直接拿到。
3 你确定你的私有字段,不需要封装成字段吗?

路人乙:

十之八九是设计错误,十之一二也是可以实现的:反射、嵌套类、internal等。

题主:

我只希望
类A的私有字段m_b可以在类B中访问,
类B的私有字段m_a可以在类A中访问,
除了在这两个类中能访问,外部的任何地方都不能访问。

添加属性可以实现,如下(我也不知道合不合题意):

   class A
    {
        private B mb;
        public void Func(B b)
        {
            mb = b;
            mb.MA = (A)this;
        }
        public B MB
        {
            get
            {
                return mb;
            }
            set
            {
                mb = (B)value;
            }
        }
    }

    class B
    {
        private A m_a;
        public void Func(A a)
        {
            m_a = a;
            a.MB = (B)this;
        }

        public A MA
        {
            get
            {
                return m_a;
            }
            set
            {
                m_a = (A)value;
            }
        }
    }

    class Program
    {
         static void Main(string[] args)
        {
            A a = new A();
            B b = new B();
            a.MB = b;
            b.MA = a;

            Console.WriteLine(a.GetType());
            Console.WriteLine(b.GetType());
            Console.WriteLine(a.MB.GetType());
            Console.WriteLine(b.MA.GetType());

            Console.ReadKey();
        }
    }

题主的解决方案:

    sealed class B:A
    {

    }

    sealed class C:A
    {

    }
   
    class Program
    {
         static void Main(string[] args)
        {
            C c = new C();
            B b = new B();

            b.SetMutualInclude(c);

            Console.WriteLine(b.GetType());
            Console.WriteLine(b.MC.GetType());
            Console.WriteLine(c.MB.GetType());

             //这种情况没有处理,现在为null
            Console.WriteLine(b.MB.GetType());
           // Console.ReadKey();
        }
    }

下面是解决上面的问题的解决办法:

   abstract class A
        {
            protected B mb;
            protected C mc;

            public void SetMutualInclude(C c)
            {
                mc = c;
                c.mb = (B)this;
            }
            public void SetMutualInclude(B b)
            {
                mb = b;
                b.mc = (C)this;
            }
        }


        sealed class B : A
        {
            public C MC
            {
                get
                {
                    return mc;
                }
            }
        }

        sealed class C : A
        {
            public B MB
            {
                get
                {
                    return mb;
                }
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                C c = new C();
                B b = new B();

                b.SetMutualInclude(c);

                Console.WriteLine(b.GetType());
                Console.WriteLine(b.MC.GetType());
                Console.WriteLine(c.MB.GetType());
                // Console.ReadKey();
            }
        }

大神的代码好难,什么时候才能全部弄懂啊,摔!

 

【原】C#两个类互相包含类对象可以吗?

上一篇:windows下重启nginx


下一篇:CSS行高——line-height