如何动态新的匿名类?

编程入门 行业动态 更新时间:2024-10-23 21:28:32
本文介绍了如何动态新的匿名类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C#3.0,你可以创建匿名类用下面的语法

In C# 3.0 you can create anonymous class with the following syntax

var o1 = new { Id = 1, Name = "Foo" };

有没有一种方法来动态创建这些匿名类给一个变量?

Is there a way to dynamic create these anonymous class to a variable?

例如:

var o1 = new { Id = 1, Name = "Foo" }; var o2 = new { SQ = 2, Birth = DateTime.Now };

动态创建实例:

Dynamic create Example:

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

怎么一回事,因为我需要做的:

Beacuse I need to do:

dynamic o1 = new ExpandObject(); o1."ID" = 1; <--"ID" is dynamic name o1."Name" = "Foo"; <--"Name" is dynamic name

和场景1:

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

如果我称之为:

dynamic o1 = new ExpandObject(); o1.Name = "123"; ShowPropertiesValue(o1);

它不能显示结果:

It can't show the result:

Name = 123

同时,我如何将ExpandoObject转换为AnonymouseType?

And also I how to Convert the ExpandoObject to AnonymouseType ?

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

最后,我修改ShowPropertiesValue()方法

Last, I modify ShowPropertiesValue() method

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)); } } }

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

How to implement DynamicNewAnonymous method or how to modify the ShowPropertiesValue()?

我的动机是:

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

如果我能勾dynamicObject的GetType方法,并迫使转换为强类型的类型。上述无缝code能正常工作。

If i can hook dynamicObject's GetType Method, and Compel convert to strongly-typed Type. The above Seamless code can work fine.

推荐答案

匿名类型只是普通类型的隐式声明。他们有一点做与动态。

Anonymous types are just regular types that are implicitly declared. They have little to do with dynamic.

现在,如果你使用的ExpandoObject并引用它通过动态变量,你可以在飞行中添加或删除字段。

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

的修改的

当然,你可以:只需将它转换为的IDictionary&LT;字符串对象&gt; 。然后你可以使用索引。

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

以上code和更可以通过点击该链接上找到。

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

更多推荐

如何动态新的匿名类?

本文发布于:2023-11-09 20:30:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!