如何动态新的匿名类?

Fla*_*ash 91 c# anonymous-types c#-3.0

在C#3.0中,您可以使用以下语法创建匿名类

var o1 = new { Id = 1, Name = "Foo" };
Run Code Online (Sandbox Code Playgroud)

有没有办法动态地创建这些匿名类到变量?


例:

var o1 = new { Id = 1, Name = "Foo" };
var o2 = new { SQ = 2, Birth = DateTime.Now };
Run Code Online (Sandbox Code Playgroud)

动态创建示例:

var o1 = DynamicNewAnonymous(new NameValuePair("Id", 1), new NameValuePair("Name", "Foo"));
var o2 = DynamicNewAnonymous(new NameValuePair("SQ", 2), new NameValuePair("Birth", 
DateTime.Now));
Run Code Online (Sandbox Code Playgroud)

因为我需要这样做:

dynamic o1 = new ExpandObject(); 
o1."ID" = 1;    <--"ID" is dynamic name
o1."Name" = "Foo";  <--"Name" is dynamic name
Run Code Online (Sandbox Code Playgroud)

和Scene1:

void ShowPropertiesValue(object o)
{
  Type oType = o.GetType();
  foreach(var pi in oType.GetProperties())
  {
    Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我打电话:

dynamic o1 = new ExpandObject();
o1.Name = "123";
ShowPropertiesValue(o1);
Run Code Online (Sandbox Code Playgroud)

它无法显示结果:

Name = 123
Run Code Online (Sandbox Code Playgroud)

还有我如何将ExpandoObject转换为AnonymouseType?

Type type = o1.GetType();
type.GetProperties();   <--I hope it can get all property of o1
Run Code Online (Sandbox Code Playgroud)

最后,我修改了ShowPropertiesValue()方法

void ShowPropertiesValue(object o)
{
  if( o is static object ) <--How to check it is dynamic or static object?
  {
    Type oType = o.GetType();
    foreach(var pi in oType.GetProperties())
    {
      Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
    }
  }
  else if( o is dynamic object )  <--How to check it is dynamic or static object?
  {
    foreach(var pi in ??? )  <--How to get common dynamic object's properties info ?
    {
      Console.WriteLine("{0}={1}", pi.Name, pi.GetValue(o, null));
    } 
  }
}
Run Code Online (Sandbox Code Playgroud)

如何实现DynamicNewAnonymous方法或如何修改ShowPropertiesValue()?

我的动机是:

dynamic o1 = new MyDynamic();
o1.Name = "abc";
Type o1Type = o1.GetType();
var props = o1Type.GetProperties(); <--I hope can get the Name Property
Run Code Online (Sandbox Code Playgroud)

如果我可以挂钩dynamicObject的GetType方法,并且Compel转换为强类型的Type.上面的无缝代码可以正常工作.

Ste*_*dit 70

匿名类型只是隐式声明的常规类型.他们没什么关系dynamic.

Now, if you were to use an ExpandoObject and reference it through a dynamic variable, you could add or remove fields on the fly.

edit

Sure you can: just cast it to IDictionary<string, object>. Then you can use the indexer.

You use the same casting technique to iterate over the fields:

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);
}
// This code example produces the following output:
// Name: John Smith
// Age: 33
Run Code Online (Sandbox Code Playgroud)

The above code and more can be found by clicking on that link.

  • http://stackoverflow.com/a/4024786/998793显示了如何通过转换为通用字典来实现这一点:`((IDictionary <string,object>)o1).Add("Name","Foo");` .然后,您可以访问"o1.Name" (4认同)
  • 您所做的只是说动态属性与强类型属性不同.这很简单. (3认同)
  • 但 ExpandoObject 无法做到这一点:`dynamic o1 = new ExpandObject(); o1."ID" = 1; o1."名称" = "Foo";` (2认同)

Dan*_*iel 13

您可以像这样创建一个ExpandoObject:

IDictionary<string,object> expando = new ExpandoObject();
expando["Name"] = value;
Run Code Online (Sandbox Code Playgroud)

在将其转换为动态后,这些值将看起来像属性:

dynamic d = expando;
Console.WriteLine(d.Name);
Run Code Online (Sandbox Code Playgroud)

但是,它们不是实际属性,无法使用Reflection访问.所以以下语句将返回null:

d.GetType().GetProperty("Name") 
Run Code Online (Sandbox Code Playgroud)