C# - How to get/set an object's property value without using Reflection
I need to be able to store a reference to an object's property in order to
read/write it later, ideally without using reflection. I'm also open to a
design pattern that works too, but performance is paramount as my
application will be making millions of calls to "run" on various "Parent"
classes. The code template below should explain what I'm trying to do.
Also, one more requirement, I'd like to keep it such that the variables I
am "requiring" are object properties of the Child class, and not data
structures stored in some list, for example.
Thank you.
class requiredAttribute : Attribute
{
}
abstract class Parent
{
abstract protected void _run();
public Parent() {
// This can do whatever set-up is necessary in order to make the
below run() call
// not need reflection.
/* What code goes here? */
}
public void run() {
// This code should reset all 'required' properties to null.
/* What goes here? */
_run();
// This code needs to ensure any required property is now not null.
// If it finds a null one, it should throw.
/* What goes here? */
}
}
class Child : Parent
{
[required]
protected object value1;
[required]
protected object value2;
// not required..
protected object value3;
protected void _run() {
// This must set all 'required' properties' values, otherwise the
Parent should throw.
value1 = "some value";
value2 = "some other value";
}
}
No comments:
Post a Comment