简体   繁体   English

JsonConvert.SerializeObject(本); 在JSON中呈现函数的结果

[英]JsonConvert.SerializeObject(this); to render the results of a function in the JSON

Is there any way to get JsonConvert.SerializeObject(this); 有没有办法得到JsonConvert.SerializeObject(this); to render the results of a function in the Class? 在类中渲染函数的结果?

     public string Name { get; private set; } // renders fine in the outputted json



        public string AdHocRecipientsStub()// I want this also in the outputted json
            {
                return AdHocRecipients.ToString().Substring(0, 15) + "...";
            }

You can just wrap it in a property: 你可以把它包装在一个属性中:

static void Main(string[] args)
{
    Console.WriteLine(JsonConvert.SerializeObject(new Test()));
}

public class Test
{
    public string Test1 { get { return "test1"; } }
    public string Test2 { get { return Test2Func(); } }
    private string Test2Func()
    {
        return "test2";
    }
}

Outputs: 输出:

{"Test1":"test1","Test2":"test2"}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM