Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
a.toraby Avatar asked Feb 05 '15 10:02

a.toraby


1 Answers

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.

like image 152
Selman Genç Avatar answered Sep 18 '22 21:09

Selman Genç