如何在 DynamicProxy 类中使用自定义属性
我尝试将自定义属性分配给来自动态代理的
System.Data.Entity.DynamicProxies.Login_A2947F53...
类示例类登录
public partial class Login
{
[CustomAttribute]
public virtual int Id
{
get;
set;
}
}
现在我尝试使用泛型和反射访问属性,
public static void Process(TSource source)
{
foreach (PropertyInfo p in target.GetType().GetProperties(flags))
{
object[] attr = p.GetCustomAttributes(true); // <- empty
}
}
但没有属性。这是由于 DynmaicProxy 还是我在这里做错了什么?
当我使用没有像这样的动态代理的具体类时,我会得到属性。
public class TestObject
{
[CustomAttribute]
public virtual string Name { get; set; }
[CustomAttribute]
public virtual string Street { get; set; }
public virtual int Age { get; set; }
public virtual string Something { get; set; }
}
I tried to assign a custom Attribute to class that comes from a dynamic proxy
System.Data.Entity.DynamicProxies.Login_A2947F53...
Example class Login
public partial class Login
{
[CustomAttribute]
public virtual int Id
{
get;
set;
}
}
Now I try to access the Attribute using Generics and Reflection
public static void Process(TSource source)
{
foreach (PropertyInfo p in target.GetType().GetProperties(flags))
{
object[] attr = p.GetCustomAttributes(true); // <- empty
}
}
But there is no Attribute. Is that due to the DynmaicProxy or what did I do wrong here?
When I use a concrete class without dynamic proxy like this one, then I get the attributes.
public class TestObject
{
[CustomAttribute]
public virtual string Name { get; set; }
[CustomAttribute]
public virtual string Street { get; set; }
public virtual int Age { get; set; }
public virtual string Something { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这个仔细一看就很明显了;
是一个动态代理类型并且对任何属性一无所知。所以我必须使用类似的东西:
而不是dynamicProxy实例来获取类型。最后是我的属性。
OK, this one was obvious after a closer look;
is a dynamicProxy type and know nothing about any Attributes. So I have to use the something like:
instead of the dynamicProxy instance to get the type from. And finaly there are my Attributes.
使用基本类型。
Use BaseType.