Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列化对象(this);在JSON中呈现函数的结果_C#_Json - Fatal编程技术网

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