SDK / API Support
cancel

System.Reflection.TargetException: Non-static method requires a target

Synercon_
Contributor.

System.Reflection.TargetException: Non-static method requires a target

Hi there,

At one of our client's location we are encountering following error when calling the method in HP.HPTRIM.SDK.TrimMainObjectSearch.

System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)


Following is the code through which I am calling the procedure

object objBaseObjectType = GetBaseObject("Classification");

Type trimMainObjectSearch = asmSDK.GetType("HP.HPTRIM.SDK.TrimMainObjectSearch");
object objInstance = Activator.CreateInstance(trimMainObjectSearch, new object[] { TRIMDatabase, objBaseObjectType });

IEnumerable objSearch = (IEnumerable)objInstance;
Type trimMainObjectSearch = asmSDK.GetType("HP.HPTRIM.SDK.TrimMainObjectSearch");

MethodInfo methodInfo = null;
methodInfo = trimMainObjectSearch.GetMethod("SelectTopLevels");
methodInfo.Invoke(objSearch, null);

Any help in regards to this will be appriciated. Let me know if you need more information in regards to this

Thanks

5 REPLIES
dchurchland
Acclaimed Contributor.

Re: System.Reflection.TargetException: Non-static method requires a target

My first question is what the point of all the reflection is?

Next thing, you instantiate the variable 'trimMainObjectSearch' twice so the code will not compile.

Also, the constructor for TrimMainObjectSearch takes a Database and the BaseObjectTypes enum, not an instance of a TrimMainObject (as you seem to be doing with GetBaseObject("Classification")).

I am on holidays soon but if you post a complete (but short) compiling sample preferrably accompanied by a summary of what you are actually trying to achieve I might have time to have a look.

 

 


Blog | Samples | HPE CM 9.1 SDK Docs
**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of Hewlett Packard Enterprise**
Synercon_
Contributor.

Re: System.Reflection.TargetException: Non-static method requires a target

Hi dchurchland

Thanks for your prompt response.

I am using reflection in order to cater to multiple versions of SDK as our clients uses different versions of Records Manager.

Sorry for the confusion in regards to the code as I realised that I posted some wrong code statements in it as they are comming from different procedures in my actual code. The actual code looks similar as below:

public Assembly asmSDK;

asmSDK = Assembly.LoadFrom(<Path of HP.HPTRIM.SDK.dll>);

Type bot = asmSDK.GetType("HP.HPTRIM.SDK.BaseObjectTypes");
object objBaseObjectType = Enum.Parse(bot, "Classification");

Type trimMainObjectSearch = asmSDK.GetType("HP.HPTRIM.SDK.TrimMainObjectSearch");
object objtrimMainObjectSearch = Activator.CreateInstance(trimMainObjectSearch, new object[] { TRIMDatabase, objBaseObjectType }); // TRIMDatabase is the object created by calling the SelectDatabase method from HP.HPTRIM.SDK.DesktopHelper


IEnumerable objSearch = (IEnumerable)objtrimMainObjectSearch;
MethodInfo methodInfo = null;
methodInfo = trimMainObjectSearch.GetMethod("SelectTopLevels");
methodInfo.Invoke(objSearch, null);

Regards

 

Mq___
Trusted Contributor.

Re: System.Reflection.TargetException: Non-static method requires a target

Hi Synercon,

I have tested a version of the code that you have supplied against the current codebase and the latest versions of 8.3, 8.2 and 8.1.  In each case the code has worked as expected.  I have attached the code as it is a little verbose (don't forget to alter the binaries path and DBid if you run it)...

I note that in the snip you provide there are no checks for null objects.  You will receive the subject error if objSearch is null:

methodInfo.Invoke(objSearch, null);

If that's not your issue can you identify which versions of RM it occurs with?

Either way, as a point of interest, do you know the reason the customer is using more than one version of RM?

 

 

**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of Hewlett Packard Enterprise**
Highlighted
Synercon_
Contributor.

Re: System.Reflection.TargetException: Non-static method requires a target

Hi Mq___

Thanks for your help and sorry for not responding early.

According to your reply, if objSearch object is null then it will generate that error. But I am not able to find out at what point it becomes null. I tested the code on my end with my client's sample data and it runs fine. The logs at client side shows that as soon as it tries to create the first top level term, it generates the error then it keeps occuring for each term.

Thanks

Mq___
Trusted Contributor.

Re: System.Reflection.TargetException: Non-static method requires a target


The logs at client side shows that as soon as it tries to create the first top level term, it generates the error then it keeps occuring for each term.

To which logs are you referring and what is 'it'?  

I'm not sure what you mean when you say that "it generates the error (when it 'creates' the first top level term) and then it keeps occuring for each term?  The SDK doesn't use reflection so it won't be throwing this error internally and for it to happen for each term implies some kind of iteration which is not present in the code sample you provided.

Which line of the sample provided causes the error?

**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of Hewlett Packard Enterprise**