Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add properties to class dynamically C#

Tags:

c#

I want to add properties to a class dynamically and set values to them. Could someone point me in the right direction to some tutorials or articles or even better provide an example.

I have a base class which is called in a foreach loop. Inside this class I need to add up to 30 properties and set values to them.

This is the method that calls the class.

foreach (Class name in this.list)
{
    ClassBeingCalled class = new ClassBeingCalled (name);
    class.Populate();
    this.newlist.Add(class);
}

Now inside the class being called, I need to create the 30 or so properties on the fly, which will be set in the "Populate" method of that class.

Like so...

 foreach (PropertyToAssign count2 in listofproperties)
 {
     string name = "_nameofproperty" + count2.name
     base.GetType().GetField(name, BindingFlags.NonPublic |
                          BindingFlags.Instance).SetValue(this, count2);
 }
like image 278
Pavan Dhariwal Avatar asked May 16 '13 11:05

Pavan Dhariwal


People also ask

How to add properties to a Class Dynamically in c#?

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. Add(new Line() { Id = 2, Name = "Two", Age = 35 }); lines.

How do I add property to ExpandoObject?

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.


2 Answers

There are a few ways to approach this.

a) You use reflection to generate a class at runtime with whatever you need. You can always derive this generated class from a base class that contains Populate but I don't see the need.

b) If you're using C# 4.0+, you could use ExpandoObject, which allows you to set whatever properties you want for your needs. Note that these are not added as properties per se, as it uses the DLR which is all runtime-based. Depending on your needs, this may be fine; this is pretty much the same as approach c, but using the DLR.

c) You could use a backing dictionary and use an indexer, like so:

private Dictionary<string, object> _internalData = new Dictionary<string, object>();

public T this[string propName]
{
    get {
        return _internalData[propName];
    }
    set {
        _internalData[propName] = value;
    }
}

By using approach 3, you're actually indexing based on strings which is probably better based on your requirements... As you can do something along the lines of this:

foreach (PropertyToAssign count2 in listofproperties)
{
    string name = "_nameofproperty" + count2.name;
    this[name] = count2.value;
}

At the end of the day, it's probably best if you explain the problem you're trying to solve, as there may be a better way to approach it entirely.

like image 146
Rudi Visser Avatar answered Sep 24 '22 01:09

Rudi Visser


It is not possible to add dynamic properties to a compiled class on the fly. You can only add dynamic properties to classes while building the class itself on the fly using Reflection.Emit

Your other option could be using a Dictionary or ExpandoObject (which is actually a dictionary). But for this option, you cannot access your dynamic properties using reflection.

like image 33
Mehmet Ataş Avatar answered Sep 24 '22 01:09

Mehmet Ataş