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
}
}
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:
get
on the properties of DerivedComponentA
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.