I have a class that stores the name of the WS method to call, as well as the type and value of the only parameter that the service receives (this will be a set of parameters, but let this make it easier for example):
public class MethodCall
{
public string Method { get; set; }
public Type ParType { get; set; }
public string ParValue { get; set; }
public T CastedValue<T>()
{
return (T)Convert.ChangeType(ParValue, ParType);
}
}
I have a method that takes a method name and parameters and uses reflection calls the method and returns the result. This works great when I use it like this:
callingclass.URL = url;
callingclass.Service = serviceName;
object[] Params = { (decimal)1 };
callingclass.CallMethod("Hello", Params);
But my type, decimal in the example, is specified in the MethodCall instance. Therefore, if I have this code:
MethodCall call = new MethodCall();
call.Method = "Hello";
call.ParType = typeof(decimal);
call.ParValue = "1";
Option 1 does not compile:
object[] Params = { (call.ParType)call.ParValue };
Option 2 does not compile any:
object[] Params = { call.CastedValue<call.ParType>() }; //Compilation error: Cannot implicitly convert type 'call.ParType' to 'object'
Option 3 using reflection compiles, but does not work when the service is called:
object[] Params = { typeof(MethodCall).GetMethod("CastedValue").MakeGenericMethod(call.ParType).Invoke(this, null) };
callingclass.CallMethod(call.Method, Params);
:
ConnectionLib.WsProxyParameterExeption: TestService.Hello URL- http://localhost/MyTestingService/ '.
- ?