簡體   English   中英

JsonConvert.SerializeObject(本); 在JSON中呈現函數的結果

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

有沒有辦法得到JsonConvert.SerializeObject(this); 在類中渲染函數的結果?

     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) + "...";
            }

你可以把它包裝在一個屬性中:

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

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM