C# 如何将类对象转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16060745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:52:45  来源:igfitidea点击:

How can I convert an Class Object into String?

c#.netwcfreflection

提问by Kishore Kumar

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

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

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.

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

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

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

采纳答案by I4V

No need to reinvent the wheel. Use Json.Net

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

string s = JsonConvert.SerializeObject(yourObject);

That is all.

就这些。

You can also use JavaScriptSerializer

你也可以使用 JavaScriptSerializer

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