473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I add a member field to an existing class in runtime?

Is there any way I can add a member field to an already existing class
during runtime?

I know there are many examples using Reflection.Emit showing how to
dynamically create a new class and adding things to it, but that's not what
I want. I really need to use existing classes. I have investigated the
Profiler API, but having its dll snapped to the CLR reminds me of the Borg
and their implants. It is not really what I -or my customers- want for
production.

I need this because I'm creating a simple OO database which needs references
to objects when updating. The value that I want to add on the fly is the
object id.

Thanks!
Manfred Suttorp

PS. I would appreciate relevant comments only - no lectures about OO design
or telling me I shouldn't do this please.
May 8 '06 #1
1 1357
You could very easily use any of the major AOP frameworks for .NET to
implement this as a "mixin" .. aspect#, naspect, and many others support
this type of behavior out of the box.

Or if you don't like that idea .. you could do the same thing they do .. on
your own without the overhead of the additional functionality

say I have the following class ..

public class Foo {
public int Bar;
public void Foo() { }
}

dynamically generate a class that looks like the following.

public class AddedId : Foo {
public long objectid;
}

If you are creating objects through a factory .. this is quite simple .. if
you are not you may have to get into situations where you use reflections to
copy over data from an existant foo into your class.

Cheers,

Greg Young
"Mannie" <.> wrote in message
news:44**********************@dreader31.news.xs4al l.nl...
Is there any way I can add a member field to an already existing class
during runtime?

I know there are many examples using Reflection.Emit showing how to
dynamically create a new class and adding things to it, but that's not
what I want. I really need to use existing classes. I have investigated
the Profiler API, but having its dll snapped to the CLR reminds me of the
Borg and their implants. It is not really what I -or my customers- want
for production.

I need this because I'm creating a simple OO database which needs
references to objects when updating. The value that I want to add on the
fly is the object id.

Thanks!
Manfred Suttorp

PS. I would appreciate relevant comments only - no lectures about OO
design or telling me I shouldn't do this please.

May 8 '06 #2

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

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
13
by: Andy Lomax | last post by:
I'm deriving from a base class in a library, and the library vendor is expecting me to add a 'string&' member in my derived class. Or, to be more precise, they have provided two virtual functions:...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
3
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: Alexander Mueller | last post by:
Owen Blacker schrieb: Something that is static just exists once. If you could override or derive it, you would create it for a second time. See it like this:
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.