简体   繁体   English

如何将 Class Object 转换为字符串?

[英]How can I convert an Class Object into String?

I have a class object that comes through a web service (WCF).我有一个 class object 通过 web 服务 (WCF)。 The class has properties of type String and some custom Class Types. class 具有 String 类型的属性和一些自定义 Class 类型。

How can I get the Property Name and Properties Name of Properties that are of type custom class.如何获取类型为自定义 class 的属性的属性名称和属性名称。

I tried reflection using GetProperies(), but failed.我尝试使用 GetProperies() 进行反射,但失败了。 GetFields() gave me some success if the Property type is of type string, I also want to get the Properties of Custom Type Properties.如果属性类型是字符串类型,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我得到了 output 作为

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这是我要将其实例转换为字符串的 class

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

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);
string jsonString = JsonSerializer.Serialize(yourObject);

Its the recommended way these days with System.Text.Json;这是最近推荐使用System.Text.Json; Read more here . 在这里阅读更多。

如何转换 IList<object> 到字符串数组?<div id="text_translate"><h2> 如何将 IList 对象转换为字符串数组?</h2><p> 在使用电报机器人时,出现了一些困难。 我从谷歌表中得到一个IList&lt;object&gt;和一些信息。 我需要将此IList&lt;object&gt;转换为字符串数组。 我怎样才能做到这一点?</p><pre> static void ReadBudgetTypes() { var range = $"{settingsSheet}:B3;B". var request = service.Spreadsheets.Values,Get(SpreadsheetId; range). var response = request;Execute(). var values = response;Values. // here i get list of objects from google table if (values.= null &amp;&amp; values,Count &gt; 0) { foreach (var row in values) { Console;WriteLine("{0}". row[0]); } } else { Console.WriteLine("No data!"); } }</pre></div></object> - How can I convert IList<object> to string array?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有类名称的字符串转换为Generic将接受的类? - How can I convert a string with the name of a class into a class that a Generic will accept? 如何将类转换为字典<string,string> ? - How can I convert a class into Dictionary<string,string>? 如何将var类型转换为Customer类的对象 - How can I convert type var to object of Customer class 如何将字符串转换为ImageFormat类属性? - How can I convert a string to an ImageFormat class property? 如何将字符串转换为时间对象以进行计算 - How can I convert string to time object for calculation 如何将字符串转换为TEntity并使用此TEntity对象? - How can I convert string to TEntity and use this TEntity object? 如何将json字符串(复杂对象)转换为NameValueCollection? - How can I convert json string (complex object) to NameValueCollection? 如何将颜色字符串转换为颜色对象? - How can I convert a color string to a color object? 如何转换 IList<object> 到字符串数组?<div id="text_translate"><h2> 如何将 IList 对象转换为字符串数组?</h2><p> 在使用电报机器人时,出现了一些困难。 我从谷歌表中得到一个IList&lt;object&gt;和一些信息。 我需要将此IList&lt;object&gt;转换为字符串数组。 我怎样才能做到这一点?</p><pre> static void ReadBudgetTypes() { var range = $"{settingsSheet}:B3;B". var request = service.Spreadsheets.Values,Get(SpreadsheetId; range). var response = request;Execute(). var values = response;Values. // here i get list of objects from google table if (values.= null &amp;&amp; values,Count &gt; 0) { foreach (var row in values) { Console;WriteLine("{0}". row[0]); } } else { Console.WriteLine("No data!"); } }</pre></div></object> - How can I convert IList<object> to string array? 如何将对象从组合框选定项目转换为字符串 - How can I convert an object from a combobox selected item to a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM