如何访问嵌套类的私有成员? [英] how to get access to private members of nested class?

查看:274
本文介绍了如何访问嵌套类的私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我已经封闭(父)类E与嵌套的类N与几个N的实例在E中。在封闭(父)类中,我正在做一些计算,我正在设置每个嵌套类的实例的值。这样的东西:

  n1.field1 = ...; 
n1.field2 = ...;
n1.field3 = ...;
n2.field1 = ...;
...

它是一个大的eval方法(在父类中)。我的意图是 - 由于所有的计算都是在父类中(它们不能每个嵌套实例都做,因为它会使代码更复杂) - 使setter仅适用于父类和getter public。



现在有一个问题:




  • 父母级别不能访问他们

  • ,当我把它们公开时,每个人都可以更改值

  • ,而C#没有朋友的概念

  • 我不能通过构造函数传递值,因为使用了延迟的评估机制(因此,在引用它们时必须创建实例 - 创建所有对象,并根据需要触发计算)



我被困 - 如何做到这一点(限制访问到父类,不再更多,不少)?






我怀疑我会先回答问题 - 但是为什么你不分割每个字段的评估 - 所以我通过示例回答:您如何计算集合的最小值和最大值?以快速的方式答案是 - 一次通过。这就是为什么我有一个eval函数,它可以一次计算和设置所有的字段。

解决方案

为了将父类和子类放在另一个程序集中,您可以使用内部作为设置器。这通常是在野外处理的。



编辑



托马斯Levesque的答案给了我一个想法:

  class Program 
{
static void Main string [] args)
{
E myE = new E();

Console.WriteLine(E.N1.Field1 =+ myE.N1.Field1);
Console.WriteLine(E.N2.Field1 =+ myE.N2.Field1);
}

public interface IN
{
int Field1 {get; }
}

public class E
{
private N _n1 = new N();
private N _n2 = new N();

public E()
{
_n1.Field1 = 42;
_n2.Field1 = 23;
}

public IN N1
{
get {return _n1; }
}

public IN N2
{
get {return _n2; }
}

私有类N:IN
{
private int _field1;

public int Field1
{
get {return _field1; }
set {_field1 = value; }
}
}
}
}

根据您需要如何公开小孩类 N ,这可能会起作用。


Background: I have enclosed (parent) class E with nested class N with several instances of N in E. In the enclosed (parent) class I am doing some calculations and I am setting the values for each instance of nested class. Something like this:

n1.field1 = ...;
n1.field2 = ...;
n1.field3 = ...;
n2.field1 = ...;
...

It is one big eval method (in parent class). My intention is -- since all calculations are in parent class (they cannot be done per nested instance because it would make code more complicated) -- make the setters only available to parent class and getters public.

And now there is a problem:

  • when I make the setters private, parent class cannot acces them
  • when I make them public, everybody can change the values
  • and C# does not have friend concept
  • I cannot pass values in constructor because lazy evaluation mechanism is used (so the instances have to be created when referencing them -- I create all objects and the calculation is triggered on demand)

I am stuck -- how to do this (limit access up to parent class, no more, no less)?


I suspect I'll get answer-question first -- "but why you don't split the evaluation per each field" -- so I answer this by example: how do you calculate min and max value of a collection? In a fast way? The answer is -- in one pass. This is why I have one eval function which does calculations and sets all fields at once.

解决方案

If it's possible for you to put the parent and child classes in another assembly, you can make use of internal for the setters. That's generally how this is dealt with in the wild.

EDIT:

Thomas Levesque's answer gave me an idea:

class Program
{
    static void Main(string[] args)
    {
        E myE = new E();

        Console.WriteLine("E.N1.Field1 = " + myE.N1.Field1);
        Console.WriteLine("E.N2.Field1 = " + myE.N2.Field1);
    }

    public interface IN
    {
        int Field1 { get; }
    }

    public class E
    {
        private N _n1 = new N();
        private N _n2 = new N();

        public E()
        {
            _n1.Field1 = 42;
            _n2.Field1 = 23;
        }

        public IN N1
        {
            get { return _n1; }
        }

        public IN N2
        {
            get { return _n2; }
        }

        private class N : IN
        {
            private int _field1;

            public int Field1
            {
                get { return _field1; }
                set { _field1 = value; }
            }
        }
    }
}

Depending on how you need to expose the child class N, this could work.

这篇关于如何访问嵌套类的私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆