Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 13th, 2007, 02:33 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deserialize from method information in .NET C#

I am trying to get around AJAX being disabled for our customers in Internet Explorer 6. I can override the JavaScript events and pass the serialized objects to the server via an iframe. This is working fine.

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<Person>(opInput);

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(zpService).GetPa rameters();
int lpParamCount = opParamInfo.Length;

Dictionary<string, object> opDictionary;
opDictionary = (Dictionary<string, object>)opSerializer.DeserializeObject(zpData);

Object[] opObjects = new Object[opDictionary.Count];
int lpLoop = 0;

KeyValuePair<string,object> opKVP;

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(zpService).Invok e(opDataService, opObjects);
 
Old September 13th, 2007, 09:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is the "javascript object" actually an object from JS? Or is it a serialized .NET object?

In the case of opSerializer.Deserialize<Person>(opInput) the type parameter has to be a type, it can't be a parameter.

Is there a problem with the code you have posted or are you looking for an alternative to some of what you have there?

I suspect that if you are trying to translate a JS object into .NET you don't have much choice but to test each property by name. The JS object doesn't have strongly typed properties so you can't test by type. (Unless I'm missing something here.)

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
NextRecordset method in ADO.NET? Clay ADO.NET 6 April 20th, 2005 07:54 AM
ASP.NET loses / drops Session State information Quickdraw General .NET 0 February 8th, 2005 03:09 PM
ASP.NET loses / drops Session State information Quickdraw VS.NET 2002/2003 1 February 8th, 2005 03:08 PM
where is the equivalent method of SetROP2 in .net williamzhou VS.NET 2002/2003 0 September 9th, 2004 08:44 PM
How to call a C# method from VB.Net? aliarifpk General .NET 1 February 26th, 2004 04:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.