1

I was wondering how you can get the actual base object of an object? I have a base class named MessageBase, and a lot of classes that inherit this base class, which themselves get inherited by other classes in different depths. An example:

Some classes inherit like this:

MessageBase --> ValueMessage --> DoubleValueMessage

others like that:

MessageBase --> ValueMessage --> [some class] --> [some other class] --> TestMessage

What I need is this: I have an object of one of these inherited classes, let's say an instance of DoubleValueMessage. I don't know in advance the type of this object, all I know is that somehwere down the nested inheritance, there is a base object (MessageBase) whose properties I need to set.

Now I tried to get a reference to that base object. So tried to get the ValueMessage the DoubleValueMessage is based upon, but I don't understand how.

I tried it like that:

public bool SetPropertyValue(object obj, string prop, object value)
{
    var item = obj.GetType().GetProperty(prop);
    //MessageBox.Show(obj.ToString());
    if (item != null)
    {
        item.SetValue(obj, value);
        return true;
    }
    else
    {
        if (obj.base != null)
        {
            SetPropertyValue(obj.base, prop, mb);
        }
    }

    return false;
}

The idea is this: I pass in an object (e.g. of type DoubleValueMessage), the property I want to set (is a base object even a property in the first place?) and an object that needs to replace the given property, in my case the MessageBase. So I thought it would be a good idea to recursively iterate down the inheritance hierarchy until the property that I need to set is found.

THe problem is: the .base keyword does not seem to be the right way to get the base object.

How can I get it? Thanks in advance for your help!

4

2 回答 2

8

基础对象不是单独的对象。派生对象基础对象。您可以像访问派生类的属性一样访问基属性,除非这些属性是私有的。如果你想要一个基类的实例然后转换对象,例如

var baseInstance = obj as MessageBase;

...但听起来您实际上不需要这样做。

于 2013-07-01T10:15:26.837 回答
1

您可以使用以下代码获取基类型的私有属性:

public bool SetPropertyValue(object obj, string prop, object value) {
    var item = obj.GetType().BaseType.GetProperty(prop, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    //MessageBox.Show(obj.ToString());
    if (item != null) {
        item.SetValue(obj, value);
        return true;
    }

    return false;
}

有关 BindingFlags 的更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx

对于多层继承,您可以执行以下操作:

public bool SetPropertyValue(object obj, string prop, object value) {
    var item = GetBaseType(obj.GetType()).GetProperty(prop, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    //MessageBox.Show(obj.ToString());
    if (item != null) {
        item.SetValue(obj, value);
        return true;
    }


    return false;
}

public Type GetBaseType(Type type) {
    if (type.BaseType != typeof(object)) {
        return GetBaseType(type.BaseType);
    }
    return type;
}
于 2013-07-01T11:39:32.977 回答