C# Dynamically Set Property

C# dynamically set property

objName.GetType().GetProperty("nameOfProperty").SetValue(objName, objValue, null)

Dynamically Add C# Properties at Runtime

Have you taken a look at ExpandoObject?

see: https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject

From MSDN:

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject.sampleMember instead of more complex syntax like sampleObject.GetAttribute("sampleMember").

Allowing you to do cool things like:

dynamic dynObject = new ExpandoObject();
dynObject.SomeDynamicProperty = "Hello!";
dynObject.SomeDynamicAction = (msg) =>
{
Console.WriteLine(msg);
};

dynObject.SomeDynamicAction(dynObject.SomeDynamicProperty);

Based on your actual code you may be more interested in:

public static dynamic GetDynamicObject(Dictionary<string, object> properties)
{
return new MyDynObject(properties);
}

public sealed class MyDynObject : DynamicObject
{
private readonly Dictionary<string, object> _properties;

public MyDynObject(Dictionary<string, object> properties)
{
_properties = properties;
}

public override IEnumerable<string> GetDynamicMemberNames()
{
return _properties.Keys;
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_properties.ContainsKey(binder.Name))
{
result = _properties[binder.Name];
return true;
}
else
{
result = null;
return false;
}
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (_properties.ContainsKey(binder.Name))
{
_properties[binder.Name] = value;
return true;
}
else
{
return false;
}
}
}

That way you just need:

var dyn = GetDynamicObject(new Dictionary<string, object>()
{
{"prop1", 12},
});

Console.WriteLine(dyn.prop1);
dyn.prop1 = 150;

Deriving from DynamicObject allows you to come up with your own strategy for handling these dynamic member requests, beware there be monsters here: the compiler will not be able to verify a lot of your dynamic calls and you won't get intellisense, so just keep that in mind.

How to dynamically set the value of a property of object instance using reflection?

The first argument you pass should be the instance holding the property you wish to set. If it's a static property pass null for the first argument. In your case change the code to:

  public void setSpeed()
{
Type type = this.GetType();
PropertyInfo property = type.GetProperty(PropertyName );
property.SetValue(this, Convert.ToInt32(PropertyValue), null);
}

for a naïve type conversion you could do

   var value = Convert.ChangeType(PropertyValue,property.PropertyType);
property.SetValue(this, value, null);

C# how Set property dynamically by string?

You're trying to set get a property named "ContractType" on _cotreport and set it's value with on _cotreport.Contract. That's not going to work for two reasons.

  1. The property name (from what I can tell in your code) is Contract not ContractType.
  2. You need to set the value on _cotreport.

Try this instead

System.Reflection.PropertyInfo property = _cotreport.GetType().GetProperty("Contract");
property.SetValue(_cotreport, COTReportHelper.ContractType.Gold, new object[0]);

If you want to set the enum value by name, that's a separate issue. Try this

var enumValue = Enum.Parse(typeof(COTReportHelper.ContractType), "Gold");
property.SetValue(_cotreport, enumValue, new object[0]);

How to assign values to properties dynamically

You can use Reflection to make your code dynamic.

To make you understand more, You can look below code

getMyDatas.cs

public class getMyDatas
{
public bool AFlag { get; set; }
public bool BFlag { get; set; }
public bool CFlag { get; set; }
}

Program.cs

using StackOverFlowTest;
using System;
using System.Reflection;

//input
string actionType = "A";

//Business
var Data = new getMyDatas();

PropertyInfo[] properties = typeof(getMyDatas).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(Data, Convert.ChangeType(property.Name.StartsWith(actionType), property.PropertyType), null);
}

//Testing
Console.WriteLine(Data.AFlag);
Console.WriteLine(Data.BFlag);
Console.WriteLine(Data.CFlag);

Shortly, I check if property name starts with input



Related Topics



Leave a reply



Submit