I have a common code of serializing a class object in my 3-4 methods ,So I am thinking to create a common function for that code and call function in all the methods
I am doingn this from the following code
DataContractJsonSerializer ser = new DataContractJsonSerializer(this.GetType()); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, this); json = Encoding.Default.GetString(ms.ToArray()); ms.Close();
I want to put this common code in seprate general function which returns Json string and which accept whole class as input parameter as I am converting whole class into a Json object , I tried creating function like
public string GenerateJsonString(class C1)
but this is giving me error on the keyword ‘class’ saying that type is required
Can anyone tell me how can I accept whole class object in seprate method or function
You are confusing a ‘class’ with an ‘object’. You serialize an object, which is an instance of a particular class (aka ‘Type’).
You can create a method taking a parameter of the .NET base type for all objects, ‘object’, like this: