动态创建属性名称 [英] Dynamically create property name

查看:51
本文介绍了动态创建属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DynamicObject类动态创建属性.在邮件方法中,我是这样使用的.(参考 http ://msdn.microsoft.com/zh-CN/library/system.dynamic.dynamicobject%28VS.95%29.aspx [

I use DynamicObject class to create properties dynamically. In mail method I use it like this.(With reference to http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject%28VS.95%29.aspx[^])

dynamic person = new DynamicProp();
p.FirstName = "AA";
p.age = 10;



但是在我的人员表中,用户可以在运行时对其进行更改.他可以添加更多字段以保留个人详细信息.
然后,我需要根据人员表的列创建属性.在这种情况下,如何使用表字段创建属性.在此示例中,我必须知道用于创建属性的属性名称.但是我不知道用户创建了哪些字段名称.



But in my person table can be altered at the runtime by the user. He can add more fields as he wants to keep the person details.
Then I need to create properties according to the person table''s columns. In this case how can I create property using table field. In this example I must know the property name to create the property. But I don''t know what field names has created by the user.

推荐答案

Hi;
我改写了你的人物课;这是:
Hi;
I rewrote your person class; here it is:
public class Person : DynamicObject
{
    public string Name { get; set; }
    public string Address { get; set; }
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();
    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        string name = binder.Name;
        return dictionary.TryGetValue(name, out result);
    }
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }
    public void AddProperty<TTValue>(string key, TTValue value = default(TTValue))
    {
        dictionary[key] = value;
    }
    public void AddProperty(string typeName, string key, object value = null)
    {
        Type type = Type.GetType(typeName);
        dictionary[key] = Convert.ChangeType(value, type);
    }
}


您可以像这样使用它:


You can use it like this:

dynamic p = new Person();
p.Name = "john";
p.Address = "address1";
p.Manager = "My Manager";
p.AddProperty<DateTime>("BirthDate", DateTime.Now);
p.AddProperty("System.String", "Weigth", "70 kg");
Console.WriteLine("My name is {0}\nMy manager is {1}\nMy birth Date is {2}",
    p.Name, p.Manager, p.BirthDate);
string w = p.Weigth;
Console.WriteLine("My Weigth is: " + w);
Console.ReadKey();



希望我能对您有所帮助.



Hope i have been helpful.


好吧,我建议使用ExpandoObject类,该类允许您创建类的动态成员.

请参阅以下链接以获取更多详细信息.

http://msdn.microsoft.com/en-us/library/system.dynamic. expandoobject.aspx [ ^ ]

使用C#中的动态关键字执行反射和XML遍历 [
Well, I will suggest to use ExpandoObject class, which allows you to create dynamic members of a class.

Please refere below links for more details.

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx[^]

Perform Reflection and XML Traversing Using the dynamic Keyword in C#[^]

I hope, this will surely help you.

Thanks :)


ExpandoObject [ ^ ],一种实现IDynamicMetaObjectProvider(就像DynamicObject一样)也实现了IDictionary<string,Object>.假设您知道添加到表中的列的名称,并且可以获取它们的值,则可以使用字典语法来设置属性,例如:
ExpandoObject[^], an implementation of IDynamicMetaObjectProvider (just like DynamicObject), also implements IDictionary<string,Object>. Assuming that you know the names of the columns added to the table, and that you can fetch their values, you can use the dictionary syntax to set properties, like this:
var person=new ExpandoObject();

// You can choose between the regular property syntax
person.FirstName="Joe";

// ...and the square bracket dictionary syntax:

person["MyDynamicallyAddedProperty"]="Value of my dynamically added property";


这篇关于动态创建属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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