C# Reflection - Casting private Object field
Posted
by alhazen
on Stack Overflow
See other posts from Stack Overflow
or by alhazen
Published on 2010-03-30T22:09:58Z
Indexed on
2010/03/30
22:13 UTC
Read the original article
Hit count: 339
I have the following classes:
public class MyEventArgs : EventArgs
{
public object State;
public MyEventArgs (object state)
{
this.State = state;
}
}
public class MyClass
{
// ...
public List<string> ErrorMessages
{
get
{
return errorMessages;
}
}
}
When I raise my event, I set 'State' of the MyEventArgs object to an object of type MyClass. I'm trying to retrieve ErrorMessages by reflection in my event handler:
public static void OnEventEnded(object sender, EventArgs args)
{
Type type = args.GetType();
FieldInfo stateInfo = type.GetField("State");
PropertyInfo errorMessagesInfo = stateInfo.FieldType.GetProperty("ErrorMessages");
object errorMessages = errorMessagesInfo.GetValue(null, null);
}
But this returns errorMessagesInfo as null (even though stateInfo is not null). Is it possible to retrieve ErrorMessages ?
Thank you
© Stack Overflow or respective owner