Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an Class Object into String?

Tags:

I have a class object that comes through a web service (WCF). The class has properties of type String and some custom Class Types.

How can I get the Property Name and Properties Name of Properties that are of type custom class.

I tried reflection using GetProperies(), but failed. GetFields() gave me some success if the Property type is of type string, I also want to get the Properties of Custom Type Properties.

Here is my Code.

public static string ToClassString(this object value) {     if (value == null)         return null;     var builder = new StringBuilder();     builder.Append(value.GetType().Name + "{ ");     foreach (var prop in value.GetType().GetFields( BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty))     {         builder.Append("{ ");         builder.Append(prop.Name + " , ");         switch (prop.FieldType.Namespace)         {             case "System":                 builder.Append(prop.GetValue(value) + " }");                 break;             default:                 builder.Append(prop.GetValue(value).ToClassString() + " }");                 break;         }     }     builder.Append("}");     return builder.ToString(); } 

I got the output as

NotifyClass{ { UniqueId , 16175 }{ NodeInfo , NodeInfo{ } }{ EventType , SAPDELETE }}

Here is the class whose instance I want to convert into string

[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.MessageContractAttribute(WrapperName="NotifyReq", WrapperNamespace="wrapper:namespace", IsWrapped=true)] public partial class Notify {      [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=0)]     public int UniqueId;      [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=1)]     public eDMRMService.NodeInfo NodeInfo;      [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=2)]     public string EventType;      public Notify()     {     }      public Notify(int UniqueId, eDMRMService.NodeInfo NodeInfo, string EventType)     {         this.UniqueId = UniqueId;         this.NodeInfo = NodeInfo;         this.EventType = EventType;     } }         
like image 791
Kishore Kumar Avatar asked Apr 17 '13 12:04

Kishore Kumar


People also ask

How do you convert a class object to a string in Python?

Python is all about objects thus the objects can be directly converted into strings using methods like str() and repr(). Str() method is used for the conversion of all built-in objects into strings. Similarly, repr() method as part of object conversion method is also used to convert an object back to a string.

Can we convert object to string in Python?

Everything is an object in Python. So all the built-in objects can be converted to strings using the str() and repr() methods.

How do you turn an object into a string in JavaScript?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);


1 Answers

No need to reinvent the wheel. Use Json.Net

string s = JsonConvert.SerializeObject(yourObject); 

That is all.

You can also use JavaScriptSerializer

string s = new JavaScriptSerializer().Serialize(yourObject); 
like image 97
I4V Avatar answered Sep 18 '22 15:09

I4V