473,770 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on, 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.Reflecti on.MemberInfo info = typeof(DerivedT ype);

Type inheritedType = inheritedFrom.G etType();
MemberInfo [] inheritedMember s = inheritedType.G etMembers();

Type thisType = derived.GetType ();
MemberInfo [] thisMembers = thisType.GetMem bers();

foreach( MemberInfo thisMember in thisMembers )
{
if parentFrame. thisMember is null
set it to inheritedFrom.t hisMember'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 1586

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

Similar topics

5
451
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 executes, but the value does not change, nor is there an exception thrown. 2) Read comments in sub routine "TestIntegerProperty"
11
12795
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
1997
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
2104
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 utilize in myownclass but the method is private and can't be inheirited. I need to utilize that base class method. -- Dennis in Houston
2
1167
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 datagrid.pagesize. I am having trouble trying to find a way to pass the name of the existing datagrid object to the function and to have it assign its the appropriate datasource, pagesize and rebind. I found a post with this ControlFromName Function and...
11
3490
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 set the property of the instance to the value here is an example of what the method might look like public void SetProperty(object instance, string property, object value) {
3
4185
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
5102
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 information about propertygrid but most of the information seems to be for VS2003.
7
1695
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
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.