惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
N
News and Events Feed by Topic
博客园 - Franky
Spread Privacy
Spread Privacy
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
博客园 - 叶小钗
S
Securelist
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Vulnerabilities – Threatpost
量子位
D
Docker
NISL@THU
NISL@THU
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
小众软件
小众软件
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Heimdal Security Blog
L
Lohrmann on Cybersecurity
IT之家
IT之家
Webroot Blog
Webroot Blog
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale

博客园 - On the road....

c# -- 对象销毁和垃圾回收 C#集合 -- Equality和Order插件 C#集合--Dictionary C#集合 -- 自定义集合与代理 C#集合 -- Lists,Queues, Stacks 和 Sets C#集合--ICollection接口和IList接口 C#集合--数组 C#集合-列举(Enumeration) C#排序比较 Customer IEnuramble Extension C#相等性比较 c#列举和迭代器 C#记录对象的变化 c#如何区分静态只读变量和常量 C#如何更好地理解引用类型和值类型 C#代理那点事儿 Pro ASP.NET MVC –第五章 使用Razor Pro ASP.NET MVC –第六章 MVC的基本工具 Pro ASP.NET MVC –第四章 语言特性精华
C#按需序列化对象为Json字符串
On the road.... · 2014-02-21 · via 博客园 - On the road....

只贴代码,不解释了。新的代理类型确实很给力!

public static class JsonHelper
{
    public static string ToJsonString<T>(IList<T> list, Func<T, string> fun)
    {
        StringBuilder buffer = new StringBuilder();
        bool isFirst = true;            
        
        foreach (T t in list)
        {
            if (!isFirst)
                buffer.Append(",");

            buffer.Append(fun(t));
            isFirst = false;
        }

        return buffer.ToString();
    }      

    public static string ToJsonString<T>(T t, Func<T, string> fun)
    {
        return fun(t);
    }
}

Josn Helper

实体类

public class Staff
{
    public string StaffNo { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Staff(string no, string fn, string ln)
    {
        StaffNo = no;
        FirstName = fn;
        LastName = ln;
    }
}

Staff Class

测试代码

class Program
{
    static void Main(string[] args)
    {
        Staff s = new Staff("10000", "11111", "22222");
        IList<Staff> list = new List<Staff> { s,s };          

        JsonHelper.ToJsonString<Staff>(list, o=>string.Format("{{firstname:{0}}}", o.FirstName));
        JsonHelper.ToJsonString<Staff>(s, o=>string.Format("{{firstname:{0}, lastname:{1}}}", o.FirstName, o.LastName)); 
        
        Console.ReadLine();
    }
}

Test code

收工,走人

posted @ 2014-02-21 16:56  On the road....  阅读(1549)  评论()    收藏  举报