简体   繁体   中英

How can I get the base object of an object (not just the BaseType)

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 (eg 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!

The base object is not a separate object. The derived object is the base object. You can just access the base properties as if they were properties of the derived class, unless the properties are private. If you want an instance of the base class then cast the object, eg

var baseInstance = obj as MessageBase;

...but it doesn't sound like you actually need to do this.

You can get the private property of the base type using the following code:

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

See http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx for more details on BindingFlags.

For multiple layers of inheritance you could do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM