C#反思:发光类现有装配 [英] C# Reflection: Emitting classes to existing assemblies

查看:123
本文介绍了C#反思:发光类现有装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用Reflection.Emit的在现有装配创建类型,或者你需要定义一个新的动态组件能够包含动态类型?

Is it possible to use Reflection.Emit to create types in an existing assembly, or do you need to define a new dynamic assembly to be able to contain dynamic types?

基本上,我打算在一个XML定义文件中定义一个类,然后将其多次实例化,并与其他几个XML文件中的数据填充阅读。冲洗和重复在多个文件夹(每个都有不同的定义文件)。

Basically, I intend to read in one XML definition file which defines a class that is then instantiated multiple times and populated with the data from several other XML files. Rinse and repeat over several folders (each with a different definition file).

我也打算利用这些数据来动态构建的界面我的应用程序,以及定义如何重新保存数据到XML文件,当数据被格式化。

I also intend to use this data to dynamically build the interface to my app as well as define how the data is formatted when re-saving the data to the XML files.

推荐答案

在使用Reflection.Emit的,你必须总是创建一个新的装配。

When using Reflection.Emit, you must always create a new assembly.

然而,一个潜在的选择是对现有的装配,并定义为合同接口。您可以在使用Reflection.Emit的运行时创建新的装配,并将其定义实现您的接口类型。您的组件可使用工厂模式的实例属性,动态生成的类型和返回的实现您(在编译时已知)接口。

However, one potential option is to take your existing assembly, and define the contracts as interfaces. You can create the new assembly at runtime with Reflection.Emit, and have it define types that implement your interfaces. Your assembly can use a factory pattern to instantiate the property, dynamically generated type, and return an implementation of your (known at compile time) interface.

这篇关于C#反思:发光类现有装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆