Click here to Skip to main content
Licence 
First Posted 10 Jan 2006
Views 34,704
Bookmarked 17 times

Dynamic Members in C#

By | 10 Jan 2006 | Article
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.

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.

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

About the Author

Till Hoffmann



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberarchippus8:57 19 Oct '09  
GeneralComments PinmemberKeith Farmer21:32 10 Jan '06  
GeneralTwo Questions PinmemberJohnDeHope39:26 10 Jan '06  
GeneralRe: Two Questions Pinmembertill_hm9:41 10 Jan '06  
GeneralRe: Two Questions PinmemberJohnDeHope310:01 10 Jan '06  
GeneralAnother note PinstaffNishant Sivakumar9:24 10 Jan '06  
GeneralRe: Another note Pinmembertill_hm9:42 10 Jan '06  
GeneralRe: Another note PinstaffNishant Sivakumar9:50 10 Jan '06  
GeneralRe: Another note PinmemberJohnDeHope310:03 10 Jan '06  
GeneralRe: Another note Pinmemberwangjianfei18:56 10 Jan '06  
GeneralRe: Another note Pinmembercserial1:32 11 Jan '06  
GeneralRe: Another note Pinmemberwangjianfei14:32 11 Jan '06  
GeneralRe: Another note PinmemberJohnDeHope33:19 11 Jan '06  
Question"Object" ? PinstaffNishant Sivakumar9:21 10 Jan '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Jan 2006
Article Copyright 2006 by Till Hoffmann
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid