序列化在运行时创建的类
我从不同的客户端获得多个 XSD,并且我需要向他们提供符合他们提供的 XSD 的 XML 格式的数据。我已经编写了一段代码,使用 codedom、System.Reflection 和 codeprovider 从 XSD 动态创建和编译类。 现在我的计划是通过多个查询从数据库获取数据,并将字段映射到创建的动态类的属性并将其序列化。我正在寻找一种映射这些字段的通用方法,它可以用于任何类型的 xsd,并且只需映射字段即可序列化并提供 XML 文件。至于查询,我将它们放入配置文件中。通用解决方案可行吗?关于如何去做的任何想法或指示?
I get multiple XSDs from various clients and I need to give them the data in XML format conforming to the XSD that they have provided. I already have written a code to dynamically create and compile a class from XSD using codedom, System.Reflection and codeprovider.
Now my plan is to get data from database through multiple queries and map the fields to the properties of the dynamic class created and serialize it. I am looking for a generic way of mapping these fields, which can be used for any type of xsd and by just mapping the fields it will serialize and gives XML file. As for the queries I am putting them in the config file. Is a generic solution do-able? Any ideas or pointers on how to go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够解决这个问题。以下是我使用的步骤:我首先使用反射和具有可序列化属性的 codedom.compiler 从 xsd 创建内存中运行时程序集。使用反射,我在该程序集中创建了类的实例,并根据从数据库获取的数据分配了属性。我将这个类转发给另一个序列化方法,该方法接受一个对象并将其序列化为 xml。
就映射而言,我确保数据库列名称需要与 xml 属性名称匹配,这样我就能够映射和调用它们。
下面是代码片段:
然后将 myClass 对象传递给接受对象类型的序列化方法,仅此而已
为此,现在我计划添加一个 UI 部分,其中“ForexRates”等映射保存在数据库中,然后它将向任何对象打开xsd 类型。
非常感谢您的回复。
I was able to resolve this. Here are the steps I used: I first created an in-memory run time assembly from xsd using reflection and codedom.compiler with serializable attributes. Using the reflection I created instance of the classes in that assembly and assigned properties from the data I got from database. This class I forwarded to another method that serializes takes an object and serializes it to xml.
As far as the mapping, I made sure that the database column names needs to be matched with the xml attribute names and with that I was able map and invoke them.
Here is the snippet:
Then pass myClass object to serialize method that accepts object type and that's it
For this, now I am planning to add a UI piece where the mappings like "ForexRates" are saved in the database and it would then open to any of the xsd types.
Thanks very much for you responses.
您无法序列化即时创建的类——至少不能以正常方式序列化。即使您找到了附加
Serializable
属性的方法,它也不会产生任何效果:不会生成序列化程序集。从理论上讲,您可以发出一个全新的类并在运行时生成它,但这绝对是一个痛苦。如果您要这样做,我会推荐使用 Mono.Cecil,而不是 .NET Framework 的默认 IL 发射器。
You can't serialize classes that are created on-the-fly--at least, not the normal way. Even if you found a way to attach the
Serializable
attribute, it wouldn't have any effect: the serialization assembly wouldn't be generated.You could, theoretically, emit an entirely new class and sgen it at runtime, but that would be an absolute pain. If you were to do that, I'd recommend Mono.Cecil over the .NET Framework's default IL emitter.