我需要能够存储对对象属性的引用以便以后读/写它,最好不使用反射。我也愿意接受一种也可以工作的设计模式,但性能是最重要的,因为我的应用程序将在各种“父”类上进行数百万次“运行”调用。下面的代码模板应该解释我想要做什么。
例如,我想保留它,以便我“需要”的变量是 Child 类的对象属性,而不是存储在某个列表中的数据结构。
最后,我想我正在寻找一些超出重置对象值或检查非空值的东西。例如,在调用 _run 之后,Parent 可能会将属性的值用于其他用途。
谢谢你。
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";
    }
}