Reputation: 23973
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; }
}
Upvotes: 2
Views: 1048
Reputation: 110
Use BaseType.
public static void Process(TSource source)
{
foreach (PropertyInfo p in target.GetType().BaseType.GetProperties(flags))
{
object[] attr = p.GetCustomAttributes(true);
}
}
Upvotes: 1
Reputation: 23973
OK, this one was obvious after a closer look;
System.Data.Entity.DynamicProxies.Login_A2947F53...
is a dynamicProxy type and know nothing about any Attributes. So I have to use the something like:
foreach (PropertyInfo p in typeof(Login).GetProperties(flags))
instead of the dynamicProxy instance to get the type from. And finaly there are my Attributes.
Upvotes: 1