How Can I add properties to a class on runtime in C#?
Solution 1
You cannot extend an existing class with new members at runtime. However, you can create a new class using System.Reflection.Emit that has the existing class as base class.
typeBuilder.SetParent(typeof(MyClass));
typeBuilder.DefineProperty("Prop1", ..., typeof(System.Int32), null);
...
See TypeBuilder.DefineProperty Method (String, PropertyAttributes, Type, Type[]) for a full example.
Solution 2
I think you have misunderstood what reflection is. Reflection does not allow you to change the code you are running. It can allow you to read your own code and even run that code.
With Reflection.Emit you can write a whole new assembly and then have it loaded into memory.
However you CANNOT change a class at runtime. Imagine, you have a class Foo, you instanciate a whole bunch of them, then decide to change the class.
What should happen to all the Foos you already instanciated?
However, what you CAN do it use Reflection.Emit to create a new class that inherits Foo, called Bar, and then add whatever you need to that class.
Alternatively if you want to programmatically add a property to a class AFTER compile time but BEFORE run time, you can look at IL Weaving/AOP. I suggest looking at PostSharp or Fody if this is what you are interested in.
Solution 3
Yes, you can use the ExpandoObject
class to achieve this.
dynamic expando = new ExpandoObject();
expando.Prop1 = 10;
expando.Prop900 = string.Empty;
Related videos on Youtube
Royi Namir
Updated on July 09, 2022Comments
-
Royi Namir almost 2 years
I have a class :
class MyClass { } ... MyClass c = new MyClass();
Is it possible to add properties / fields to this class on run-time ?
(I don't know what are their types or names on compile-time and they don't have a common interface which I can use.)
psuedo example :
Add property named "Prop1" [type System.Int32] Add property named "Prop900" [type System.String]
I already read this question but it uses interface
Thanks in advance.
-
Henk Holterman about 11 yearsAnd then access them through reflection everywhere? How about a
Dictionary<string, object>
? -
Dennis about 11 yearsWhat use-case do you need it for? What about
ICustomeTypeDescriptor
? -
Dennis about 11 years@RoyiNamir: reflection isn't about IL modification - that's what you want to achieve.
-
Jason Larke about 11 yearsWhy would you want to 'learn' reflection by jumping straight into possibly the worst use of it?
-
-
Royi Namir about 11 yearsI've succeed doing it. thanks. ( that was the way). Thanks for answering the actual question.