如何使用 C# 4.0 中的 AssemblyBuilder 生成虚拟属性?
我目前正在努力创建具有虚拟属性的程序集。 MSDN 上的示例仅创建普通属性。如何在具有虚拟属性的程序集中创建一个类?
我希望能够生成这样的类:
public class ClassA
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string ClassName { get; set; }
public virtual ClassB Partner { get; set; }
}
public class ClassB
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
PropertyBuilder 类没有 PropertyAttributes.Virtual,所以我不知道如何创建虚拟属性。如果我自己在 Visual Studio 中创建此类,然后在 Reflector 中打开它,属性它们本身是虚拟的,所以这是可能的。
怎么办呢?
I'm currently working on creating an Assembly with virtual properties. The examples on MSDN are only creating normal properties. How do I create a class inside an assembly which has virtual properties?
I would like to be able to generate a class like this:
public class ClassA
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string ClassName { get; set; }
public virtual ClassB Partner { get; set; }
}
public class ClassB
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
The PropertyBuilder class doesn't have PropertyAttributes.Virtual, so I don't know how to create a virtual property. If I create this class in Visual Studio myself, and then open it in Reflector, the properties themselfes are virtual, so it is possible.
How can it be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是在运行时创建一个类的示例,该类包含一个虚拟的整数属性
Id
:您在
中找不到与
是因为属性没有这个概念。方法可以是虚拟的,因此当您声明虚拟属性时,您就是在声明虚拟 getter 和 setter 方法。查看 MethodAttributes 枚举。virtual
相关的任何内容的原因PropertyBuilderHere's an example of creating a class at runtime that contains a single integer property
Id
which is virtual:The reason you don't find anything related to
virtual
in thePropertyBuilder
is because properties don't have this concept. Methods can be virtual, so when you declare a virtual property you are declaring virtual getter and setter methods. Take a look at MethodAttributes enumeration.