C#使用dynamicobject类从循环字符串设置动态属性 [英] C# setting dynamic properties from a loop string using dynamicobject class

查看:148
本文介绍了C#使用dynamicobject类从循环字符串设置动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是仅使用DynamicObject创建具有动态属性的动态类.因此,我创建了一个继承DynamicObject的类.

My goal is to create a dynamic class with dynamic properties using only DynamicObject. So I''ve created a class that inherits 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;
}
}
 
public class Program
{
public static void Main()
{
    dynamic person = new MyDynamicObject();
    person.firstname = "Hello";
    Console.WriteLine(person.firstname);
 }
}  



当然,上面的代码将按预期工作.但是我的问题是属性是来自字符串数组.喜欢...

我尝试过的事情:



Of course the above code will work as expected. But my problem is the properties is from a string array. Like...

What I have tried:

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



这样我就可以输出
person.taxid,很快....
这可能吗?

注意:我不能使用ExpandoObject,所以有没有办法像ExpandoObject那样实现呢?
谢谢.



So I could output like
person.taxid and soon....
Is this possible?

Note: I can''t use ExpandoObject, so is there a way I can implement this like ExpandoObject?
Thanks

推荐答案

好的,所以根据某人的建议,我想出了一个适合我要求的解决方案.我创建了一个新的Add方法,该方法接受一个名称.以下是我使用的更新代码.

Okay so based on the suggestion of someone, I came up with a solution that fits my requirements. I created a new Add method which accept a name. 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);
}


这篇关于C#使用dynamicobject类从循环字符串设置动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆