限制到公开的setter到特定对象的访问(C#) [英] Limiting access to a public setter to specific objects (C#)

查看:138
本文介绍了限制到公开的setter到特定对象的访问(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个充当我的应用程序的环境中的类(在C#)。

I'm trying to create a class (in C#) that serves as an environment for my application.

我试图使课堂充满活力,和把它作为我的应用程序的参数实体。问题是,我希望能够改变这个环境类的属性(公众制定者),但在同一时间,我想接收环境中的类无法使用这些setter方法​​。

I'm trying to make the class dynamic, and send it as a parameter to entities in my application. The problem is, that I want to be able to change the properties of this environment class (public setters), but at the same time I want the classes that receive the environment to be unable to use these setters.

我似乎无法找到短语我的问题(我的数字是我找不到像这样在谷歌或MSDN任何原因的一部分)的好方法,但放不久,我要创建与公众都只是我的一些对象,而不是对所有

I can't seem to find a good way to phrase my question (which I figure is a part of the reason I can't find anything like this on Google or msdn), but to put shortly, I want to create a class with setters that are public only for some of my objects and not for all.

我目前setter方法​​的类有趣以下的想法:
。避免公众制定者都在一起,并使用事件注册暴露私有字段

I'm currently amusing the following idea: Avoiding the public setters all together, and expose the private fields using event registration.

本课程将在新的第三注册事件。对象(作为参数发送给构造函数)。将由环境进行登记的方法并不多,然后制定者,所以引发这些事件将允许访问私人领域。

The class will register to events in a new third object (sent as a parameter to the constructor). The methods that will be registered by the environment are not much more then setters, and so triggering these events will "allow access" to the private fields.

我喜欢一些想法(看到,因为我觉得我的是不是所有的伟大),或者更好一些图案,我可以利用。

I'd love some ideas (seeing as I feel that mine isn't all that great), or better yet some patterns I could make use of.

在此先感谢

推荐答案

您可以创建一个代理,并发送代理实体类。

You could create a proxy, and send that proxy to your entity classes.

class MyClass
{
    public int MyProperty { get; set; }
}

class MyProxyClass
{
    public MyProxyClass(MyClass myClass)
    {
        _myClass = myClass;
    }

    private MyClass _myClass;

    public int MyProperty
    {
        get { return _myClass.MyProperty; }
    }
}

这篇关于限制到公开的setter到特定对象的访问(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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