473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Setting an existing object's members through reflection

Cat
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.
Nov 16 '05 #1
0 1563

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andre | last post by:
I have two questions; 1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
by: Lucas Tam | last post by:
Hi all, I need to set a property on a subclass such as: Report.ExportOptions.ExportFormatType = ExportFormatType.Excel AND Report.ExportOptions.FormatOptions = excelFormatOpts
9
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to...
2
by: Damon Gravning via DotNetMonster.com | last post by:
I am trying to use one function to handle a SelectedIndexChanged event for 5 radio button groups that correspond to 5 populated Datagrids. The radio buttons have a choice of 5,10 25 for a...
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
3
by: wildThought | last post by:
If I have an object that contains a generic dictionary inside of it, how do I get access to its properties such as count?
4
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I have assigned an existing object to propertygrid.selectedobject property. But I only want some of the object's properties not browsable and/or readonly. How can I do that? I'm trying to find...
7
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.