如何将类对象转换为字符串? [英] How can I convert an Class Object into String?

查看:87
本文介绍了如何将类对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 Web 服务 (WCF) 提供的类对象.该类具有 String 类型的属性和一些自定义类类型.

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.

我尝试使用 GetProperies() 进行反射,但失败了.如果属性类型是字符串类型,GetFields() 给了我一些成功,我也想获取自定义类型属性的属性.

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.

这是我的代码.

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();
}

我得到的输出为

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

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;
    }
}        

推荐答案

无需重新发明轮子.使用 Json.Net

No need to reinvent the wheel. Use Json.Net

string s = JsonConvert.SerializeObject(yourObject);

仅此而已.

您也可以使用 JavaScriptSerializer

You can also use JavaScriptSerializer

string s = new JavaScriptSerializer().Serialize(yourObject);

这篇关于如何将类对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆