I'm trying to set the IsReadOnly
property via reflection but GetField()
keep returning null. I have no idea why.
Here's my code:
class Foo
{
[ReadOnly(true)]
public int x { get; set; }
public int y { get; set; }
public void lol()
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["x"];
ReadOnlyAttribute attr = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
attr.Dump();
attr.GetType().GetField("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance).Dump();
}
}
output (on LINQPAD):
Why is this returning null if the property is in there (as shown in the image)?
typeof(ReadOnlyAttribute).GetField("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance)
doesn't works either.
Aucun commentaire:
Enregistrer un commentaire