简体   繁体   中英

What does System.Reflection.TargetException: Non-static method requires a target. mean?

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);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM