在运行时动态添加c#属性

本文关键字:属性 添加 动态 运行时 | 更新日期: 2023-09-27 18:07:31

我知道有一些问题可以解决这个问题,但答案通常是遵循建议使用字典或参数集合的路线,这在我的情况下行不通。

我正在使用一个通过反射工作的库,用带有属性的对象做很多聪明的事情。这适用于定义类和动态类。我需要更进一步,按照下面这些行做一些事情:

public static object GetDynamicObject(Dictionary<string,object> properties) {
    var myObject = new object();
    foreach (var property in properties) {
        //This next line obviously doesn't work... 
        myObject.AddProperty(property.Key,property.Value);
    }
    return myObject;
}
public void Main() {
    var properties = new Dictionary<string,object>();
    properties.Add("Property1",aCustomClassInstance);
    properties.Add("Property2","TestString2");
    var myObject = GetDynamicObject(properties);
    //Then use them like this (or rather the plug in uses them through reflection)
    var customClass = myObject.Property1;
    var myString = myObject.Property2;
}

库可以很好地处理动态变量类型,手动分配属性。但是,我不知道在此之前会添加多少或哪些属性。

在运行时动态添加c#属性

你看过ExpandoObject了吗?

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

从MSDN:

ExpandoObject类使您能够在运行时添加和删除其实例的成员,还可以设置和获取这些成员的值。该类支持动态绑定,这使您能够使用sampleObject之类的标准语法。而不是更复杂的语法,如sampleObject.GetAttribute("sampleMember"。

允许你做一些很酷的事情,比如:

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

根据您的实际代码,您可能更感兴趣:

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;
        }
    }
}

这样你只需要:

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

派生自DynamicObject允许你想出你自己的策略来处理这些动态成员请求,注意这里有怪物:编译器将能够验证你的许多动态调用,你不会得到智能感知,所以请记住这一点。

感谢@Clint的精彩回答:

只是想强调使用Expando对象解决这个问题是多么容易:

var dynamicObject = new ExpandoObject() as IDictionary<string, Object>;
foreach (var property in properties) {
    dynamicObject.Add(property.Key,property.Value);
}      

你可以将json字符串反序列化为字典,然后添加新的属性,然后序列化它。

var jsonString = @"{}";
var jsonDoc = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
jsonDoc.Add("Name", "Khurshid Ali");
Console.WriteLine(JsonSerializer.Serialize(jsonDoc));