System.Reflection.TargetException: 非静态方法需要一个目标.意思? [英] What does System.Reflection.TargetException: Non-static method requires a target. mean?

查看:48
本文介绍了System.Reflection.TargetException: 非静态方法需要一个目标.意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我从某处接收到 functionCode 值并需要反映适当的类.我试图根据 this 解决方案反映适当的类型.但这对我不起作用.我不能使用 GetField() 方法,因为我正在处理一个 PCL 项目.因此我尝试了这些代码行:

In my application I receive the functionCode value from somewhere and need to reflect the appropriate class. I tried to reflect the appropriate type According to this solution. but it doesn't work for me. I can not use GetField() method because I'm working on a PCL project. Therefore I tried these lines of code:

AssemblyName name = new AssemblyName("MyLibrary");
var type = Assembly.Load(name);
type.DefinedTypes.FirstOrDefault(x =>
x.GetDeclaredProperty("functionCode") != null &&
 (byte)x.GetDeclaredProperty("functionCode").GetValue(null) == val);

它也不起作用.它抛出 System.Reflection.TargetException: Non-static method requires a target.

It does not work too. It throws System.Reflection.TargetException: Non-static method requires a target.

推荐答案

这意味着非静态方法需要一个对象.如果您有一个实例成员,那么您必须使用一个实例来获取它的值.因为没有实例它不存在.所以你需要将类型的实例而不是 null 传递给 GetValue 方法.或者使成员 static 如果您不希望它成为实例成员.

It means non static method requires an object. If you have an instance member then you have to use an instance to get it's value. Because without an instance it doesn't exist.So you need to pass an instance of the type instead of null to GetValue method.Or make the member static if you don't want it to be an instance member.

这篇关于System.Reflection.TargetException: 非静态方法需要一个目标.意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆