【总结】反射与接口的新发现

在<<利用反射,自动将对象生成json字符串【反射应用的一个小DEMO】>>一文中,我用到反射和扩展方法的技术来实现将对象序列化成json字符串的功能.后来我又对代码进行了些许重构,代码如下:

ContractedBlock.gif ExpandedBlockStart.gif View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using System.Reflection;
7
8 namespace CodeCenterCore.Refelect
9 {
10 [AttributeUsage(AttributeTargets.Property, Inherited = false)]
11 public class ToJSONAtrribute : Attribute
12 {
13 public ToJSONAtrribute()
14 {
15 }
16 }
17
18 public static class ExensionJSON
19 {
20 public static string ToJSONList<T>(this IEnumerable<T> source)
21 {
22 string returnValue = "";
23 StringBuilder sb = new StringBuilder();
24 sb.Append("[");
25 foreach (T item in source)
26 {
27 sb.Append(item.ToJSON() + ",");
28 }
29 sb.Append("]");
30 returnValue = sb.ToString();
31 returnValue = Regex.Replace(returnValue, @",]", "]");
32 return returnValue;
33 }
34
35 public static string ToJSON<T>(this T source)
36 {
37 string returnValue = "";
38 StringBuilder sb = new StringBuilder();
39 sb.Append("{");
40 Type typeInfo = source.GetType();
41 PropertyInfo[] properties = typeInfo.GetProperties();
42 foreach (PropertyInfo property in properties)
43 {
44 object[] attrs = property.GetCustomAttributes(typeof(ToJSONAtrribute), false);
45 if (attrs != null)
46 {
47 foreach (ToJSONAtrribute attr in attrs)
48 {
49 if (attr != null)
50 {
51 sb.Append(string.Format("\"{0}\":\"{1}\",", property.Name, property.GetValue(source, null)));
52 }
53 }
54 }
55 }
56 sb.Append("}");
57 returnValue = sb.ToString();
58 returnValue = Regex.Replace(returnValue, @",}", "}");
59 return returnValue;
60 }
61 }
62 }

以下是我今天的一个新发现(看见别人代码是这样些的):

ContractedBlock.gif ExpandedBlockStart.gif View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6
7 namespace CodeCenterCore.Refelect
8 {
9 public interface IEntity
10 {
11 }
12 public class User : IEntity
13 {
14 private int id;
15 private string name;
16 public User(int id, string name)
17 {
18 this.id = id;
19 this.name = name;
20 }
21 [ToJSONAtrribute]
22 public int Id
23 {
24 get { return this.id; }
25 set { this.id = value; }
26 }
27 [ToJSONAtrribute]
28 public string Name
29 {
30 get { return this.name; }
31 set { this.name = value; }
32 }
33 }

以上代码,先定义了一个空接口IEntity,接口里面没有定义任何的成员,然后定义一个类User派生自IEntity.

IEntity entity = new User(1,"peter");

string entityJson = entity.ToJSON();

此时在扩展方法中,T的类型为IEntity,sorce的类型仍旧为User,也就是说,利用反射依旧可以取得User对象上的成员.

PS:表达能力实在是太差了,要多写,多写!组织语言的能力都丢的差不多了.

转载于:https://www.cnblogs.com/riverdeng/archive/2011/07/19/2111103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值