简体   繁体   English

循环中的c#DynamicObject类动态属性

[英]c# DynamicObject class dynamic properties from loop

So I've created a class that inherits DynamicObject 所以我创建了一个继承DynamicObject的类

public class MyDynamicObject : DynamicObject{
private Dictionary<string, object> Fields = new Dictionary<string, object>();

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
    return Fields.TryGetValue(binder.Name, out result);
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
    Fields[binder.Name] = value;
    return true;
}
}

And call this class here 并在这里叫这个班

public class Program
{
public static void Main()
{
    dynamic person = new MyDynamicObject();
    person.firstname = "Hello";
    Console.WriteLine(person.firstname);
 }
}  

Of course this will work. 当然可以。 But I need to create properties from a string array like 但是我需要从像这样的字符串数组创建属性

string[] fields = new string[]{"taxid","newcol","addrs","gender"};
dynamic person = new MyDynamicObject();
foreach(var f in fields)
{
  person.f = "hello";
}

So the output will be person.taxi, person.newcol, person.addrs, person.gender 因此输出将为person.taxi,person.newcol,person.addrs,person.gender

Is this possible? 这可能吗?

Expose the Fields dictionary in some way, or (better) a method that allows one to explicitly set a property by name. 以某种方式或更好的方法公开Fields字典,该方法允许人们按名称显式设置属性。

Note that ExpandoObject already does this, as it can be cast to IDictionary<string, object> and then you 请注意, ExpandoObject已经执行了此操作,因为可以将其IDictionary<string, object>IDictionary<string, object> ,然后您可以

ExpandoObject eo = new ExpandoObject();
IDictionary<string, object> dict = eo;
dynamic d = eo;
dict["MyProperty"] = 42;
Console.WriteLine(d.MyProperty); // 42

If you can't just use ExpandoObject itself, you can copy its approach. 如果不能只使用ExpandoObject本身,则可以复制其方法。

Okay so based on the suggestion of @Jon Hanna, I came up with a solution that fits my requirements. 好的,所以根据@Jon Hanna的建议,我想出了一个适合我要求的解决方案。 I created a new Add method which accept a name. 我创建了一个接受名称的新Add方法。 Below is the updated code I used. 以下是我使用的更新代码。

public class DynamicFormData : DynamicObject
{
    private Dictionary<string, object> Fields = new Dictionary<string, object>();

    public int Count { get { return Fields.Keys.Count; } }

    public void Add(string name, string val = null)
    {
        if (!Fields.ContainsKey(name))
        {
            Fields.Add(name, val);
        }
        else
        {
            Fields[name] = val;
        }
    }

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

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

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        if (Fields.ContainsKey(binder.Name) &&
            Fields[binder.Name] is Delegate)
        {
            Delegate del = Fields[binder.Name] as Delegate;
            result = del.DynamicInvoke(args);
            return true;
        }
        return base.TryInvokeMember(binder, args, out result);
    }
}

Then I just call it like this. 然后我就这样称呼它。

string[] fields = new string[]{"taxid","newcol","addrs","gender"};
dynamic formData = new DynamicFormData();

foreach(string field in fields)
{
    formData.Add(field, null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM