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!