Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi.
I have this code in flex
C#
var obj:Object  = new Object();
obj.firstName = "first name";
obj.lastName = "last name";
return obj;

How can I write similar code in c#? I exactly wants is dynamic object having dynamic properties
Posted
Updated 9-Dec-11 4:53am

You must be studying Anonymous Types in C#.

Here is the link to get you started:
http://msdn.microsoft.com/en-us/library/bb397696.aspx[^]

Regards,
Eduard
 
Share this answer
 
Comments
niravsahayata 21-Dec-11 6:02am    
how can i pass Anonymous variables to function??
Please check Anonymous functions in MSDN
var obj = new { FirstName = "first name", LastName = "last name" };
 
Share this answer
 
Comments
niravsahayata 21-Dec-11 6:02am    
how can i pass Anonymous variables to function??
raju melveetilpurayil 21-Dec-11 6:06am    
http://stackoverflow.com/questions/3433856/passing-anonymous-type-as-method-parameters
may help you..
I like this question because it has nice code. I sometimes could use this question next time I want to ask a good question. I think it is really helping me to read these good questions. Soon I want to make an answer will nice code like this one. Thank you for posting and helping me!
 
Share this answer
 
v3
Comments
[no name] 9-Dec-11 10:51am    
This is place for writing your solution. If you want to appreciative question comment on it. Also, you can give your ratings.
Hugo1991 9-Dec-11 11:03am    
Yes this is a good solution for me. I am getting better. Glad you also like it too. Thank you for the rating!
 
Share this answer
 
For creating a dynamic object you need do derive your class from DynamicObject class and override its TryGetMember and TrySetMember.
Please study the example in this page :
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trygetmember.aspx[^]

Hope it helps.
 
Share this answer
 

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