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 ?
Edit: I should clarify that the event handler is in a different assembly, and I cannot reference the first assembly (that contains MyEventArgs and MyClass) for build issues.
Thank you
Edit: Solution
FieldInfo stateInfo = args.GetType().GetField("State");
Object myClassObj = stateInfo.GetValue(args);
PropertyInfo errorMessagesInfo = myClassObj.GetType().GetProperty("ErrorMessages");
object errorMessagesObj = errorMessagesInfo.GetValue(myClassObj, null);
IList errorMessages = errorMessagesObj as IList;