有没有针对一个复杂类的快速自动ToString()方法?

yuzhouhuo2010 2015-06-21 09:50:24
想实现对一个类中字段数据的描述,我从网上查阅了资料,自己实现了一个方法,能够对一个类直接实现描述。

class xxx
{
public int ma = 20;
private int mb = 100;
private string sa = "abc";
private string sb = "33333";
private string sc = "ewr";
}

// 通过反射的形式获得一个对象的所有字段名字和值的描述集合
public static string MSreflection<T>(T type, int ISeveryHH)
{
Type ty = type.GetType();
FieldInfo[] fiel = ty.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

string sall = "";
string HH = ISeveryHH == 1 ? Environment.NewLine : "";
foreach (FieldInfo fil in fiel)
{
object onow = fil.GetValue(type);
string sone = onow != null ? onow.ToString() : "NULL";
sall += "[" + fil.Name + "=" + sone + "] " + HH;
}
return sall;
}


然后我调用这个方法

xxx mx = new xxx();
MSreflection<xxx>(mx, 1);


得到了结果
[ma=20]
[mb=100]
[sa=abc]
[sb=33333]
[sc=ewr]

感觉很方便,但是如果类中包含了更复杂的数据字段呢,比如


class test
{
Dictionary<int, int> dary = new Dictionary<int, int>();
List<List<int>> sary = new List<List<int>>();
List<int> nary = new List<int>();
xxx mx = new xxx();
wori wr = new wori();
Tuple<Type, List<int>, string, int, long> tup = new Tuple<Type, List<int>, string, int, long>(typeof(int),null, null, 0, 1);
}



然后我调用这个方法

test ttt = new test();
MSreflection<test>(ttt, 1);

得到结果是:
[dary=System.Collections.Generic.Dictionary`2[System.Int32,System.Int32]]
[sary=System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.Int32]]]
[nary=System.Collections.Generic.List`1[System.Int32]]
[mx=yuzhouJuxiao.ZZtest+xxx]
[wr=yuzhouJuxiao.ZZtest+wori]
[tup=(System.Int32, , , 0, 1)]

这不是我想要的结果,我希望描述的方法能够自动识别其是否是List、Dictionary,Tuple并且对它们的每一个值进行描述,具体该怎么做呢? NET组件里面是否自带了相应的描述方法呢?.NET通过序列或反序列可以将一个对象直接保存,那么
...全文
472 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
SileT120 2015-06-30
  • 打赏
  • 举报
回复
引用 1 楼 yuzhouhuo2010的回复:
.NET通过序列或反序列可以将一个对象直接保存,那么肯定是具有这种能力的。但是我们如何能够运用实现描述呢?还望各位大哥大姐指点下啊!
试试Google protobuffer,很牛的序列化和反序列化工具
暈哥 2015-06-28
  • 打赏
  • 举报
回复
楼主是要这种效果吗? 2014-06-25 15:14:28.5564 | Form.OnLoad => MainForm.MainForm_Load => ThriftService.StartRequestService ==> Starting request service on port 21021 ... 2014-06-25 15:14:37.1339 | StackBuilderSink.AsyncProcessMessage => StackBuilderSink._PrivateProcessMessage => <>c__DisplayClass1.<ServiceTest>b__0 ==> NSIotServiceRequestMsg.SeqId: 6 NSIotServiceRequestMsg.EnumServiceRequestType: ServiceStart NSIotServiceRequestMsg.EnumServiceType: FtpService NSIotFtpServiceRequestMsg.ServerIp: 172.29.40.153 NSIotFtpServiceRequestMsg.ServerPort: 21 NSIotFtpServiceRequestMsg.ConnectTryTimes: 3 NSIotFtpServiceRequestMsg.ConnectTryInterval: 30 NSIotFtpServiceRequestMsg.EnumServiceDataFlowType: Download NSIotFtpServiceRequestMsg.ServiceLastTime: 180 NSIotFtpServiceRequestMsg.MaxULThroughput: -1 NSIotFtpServiceRequestMsg.MaxDLThroughput: -1 2014-06-25 15:14:37.9776 | ReplyManager.ServiceConfirm => ThriftClient.Response => ThriftClient.Connect ==> Response connected smt on by1-24775-b01:21020. 2014-06-25 15:14:37.9776 | <>c__DisplayClass1.<ServiceTest>b__0 => ReplyManager.ServiceConfirm => ThriftClient.Response ==> NSIotServiceResponseMsg.SeqId: 6 NSIotServiceResponseMsg.EnumServiceResponseType: ServiceConfirm NSIotServiceConfirmResponseMsg.Result: Ok NSIotServiceConfirmResponseMsg.ErrorMessage: 2014-06-25 15:14:40.2275 | StackBuilderSink.AsyncProcessMessage => StackBuilderSink._PrivateProcessMessage => <>c__DisplayClass5.<ServiceStatics>b__3 ==> NSIotServiceStatisticsRequestMsg.SeqId: 6 NSIotServiceStatisticsRequestMsg.EnumServiceUnitMark: Second NSIotServiceStatisticsRequestMsg.StaticsReportStart: 0 NSIotServiceStatisticsRequestMsg.StaticsReportEnd: 10 NSIotServiceStatisticsRequestMsg.StaticsReportType: 512 2014-06-25 15:14:47.8208 | <>c__DisplayClass5.<ServiceStatics>b__4 => ThriftClient.Response => ThriftClient.Connect ==> Response connected smt on by1-24775-b01:21020. 2014-06-25 15:14:47.8208 | <>c__DisplayClassd.<InvokeReport>b__c => <>c__DisplayClass5.<ServiceStatics>b__4 => ThriftClient.Response ==> NSIotServiceResponseMsg.SeqId: 6 NSIotServiceResponseMsg.EnumServiceResponseType: ServiceStatics NSIotServiceStaticsResponseMsg.StaticsReportType: 512 NSIotServiceStaticsResponseMsg.RealTimePelr: 0 NSIotServiceStaticsResponseMsg.AveragePelr: 0 NSIotServiceStaticsResponseMsg.MaxPD: 0 NSIotServiceStaticsResponseMsg.MinPD: 0 NSIotServiceStaticsResponseMsg.RealTimePD: 0 NSIotServiceStaticsResponseMsg.AveragePD: 0 NSIotServiceStaticsResponseMsg.MaxULThroughput: 0 NSIotServiceStaticsResponseMsg.MaxDLThroughput: 23824280 NSIotServiceStaticsResponseMsg.AverageULThroughput: 0 NSIotServiceStaticsResponseMsg.AverageDLThroughput: 23644253 NSIotServiceStaticsResponseMsg.TotalACK: 0 NSIotServiceStaticsResponseMsg.TotalNACK_DTX: 0 2014-06-25 15:14:48.1801 | StackBuilderSink.AsyncProcessMessage => StackBuilderSink._PrivateProcessMessage => <>c__DisplayClass1.<ServiceTest>b__0 ==> NSIotServiceRequestMsg.SeqId: SMT Active Request,CASEFINISHED,TD-LTE,TC_6_1_1_3,Pass NSIotServiceRequestMsg.EnumServiceRequestType: ServiceStopAll NSIotServiceRequestMsg.EnumServiceType: None
yuzhouhuo2010 2015-06-28
  • 打赏
  • 举报
回复
引用 18 楼 sp1234 的回复:
[quote=引用 11 楼 wjlsmail 的回复:] Ref: http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 通过序列化将.net对象转换为JSON字符串 在web开发过程中,我们经常需要将从数据库中查询到的数据(一般为一个集合,列表或数组等)转换为JSON格式字符串传回客户端,这就需要进行序列化,这里用到的是JsonConvert对象的SerializeObject方法。其语法格式为:JsonConvert.SerializeObject(object),代码中的”object”就是要序列化的.net对象,序列化后返回的是json字符串。
关键点其实也不仅仅是序列化,而是序列化后的格式。JObject.ToString()输出的格式很易读,而JsonConvert.SerializeObject ()输出的格式则不易读只易于传输。[/quote] 感谢SP大神的耐心讲解,虽然我根据自己的需求不会使用JSON,但是通过这些交流,我对JSON已经有了较为基础的认识,以后有这方面需要也能跟快速地去思考了,真是谢谢啊!
  • 打赏
  • 举报
回复
引用 11 楼 wjlsmail 的回复:
Ref: http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 通过序列化将.net对象转换为JSON字符串 在web开发过程中,我们经常需要将从数据库中查询到的数据(一般为一个集合,列表或数组等)转换为JSON格式字符串传回客户端,这就需要进行序列化,这里用到的是JsonConvert对象的SerializeObject方法。其语法格式为:JsonConvert.SerializeObject(object),代码中的”object”就是要序列化的.net对象,序列化后返回的是json字符串。
关键点其实也不仅仅是序列化,而是序列化后的格式。JObject.ToString()输出的格式很易读,而JsonConvert.SerializeObject ()输出的格式则不易读只易于传输。
yuzhouhuo2010 2015-06-27
  • 打赏
  • 举报
回复
谢谢各位前辈的指点,我测试了json的效果,感觉不是很符合自己的需要,9楼caozhy和16楼WM_JAWIN给出的意见很好,我自己来分析处理数据可能更符合自己的需要!
WM_JAWIN 2015-06-25
  • 打赏
  • 举报
回复
就用你那玩艺,根据 onow 类型,再处理就OK了 if(onow is IEnumable) //针对IEnumable在这里处理掉 else if (onow is .....
  • 打赏
  • 举报
回复
使用序列化和反序列化方法就可以,直接序列化json
software_artisan 2015-06-25
  • 打赏
  • 举报
回复
这个难道不是序列化为json就行了嘛???
showjim 2015-06-24
  • 打赏
  • 举报
回复
Json不是挺符合需求的吗?
smthgdin_020 2015-06-24
  • 打赏
  • 举报
回复
可以是使用序列化和反序列化。
wjlsmail 2015-06-24
  • 打赏
  • 举报
回复
Ref:
http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html

通过序列化将.net对象转换为JSON字符串

在web开发过程中,我们经常需要将从数据库中查询到的数据(一般为一个集合,列表或数组等)转换为JSON格式字符串传回客户端,这就需要进行序列化,这里用到的是JsonConvert对象的SerializeObject方法。其语法格式为:JsonConvert.SerializeObject(object),代码中的”object”就是要序列化的.net对象,序列化后返回的是json字符串。

yuzhouhuo2010 2015-06-22
  • 打赏
  • 举报
回复
引用 5 楼 starfd 的回复:
ToString应当是符合业务需要吧…… 你连字段都要显示出来,但你又只是简单的罗列显示……
我的意思是希望罗列的步骤简单,显示的数据很有可视性,不因为要显示数据和字段而干扰类的设计。假如序列化为XML,那么设计类的时候就要考虑可行性。而且XML显示的数据很不友好,看起来一大堆的数据。
  • 打赏
  • 举报
回复
引用 3 楼 yuzhouhuo2010 的回复:
谢谢,我测试json和XML后,发现首先需要引用一个空间,然后后面的操作很繁琐,也有很多不足的地方,包括XML不能序列化私有字段,不支持DIctionary的直接序列化(要报错),序列化后的数据可视性很不友好。
你肯定没有测试json。
threenewbee 2015-06-22
  • 打赏
  • 举报
回复
递归+反射。其实序列化也是这么做的。 不过注意,有些属性可能存在循环引用。会导致无限递归。典型的例子是双向链表。这种情况要加上判断。
  • 打赏
  • 举报
回复
不管什么类型,直接用第三方库转成json是最简单有效而且易读的
  • 打赏
  • 举报
回复
ToString应当是符合业务需要吧…… 你连字段都要显示出来,但你又只是简单的罗列显示……
tcmakebest 2015-06-22
  • 打赏
  • 举报
回复
数据字段的复杂性是无限的,不可能都是可序列化的,最终其实会是树形,楼主要懂得取舍
  • 打赏
  • 举报
回复
可以序列化为json或者xml字符串。
yuzhouhuo2010 2015-06-21
  • 打赏
  • 举报
回复
.NET通过序列或反序列可以将一个对象直接保存,那么肯定是具有这种能力的。但是我们如何能够运用实现描述呢?还望各位大哥大姐指点下啊!
加载更多回复(2)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧