C# .NET 反射创建类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/799022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET Reflection Create Class Properties
提问by Russ Bradberry
I am fairly new to reflection and I would like to know, if possible, how to create an instance of a class then add properties to the class, set those properties, then read them later. I don't have any code as i don't even know how to start going about this. C# or VB is fine.
我对反射相当陌生,如果可能的话,我想知道如何创建一个类的实例,然后向该类添加属性,设置这些属性,然后再阅读它们。我没有任何代码,因为我什至不知道如何开始解决这个问题。C# 或 VB 都可以。
Thank You
谢谢你
EDIT: (to elaborate)
编辑:(详细说明)
My system has a dynamic form creator. one of my associates requires that the form data be accessible via web service. My idea was to create a class (based on the dynamic form) add properties to the class (based on the forms fields) set those properties (based on the values input for those fields) then return the class in the web service.
我的系统有一个动态表单创建器。我的一位同事要求可以通过 Web 服务访问表单数据。我的想法是创建一个类(基于动态表单)向类添加属性(基于表单字段)设置这些属性(基于这些字段的输入值),然后在 Web 服务中返回该类。
additionally, the web service will be able to set the properties in the class and eventually commit those changes to the db.
此外,Web 服务将能够在类中设置属性并最终将这些更改提交到数据库。
采纳答案by BinaryMisfit
I know this is being overly simplified by why not just KISS and generate the required Xml to return through the Web Service and then parse the returned Xml to populate the database.
我知道这被过度简化了,因为为什么不只是 KISS 并生成所需的 Xml 以通过 Web 服务返回,然后解析返回的 Xml 以填充数据库。
My reasoning is that for the expanded reason you suggest doing this I can see the value or reason for wanting a dynamic class?
我的推理是,出于您建议这样做的扩展原因,我可以看到想要动态类的价值或原因?
回答by Greg Beech
If you mean dynamically create a class, then the two options are:
如果你的意思是动态创建一个类,那么两个选项是:
- Reflection.Emit- Difficult, Fast to create the class
- CodeDom- Less Difficult, Slower to create the class
- Reflection.Emit- 创建类困难、快速
- CodeDom- 难度较低,创建类较慢
If you mean create an instance of an existing class, then start with Activator.CreateInstanceto create an instance of the object, and then look at the methods on Typesuch as GetPropertywhich will return a PropertyInfothat you can call GetValueand SetValueon.
如果您的意思是创建现有类的实例,那么从Activator.CreateInstance开始创建对象的实例,然后查看Type上的方法,例如GetProperty,它将返回一个PropertyInfo,您可以在其上调用GetValue和SetValue。
Update: For the scenario you describe, returning dynamic data from a web service, then I'd recommend against this approach as it's hard for you to code, and hard for statically-typed languages to consume. Instead, as suggested in the comments and one of the other answers, some sort of dictionary would likely be a better option.
更新:对于您描述的场景,从 Web 服务返回动态数据,然后我建议不要使用这种方法,因为您很难编码,并且静态类型语言很难使用。相反,正如评论和其他答案之一所建议的那样,某种字典可能是更好的选择。
(Note that when I say return some sort of dictionary, I am speaking figuratively rather than literally, i.e. return something which is conceptually the same as a dictionary such as a list of key-value pairs. I wouldn't recommend directly returning one (even if you're using WCF which does support this) because it's typically better to have full control over the XML you return.)
(请注意,当我说返回某种字典时,我是比喻性的而不是字面意思,即返回概念上与字典相同的内容,例如键值对列表。我不建议直接返回一个(即使您使用的是支持此功能的 WCF),因为通常最好完全控制您返回的 XML。)
回答by Noldorin
The Execution-Time Code Generationchapter of Eric Gunnerson's book (A Programmer's Introduction to C#) has some great information on this topic. See page 14 and onwards in particular. He outlines the two main methods of accomplishing dynamic class/code generation (CodeDOM and the Reflection.Emit namespace). It also discusses the difficulty and performance of the two approaches. Have a read through that, and you ought to find everything you might need.
Eric Gunnerson 的书(A Programmer's Introduction to C#)的Execution-Time Code Generation一章有一些关于这个主题的重要信息。特别参见第 14 页及以后。他概述了完成动态类/代码生成的两种主要方法(CodeDOM 和 Reflection.Emit 命名空间)。它还讨论了两种方法的难度和性能。通读一遍,你应该能找到你可能需要的一切。
回答by Denis Troller
The real question is, what do you need to use those properties for?
真正的问题是,您需要使用这些属性做什么?
What are gonna be the use cases? Do you need to bind those properties to the UI somehow? Using what kind of technology? (WPF, Windows Forms?)
将是什么用例?您是否需要以某种方式将这些属性绑定到 UI?使用什么样的技术?(WPF、Windows 窗体?)
Is it just that you need to gather a set of key/value pairs at runtime? Then maybe a simple dictionary would do the trick.
是否只是需要在运行时收集一组键/值对?那么也许一个简单的字典就可以解决问题。
Please elaborate if you can on what it is you need, and I'm sure people here can come up with plenty of ways to help you, but it's difficult to give a good answer without more context.
如果您可以详细说明您需要什么,我相信这里的人可以想出很多方法来帮助您,但是如果没有更多背景,很难给出一个好的答案。