C# 反射 - 转换私有对象字段
我有以下类:
public class MyEventArgs : EventArgs
{
public object State;
public MyEventArgs (object state)
{
this.State = state;
}
}
public class MyClass
{
// ...
public List<string> ErrorMessages
{
get
{
return errorMessages;
}
}
}
当我引发事件时,我将 MyEventArgs 对象的“State”设置为 MyClass 类型的对象。我试图通过事件处理程序中的反射来检索 ErrorMessages:
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);
}
但这会将 errorMessagesInfo 返回为 null(即使 stateInfo 不为 null)。是否可以检索 ErrorMessages ?
编辑:我应该澄清事件处理程序位于不同的程序集中,并且我无法引用第一个程序集(包含 MyEventArgs 和 MyClass)来解决构建问题。
谢谢
编辑:解决方案
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要对此进行反射,只需将
EventArgs
转换为MyEventArgs
即可访问ErrorMessages
属性:您应该避免在其类型完全已知的情况下使用反射。相反,您应该使用类型转换将引用转换为您期望的类型。当类型信息是动态的或者在代码中编译时不可用时(事实似乎并非如此),反射就有意义。
You don't need reflection for this, just cast
EventArgs
toMyEventArgs
and then you can access theErrorMessages
property:You should avoid using reflection for something where they types are fully known. Instead, you should use type casting to convert the reference to the types you expect. Reflection makes sense when the type information is dynamic, or not available at compile time in your code (which doesn't appear to be the case).
一个例子....
An example....
您需要传递 MyClass 的实例:
如果发送者的类型为 MyClass,则意味着:
You need to pass the instance of MyClass:
If the sender is of type MyClass that would mean:
解决方案:
Solution: