C#4动态关键字 - 如何设置自定义属性

时间:2011-03-12 12:56:12

标签: dynamic c#-4.0


我应该在以下函数中更改它以便编译和正常工作?

private void Test()
{
    dynamic dyn;
    dyn.Prop1 = 'A';
    dyn.Prop2 = "asdfsdf";
    dyn.Prop3 = 5;
    foreach (PropertyInfo propertyInfo in dyn.GetType().GetProperties())
    {
        Console.WriteLine(propertyInfo.Name);
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:4)

您可能正在寻找ExpandoObject

要查看有哪些属性(取自MSDN):

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;

foreach (var property in (IDictionary<String, Object>)employee)
{
    Console.WriteLine(property.Key + ": " + property.Value);
}