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);
}
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.
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.
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 string
s 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With