Click here to Skip to main content
15,915,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How to generate class properties dynamically with array of parameters passed. Something like below, can any give me good and simple example
C#
[WebMethod]
       public static object LoadData(NameValue[] dtdata)
       {
           // Creating a dynamic dictionary.
           dynamic dt = new DynamicDictionary();
           foreach (var value1 in dtdata)
           {
             var key = value1.name;
             dt.key = value1.value; // this is error
             //should be something like dt.Firstname, I need to loop around each parameter and create the same as property
           }
           return dt;
       }

basically I should return object with all the generated properties. (just like normal object which has dot operator accessibility)

Thanks in Advance
Posted
Updated 11-Apr-12 19:42pm
v3

1 solution

Look at the System.CodeDom namespace. There you'll find many classes for generating code (classes with methods and properties) dynamically.
But when it comes to using your freshly generated classes, I do recommend that they should implement some interface(s), otherwise you'll have to use reflection to access them.
 
Share this answer
 
Comments
VJ Reddy 12-Apr-12 2:52am    
Good suggestion. 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900