Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamic Members in C#

0.00/5 (No votes)
10 Jan 2006 1  
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