Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I add properties to a class on runtime in C#?

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.

like image 400
Royi Namir Avatar asked Feb 06 '13 08:02

Royi Namir


People also ask

How do I add a property to a runtime class?

Question. Based on condition i have to add property to class at run time. class Line { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } var lines = new List<Line>(); lines. Add(new Line() { Id=1, Name="One", Age=25 }); lines.

How to add new property to a class dynamically in c#?

We will add the Language property. // Add properties dynamically to expando AddProperty(expando, "Language", "English"); The AddProperty method takes advantage of the support that ExpandoObject has for IDictionary<string, object> and allows us to add properties using values we determine at runtime.

Can we create a class at runtime?

Dynamic Class creation enables you to create a Java class on the fly at runtime, from source code created from a string. Dynamic class creation can be used in extremely low latency applications to improve performance.

How do properties work in C#?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.


2 Answers

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.

like image 78
dtb Avatar answered Oct 16 '22 14:10

dtb


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.

like image 38
Aron Avatar answered Oct 16 '22 13:10

Aron