Reputation: 3391
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.
Upvotes: 12
Views: 48747
Reputation: 4246
Another reason for this error is that when you read a value from an instance object and the instance object you read from is null
.
Upvotes: 3
Reputation: 101731
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.
Upvotes: 17