35

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;
    }
}        
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
  • 1
    How is the output different from what you expect? is the problem that it doesn't include `NodeInfo`'s members? if so: what does `NotInfo` look like? are the members `public`? is it a public type? etc – Marc Gravell Apr 17 '13 at 13:02

2 Answers2

101

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);
I4V
  • 34,891
  • 6
  • 67
  • 79
  • I dont want to use external assemblies – Kishore Kumar Apr 17 '13 at 13:06
  • 8
    JavaScriptSerializer isn't an external assembly. – Christian Hayter Apr 17 '13 at 13:09
  • 4
    @TheIndianProgrammmer `JavaScriptSerializer` is built-in (`System.Web.Script.Serialization`) http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.100).aspx – I4V Apr 17 '13 at 13:09
  • 1
    Note that you need to add a reference to `System.Web.Extensions` to be able to use `System.Web.Script.Serialization` that has the `JavaScriptSerializer` class. – Beyondo Jul 29 '19 at 06:46
2
string jsonString = JsonSerializer.Serialize(yourObject);

Its the recommended way these days with System.Text.Json; Read more here.

Xserge
  • 199
  • 2
  • 9