我有一个基础(抽象)类Component。我想控制对派生类的属性的访问,这样每个人都有读访问权限,但写访问只允许某些类访问。
这些“特定类”是目前实现抽象基类MessageHandler<TMessage>的任何东西。理想情况下,我也希望实现IMessageHandler的类能够获得访问权限,但我认为这会使要求变得有点困难。
这将在xbox上运行,因此我希望避免创建临时对象(例如只读副本)。我还希望最小化方法调用的数量,以获得读/写的值。
Component类和MessageHandler<TMessage>类目前在它们自己的程序集中,在使用我的应用程序接口时,这两个程序集都将被其他项目引用。
我猜我将不得不以某种方式改变我的模型,但我无法理解它。
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
}
}发布于 2012-01-24 19:59:24
你能不能像这样控制setter:
public string Prop {
get { return ...; }
set { if (!(this is MessageHandler<TMessage>))
throw Exception(...);
.... setter code;
}
}https://stackoverflow.com/questions/8986370
复制相似问题