如何限制访问嵌套类成员封闭类? [英] How to restrict access to nested class member to enclosing class?

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

问题描述

是否可以指定一个嵌套类的成员可以由封闭类访问,而不是其他类?

Is it possible to specify that members of a nested class can be accessed by the enclosing class, but not other classes ?

这里的问题的说明(当然我的实际code是一个比较复杂......):

Here's an illustration of the problem (of course my actual code is a bit more complex...) :

public class Journal
{
    public class JournalEntry
    {
        public JournalEntry(object value)
        {
            this.Timestamp = DateTime.Now;
            this.Value = value;
        }

        public DateTime Timestamp { get; private set; }
        public object Value { get; private set; }
    }

    // ...
}

我想从创建 JournalEntry的的实例prevent客户code,但杂志必须能够创建它们。如果我让构造公开,任何人都可以创建实例...但如果​​我让它私有,杂志将不能够!

I would like to prevent client code from creating instances of JournalEntry, but Journal must be able to create them. If I make the constructor public, anyone can create instances... but if I make it private, Journal won't be able to !

请注意, JournalEntry的类必须是公开的,因为我希望能够将已有的条目,以客户code。

Note that the JournalEntry class must be public, because I want to be able to expose existing entries to client code.

任何建议将AP preciated!

Any suggestion would be appreciated !


更新:谢谢大家对您的输入,我最终去为公众 IJournalEntry 接口,通过实施私有 JournalEntry的类(尽管我的问题的最后一个要求......)

UPDATE: Thanks everyone for your input, I eventually went for the public IJournalEntry interface, implemented by a private JournalEntry class (despite the last requirement in my question...)

推荐答案

如果你的类是不是太复杂,你既可以使用一个接口是公开可见的,使实际实现类的私人或者你可以做的一个受保护的构造 JornalEntry 类,并有一个私人类 JornalEntryInstance JornalEntry 导出与实际上是由你的杂志

If your class is not too complex you could either use an interface which is publically visible and make the actual implementing class private or you could make a protected constructor for the JornalEntry class and have a private class JornalEntryInstance derived from JornalEntry with a public constructor which is actually instantiated by your Journal.

public class Journal
{
    public class JournalEntry
    {
        protected JournalEntry(object value)
        {
            this.Timestamp = DateTime.Now;
            this.Value = value;
        }

        public DateTime Timestamp { get; private set; }
        public object Value { get; private set; }
    }

    private class JournalEntryInstance: JournalEntry
    { 
        public JournalEntryInstance(object value): base(value)
        { }
    }
    JournalEntry CreateEntry(object value)
    {
        return new JournalEntryInstance(value);
    }
}

如果您的实际类是太复杂,要么做那,你可以用构造暂时不完全隐形脱身,可以使内部构造,因此仅在装配可见。

If your actual class is too complex to do either of that and you can get away with the constructor being not completely invisible, you can make the constructor internal so it is only visible in the assembly.

如果那也是不可行的,你总是可以使构造私人和使用反射来从你的日记类调用它:

If that too is infeasible you can always make the constructor private and use reflection to call it from your journal class:

typeof(object).GetConstructor(new Type[] { }).Invoke(new Object[] { value });

现在,我想想,另一种可能性是在从内类设置包含类使用一个专用的委托

Now that I think about it, another possibility would use a private delegate in the containing class which is set from the inner class

public class Journal
{
    private static Func<object, JournalEntry> EntryFactory;
    public class JournalEntry
    {
        internal static void Initialize()
        {
            Journal.EntryFactory = CreateEntry;
        }
        private static JournalEntry CreateEntry(object value)
        {
            return new JournalEntry(value);
        }
        private JournalEntry(object value)
        {
            this.Timestamp = DateTime.Now;
            this.Value = value;
        }

        public DateTime Timestamp { get; private set; }
        public object Value { get; private set; }
    }

    static Journal()
    {
        JournalEntry.Initialize();
    }

    static JournalEntry CreateEntry(object value)
    {
        return EntryFactory(value);
    }
}

这应该给你,而无需诉诸慢反射或引入其他类所需的可见度水平/接口

This should give you your desired visibility levels without needing to resort on slow reflection or introducing additional classes / interfaces

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

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