I have class Base, and class Derived.
I am serializing Derived objects from XML, and these Derived objects are
allowed to 'inherit' the values from other named Base objects already defined
in the XML.
<Base name="cat"/>
<Derived name="tabby" inherits="cat"/>
Some Derived objects are not fully described in the XML, and their members
are left null. They should be initializing these null values from the Base
objects.
I bought Programming C#, (O'Reilly) and it has a good example on reflecting
an class's members and methods. It even shows how to instantiate an object
and invoke its methods using pure reflection.
I'm not trying to do either of those things.
I have an already created Derived object. I am iterating over its members
using System.Reflection, checking if they are null, and seeing if the
instance of the Base object it derives from also has the corresponding member.
If the member is null, and the Base object has that member as well, I want
to assign Derived.member to the value of Base.member. These are all instances
of these classes, not static members or methods.
I have no idea how to do actually do the assignment.( How do I get a
reference to my existing Derived.member and Base.member programatically?
I don't want to have to type in each member method if Reflection can do this
for me. This would also save me time because it would work even if I added or
removed members.
System.Reflection.MemberInfo info = typeof(DerivedType);
Type inheritedType = inheritedFrom.GetType();
MemberInfo [] inheritedMembers = inheritedType.GetMembers();
Type thisType = derived.GetType();
MemberInfo [] thisMembers = thisType.GetMembers();
foreach( MemberInfo thisMember in thisMembers )
{
if parentFrame. thisMember is null
set it to inheritedFrom.thisMember's value
}
I've only spent about a week with C#, so I'm open to any sort of criticism,
even if it doesn't directly relate to my problem.