不同阶层获得不同财产的方式

发布于 2024-12-28 21:35:56 字数 1043 浏览 2 评论 0原文

我有一个基(抽象)类Component。我想控制对派生类属性的访问,以便每个人都可以获得读取访问权限,但只有某些类允许写入访问权限。

这些“某些类”当前是实现抽象基类 MessageHandler 的任何类。理想情况下,我还希望一个实现 IMessageHandler 的类能够获得访问权限,但我认为这使得要求变得更加严格。

这将在 xbox 上运行,因此我想避免创建临时对象(例如只读副本)。我还想最大限度地减少获取读取/写入值的方法调用次数。

Component 类和 MessageHandler 类当前位于它们自己的程序集中,在使用我的 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
    }
}

I have a base (abstract) class Component. I want to control access to properties of derived classes such that everyone gets read access, but write access is only allowed by certain classes.

Those 'certain classes' are currently anything that implements the abstract base class MessageHandler<TMessage>. Ideally I'd also like an class implementing IMessageHandler to be able to get access, but I think that makes the requirement a bit tougher.

This will run on the xbox and so I want to avoid creating temporary objects (such as read-only copies). I also want to minimise the number of method calls to get at the value that's read/written to.

The Component class and MessageHandler<TMessage> classes are currently in their own assemblies, both of which will be referenced by other projects when using my API.

I'm guessing I'm going to have to change my model somehow, but I can't get my head around it.

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不爱素颜 2025-01-04 21:35:56

隔离(基于您提出的问题和我的理解)是基于基类定义(如果有)进行的。这意味着隔离应该从它开始。

或者,如果您说,如果某个 class X 实现 MessageHandler 应该能够以多种方式对 class Y 类型对象起作用。在我看来,这意味着两者之间存在着紧密的关系
Y 类MessageHandler

这让我觉得你可以做这样的事情:

  • only public get on the properties of DerivedComponentA
  • in MessageHandler 定义一个通用的 protected SetProperty(Component compo, string propertyName, object propertyValue) 并通过反射设置所需的属性。

通过这种方式,在任何 Component 派生类上设置属性的唯一可能方法是使用 MessageHandler 方法,因此仅适用于这些派生类谁从中衍生出来。对于其余可用类型,您提供public get(只读)权限来读取数据。

希望这有帮助。

The isolation (based on question you made and what I understood) is made based on base class definition (if any). That means the isolation should start from it.

Or, if you say, that if some class X implements MessageHandler should be able to act in several way on class Y type objects. That, imo, means that there is a tough relation between
class Y and MessageHandler.

This leads to think me that you can do somehting like this:

  • only public get on the properties of DerivedComponentA
  • in MessageHandler define a generic protected SetProperty(Component compo, string propertyName, object propertyValue) and set required property with reflection.

In this way only possible way to set a property on any Component derived class is by using MessageHandler method, so will be available only for those ones who derive from it. For the rest of available types you provide only public get (readonly) poperty for reading data.

Hope this helps.

荆棘i 2025-01-04 21:35:56

我建议你在这里使用接口。拥有一个具有只读属性的只读接口 IReadOnlyComponent 和一个为该属性添加 Set 方法的 IWritableComponent : IReadOnlyComponent
抽象基类 Component 实现 IWritableComponent,因此具有只读属性和 Set 方法。
仅将组件实例作为 IWritableComponent 传递给继承自 MessageHandler 或实现 IMessageHandler 的类,并将其作为 IReadOnlyComponent 传递给所有其他课程。

I suggest you work with interfaces here. Have one read only interface IReadOnlyComponent that has a read only property and an IWritableComponent : IReadOnlyComponent that adds a Set method for the property.
The abstract base class Component implements IWritableComponent and thus has the read only property and the Set method.
Pass the component instance as IWritableComponent only to classes that inherit from MessageHandler or implement IMessageHandler and pass it as IReadOnlyComponent to all other classes.

眼眸 2025-01-04 21:35:56

你能不能只像这样控制setter:

public string Prop {
  get { return ...; }
  set { if (!(this is MessageHandler<TMessage>)) 
           throw Exception(...);
        .... setter code;
  }
}

Can you not just control the setter like this:

public string Prop {
  get { return ...; }
  set { if (!(this is MessageHandler<TMessage>)) 
           throw Exception(...);
        .... setter code;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文