c# – 对不同类的不同访问属性
我有一个基(抽象)类Component.我想控制对派生类的属性的访问,以便每个人都获得读访问权限,但只有某些类允许写访问权限.
那些’某些类’当前是实现抽象基类MessageHandler< TMessage>的任何东西.理想情况下,我也喜欢实现IMessageHandler的类能够访问,但我认为这使得要求更加困难. 这将在xbox上运行,因此我想避免创建临时对象(例如只读副本).我还想最小化方法调用的数量,以获得读/写的值. Component类和MessageHandler< TMessage>类当前在它们自己的程序集中,当使用我的API时,这两个程序集都将被其他项目引用. 我猜我不得不以某种方式改变我的模型,但我无法理解它. public abstract class Component { } public class DerivedComponentA : Component { int property {get; set;} } public abstract class MessageHandler<TMessage> { } public class IntMsghandler : MessageHandler<int> { void DoThing(DerivedComponentA derivedComponentA) { derivedComponentA.property = 5; // Allowed } } public class AnyClass // Doesn't inherit MessageHandler,or implement IMessageHandler { void DoThing(DerivedComponentA derivedComponentA) { derivedComponentA.property = 5; // Not Allowed } } 解决方法
基于基类定义(如果有的话)进行隔离(基于您提出的问题和我理解的内容).这意味着隔离应该从它开始.
或者,如果你说,如果某个类X实现MessageHandler应该能够在类Y类型对象上以多种方式行动.那,imo,意味着两者之间存在着艰难的关系 这导致我认为你可以这样做: >只有public才能获得DerivedComponentA的属性 这样,只有在任何Component派生类上设置属性的可能方法是使用MessageHandler方法,因此只能从那些派生它的人那里获得.对于其他可用类型,您只提供用于读取数据的公共get(只读)属性. 希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |