Click here to Skip to main content
15,905,875 members
Articles / Web Development / ASP.NET
Article

Dynamic Members in C#

Rate me:
Please Sign up or sign in to vote.
1.00/5 (10 votes)
10 Jan 2006 50.5K   470   18   14
An article on how to add dynamic members to your objects at runtime.

Sample Image

Introduction

Very often, you have to inherit from classes even if there is only one more variable you have to add. Using this code, you will be able to add members dynamically at runtime.

Using the code

To get the advantage of dynamic members, you have to inherit your classes from Object. This class offers the interface to add members dynamically. The interface consists of only four functions.

C#
class Object
{
    public bool setDynamicMember(string sName, object oObject);
    public object getDynamicMember(string sName);
    public bool addDynamicMember(string sName, object oObject);
    public bool removeDynamicMember(string sName); 
}

I think the names of the functions are self explanatory.

Points of Interest

Each dynamic member is represented by an instance of a storage class called dynamicMemberContainer.

C#
class dynamicMemberContainer
{
    public string sName;
    public object oObject;
}

These instances are listed in an array (System.Collections.Arraylist).

History

This is the first version of the code, so there is no history.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
archippus19-Oct-09 8:57
archippus19-Oct-09 8:57 
GeneralComments Pin
Keith Farmer10-Jan-06 21:32
Keith Farmer10-Jan-06 21:32 
Dictionary<t,k> is available in 2.0. Dictionary<string, k=""> would create a hashtable with string keys and values of type K.

Also, look up ExtenderProvider. It's the mechanism that Win- and WebForms use to bind tooltips, help, and the like to instances.

It's very bad to create a type that conflicts with a keyword, particularly a common one such as "object". At the least, you could use "MyObject", "FooObject" or "ExtendedObject".

For dynamic language behavior like what you're trying to emulate, I suggest trying IronPython, Boo, or Monad.
GeneralTwo Questions Pin
JohnDeHope310-Jan-06 9:26
JohnDeHope310-Jan-06 9:26 
GeneralRe: Two Questions Pin
till_hm10-Jan-06 9:41
till_hm10-Jan-06 9:41 
GeneralRe: Two Questions Pin
JohnDeHope310-Jan-06 10:01
JohnDeHope310-Jan-06 10:01 
GeneralAnother note Pin
Nish Nishant10-Jan-06 9:24
sitebuilderNish Nishant10-Jan-06 9:24 
GeneralRe: Another note Pin
till_hm10-Jan-06 9:42
till_hm10-Jan-06 9:42 
GeneralRe: Another note Pin
Nish Nishant10-Jan-06 9:50
sitebuilderNish Nishant10-Jan-06 9:50 
GeneralRe: Another note Pin
JohnDeHope310-Jan-06 10:03
JohnDeHope310-Jan-06 10:03 
GeneralRe: Another note Pin
Jeff.Wang10-Jan-06 18:56
Jeff.Wang10-Jan-06 18:56 
GeneralRe: Another note Pin
cserial11-Jan-06 1:32
cserial11-Jan-06 1:32 
GeneralRe: Another note Pin
Jeff.Wang11-Jan-06 14:32
Jeff.Wang11-Jan-06 14:32 
GeneralRe: Another note Pin
JohnDeHope311-Jan-06 3:19
JohnDeHope311-Jan-06 3:19 
Question"Object" ? Pin
Nish Nishant10-Jan-06 9:21
sitebuilderNish Nishant10-Jan-06 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.