Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to allow new fields to be added in my class by calling class.
Posted

This[^] will do it, if anything will.
 
Share this answer
 
Comments
Tarun.K.S 8-Jul-11 5:11am    
Nice article. 5+
whitehaker 8-Jul-11 7:08am    
Thanks alot good article. my 5
fjdiewornncalwe 8-Jul-11 7:18am    
I like that link. I don't think I ever saw that article. +5
The key here is using System.Reflection.Emit.
See http://msdn.microsoft.com/en-us/library/3y322t50.aspx[^].

This is one of the very difficult topics. Do you really want what are you asking about? Could you share your ultimate goal of it? Most likely much more simple approach could be considered.

—SA
 
Share this answer
 
v2
Comments
Christian Graus 8-Jul-11 4:47am    
Yes, there is no doubt that if he adds properties, they won't be strongly typed, or easily accessed.
Sergey Alexandrovich Kryukov 8-Jul-11 4:56am    
Absolutely! As I say, this is difficult. True, emitting properties themselves is still all right; using them would be a real pain.
--SA
Tarun.K.S 8-Jul-11 5:11am    
Good answer! 5+
Sergey Alexandrovich Kryukov 8-Jul-11 5:14am    
Thank you, Tarun.
--SA
Espen Harlinn 8-Jul-11 7:02am    
My 5
As SAKryukov mentions, there could be a simpler solution - obviously depending on what you are doing. Could it be that you are looking for attached properties[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jul-11 1:11am    
Wait a minute? WPF? However, it's not specified. Or did you mean using the engine without the rest of WPF (I'm pretty sure it would be possible)? Interesting idea anyway, my 5.
--SA
Espen Harlinn 10-Jul-11 6:57am    
It's one of the few other ways I'm able to make sense out of the question. If this is what he want - it makes sense, but it could have been better formulated.

Code injection, as you pointed out, is the other :)
Sergey Alexandrovich Kryukov 12-Jul-11 16:30pm    
Agree, only I don't know where I pointed out "code injection". I don't think I did.
Thank you.
--SA
Espen Harlinn 12-Jul-11 16:52pm    
That's what you do when you modify an existing class using emit :)
I am fairly sure you don't actually want what you say you want. Properties added dynamically could not possibly be accessed in compiled code (since you wouldn't know how to reference them). So there is almost no point in actually creating properties. What you want is somewhere for a calling class to store information against names. You can do that with a Hashtable.
 
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