Question

I have a webservice controller that had public static properties, which are webservices. I use reflection to invoke methods on those webservices.

When all webservice properties were static, there was no problem at all. This worked good to invoke a method in the webservicecontroller using reflection:

Type wsControllerType = typeof(wsController);
PropertyInfo WebserviceProperty = wsControllerType.GetProperty(wsName, Some bindingflags);
MethodInfo method = WebserviceProperty.PropertyType.GetMethod(methodname);
method.Invoke(WebserviceProperty.GetValue(null, null), parameters);

The webservicecontroller is converted to a singleton, so the static webservice properties are removed.

When I try to call a method through reflection now, I get a TargetException on the last part (WebserviceController.GetValue(null, null).

I tried multiple things, but can't get it to work. I think i'm missing something simple.. Who can help?

Was it helpful?

Solution

In order to invoke an instance property via reflection you will need to provide an instance to work on. The post mentioned that WebServiceProperty had been turned into a singleton so just use the singleton value

WebserviceProperty.GetValue(wsController.GetTheSingleton(), null)

Although I must say that it seems a bit odd to even do reflection here. You have access to the wsController type hence you should have the ability to bind statically to all of its members. Why not just use normal non-reflection here?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top