I now want to Deserialize the JavaScript object, and pass these parameters to a .NET method.
I have the name of the method as a string, and the JavaScript object.
Using Reflection I want to loop through the parameters of the method and Deserialize each JavaScript object to the correct type.
For example I want to achieve this:
Person opPerson = opSerializer.Deserialize
but where Person is the data type of a parameter of a method, i.e. replace Person with a variable and please don't suggest a large switch statement!
The following example works for methods with multiple int and string parameters:
string zpService = Request["Service"].ToString();
string zpData = Request["_data"].ToString();
string[] zpDataSplit = zpData.Split(',');
string[] zpItemSplit;
DataService opDataService; // Data Service is a Web Service Class
opDataService = new DataService();
JavaScriptSerializer opSerializer = new JavaScriptSerializer();
ParameterCollection opParamCollection = new ParameterCollection();
Parameter opParam;
ParameterInfo[] opParamInfo = opDataService.GetType().GetMethod(zpServ
int lpParamCount = opParamInfo.Length;
Dictionary
opDictionary = (Dictionary
Object[] opObjects = new Object[opDictionary.Count];
int lpLoop = 0;
KeyValuePair
foreach (ParameterInfo opInfo in opParamInfo)
{
opKVP = opDictionary[opInfo.Name];
switch (opInfo.ParameterType.FullName)
{
case "System.String":
opObjects[lpLoop] = opKVP.Value.ToString();
break;
default:
opObjects[lpLoop] = opKvp.Value;
break;
}
lpLoop ++;
}
opDataService.GetType().GetMethod(zpServ
Replies
Know the answer? Post it — somebody with the same question will find it here.