到POCO类添加属性在运行时 [英] Add property to POCO class at runtime

查看:196
本文介绍了到POCO类添加属性在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择 ServiceStack OrmLite 为我的项目,这是一个纯粹的面向数据的应用程序。我愿意允许最终用户创建的,将被用来生成使用的的CodeDOM

I selected ServiceStack OrmLite for my project which is a pure Data-Oriented application. I am willing to allow the end user to create his own Object Types defined in an XML format that will be used to generate classes at runtime using CodeDOM.

我也将确定一些所需的系统对象的应用程序(即用户),但我不能预见所有的最终用户会使用,所以我在寻找一种方式,让我扩大在设计时创建类的属性。样品波纹管

I will be also defining some "system" objects required by the application (i.e. User) but I cannot foresee all the properties the end user will use and therefore I am looking for a way to allow extending the classes I create in design time. Sample bellow

public class User
{
    public Guid Uid { get; set; }
    public String Username { get; set; }
    public String Password { get; set; }
}



最终用户希望有一个电子邮件地址。他应该能在2属性添加到上层阶级和全班将(仍然可以通过OrmLite使用,因为它允许覆盖:

The end user wants to have an Email and an Address. He should be able to add the 2 properties to the upper class and the whole class will be (which still can be used by OrmLite, since it allows overwriting :

public class User
{
    public Guid Uid { get; set; }
    public String Username { get; set; }
    public String Password { get; set; }
    public String Email{ get; set; }
    public String Address { get; set; }
}

我知道有可能这样做,以使系统崩溃(如果类已经被实例化)的风险,所以我在寻找,以避免此问题的最佳途径,模仿需要我。

I know that there might be a risk of doing so to crash the system (if the class is already instantiated) so I am looking for the best way to avoid this issue and mimic the need I have.

推荐答案

似乎有两个部分你在做什么在这里,你需要创建种动态来支持额外的属性。你还需要确保不会有重复的类型在你的AppDomain,即两个用户

It seems that there are two parts to what you're doing here. You need to create types dynamically to support the additional properties. You also need to ensure that you never end up with duplicate types in your AppDomain, i.e. two different definitions of User.

提出的各种建议已经给手柄如何创建类型。在一个项目中,我们有类似的东西。我们创建了一个基类,有核心性能和存储'扩展'性质的字典。然后,我们使用 Reflection.Emit的来创建一个派生类型具有良好的特性。每个属性的定义简单地读或写在基类的字典。由于 Reflection.Emit的限嗣继承写低水平的IL代码,它起初看似复杂。我们在其他类库中写了一些样品派生类和编译他们。这些是什么,我们会实际需要,实现在运行时的例子。然后,我们使用程序Ildasm.exe看到编译器产生什么代码。这使得它很容易找出我们如何能够在运行时生成相同的代码。

The various suggestions already given handle how to create the types. In one project, we had something similar. We created a base class that had the core properties and a dictionary to store the 'extension' properties. Then we used Reflection.Emit to create a derived type that had the desired properties. Each property definition simply read from or wrote to the dictionary in the base class. Since Reflection.Emit entails writing low-level IL code, it seems complex at first. We wrote some sample derived classes in another class library and compiled them. These were examples of what we'd actually need to achieve at runtime. Then we used ildasm.exe to see what code the compiler produced. This made it quite easy to work out how we could generate the same code at runtime.

你的第二个挑战是避免有重复的类型名称。我们追加一个GUID(含无效字符去掉),以每个生成类型的名称,以确保这从来没有发生过。简单的办法,虽然我不知道你是否能与你的ORM脱身。

Your second challenge is to avoid having duplicate type names. We appended a guid (with invalid characters removed) to the name of each generated type to make sure this never happened. Easy fix, though I don't know whether you could get away with that with your ORM.

如果这是服务器代码,您还需要考虑的是,组件在.NET永远不会卸载。因此,如果你反复在运行时产生新的类型,你的进程将继续增长。同样将在客户端代码中发生的,但如果你不希望为长时间运行过程中,这可能是问题不大。

If this is server code, you also need to consider the fact that assemblies are never unloaded in .NET. So if you're repeatedly generating new types at runtime, your process will continue to grow. The same will happen in client code, but this may be less of an issue if you don't expect the process to run for an extended period of time.

我说组件不卸载;但是,您可以卸载整个的AppDomain 。因此,如果这是服务器代码,你可以在自己的应用程序域整个操作来看,那么它拆掉后,保证了动态创建类型卸载。

I said assemblies are not unloaded; however, you can unload an entire AppDomain. So if this is server code you could have the entire operation run in its own appdomain, then tear it down afterwards to ensure that the dynamically created types are unloaded.

这篇关于到POCO类添加属性在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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