C# 序列化对象(this);在JSON中呈现函数的结果
是否有任何方法可以获取C# 序列化对象(this);在JSON中呈现函数的结果,c#,json,C#,Json,是否有任何方法可以获取JsonConvert.SerializeObject(这个)以呈现类中函数的结果 public string Name { get; private set; } // renders fine in the outputted json public string AdHocRecipientsStub()// I want this also in the outputted json {
JsonConvert.SerializeObject(这个)代码>以呈现类中函数的结果
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"}