C# 将JsonBody移动到其他类文件

C# 将JsonBody移动到其他类文件,c#,json,C#,Json,我试图将一个jsonBody对象移动到另一个类文件中,但是当我从另一个类调用该方法时,json属性无法正确传递 有什么解决办法吗 这就是我正在尝试的 public void WhenIPerformPostOperatiom(string url, Table table) { string productId = "11234"; _settings.Request = new RestRequest(url, Method.POST); JsonObjects jsonBod

我试图将一个jsonBody对象移动到另一个类文件中,但是当我从另一个类调用该方法时,json属性无法正确传递 有什么解决办法吗

这就是我正在尝试的

public void WhenIPerformPostOperatiom(string url, Table table) {
 string productId = "11234";
 _settings.Request = new RestRequest(url, Method.POST);

     JsonObjects jsonBody = new JsonObjects();
     jsonBody.AQuestions(productId, table);

 _settings.Request.AddJsonBody(jsonBody);
}
还有这个物体

public class JsonObjects
    {
       public void AQuestions(string productId,Table table)
        {
            dynamic data = table.CreateDynamicInstance();
            var jsonBody = new AccountRoot()
            {
                productId = productId,
                questions = new[]
                  {
                       new Questions() {
                        questionId = "b2b-2.01",
                        question ="Q1",
                        answers = new []
                        {
                            new Answers(){
                            answerValue = data.answerValue1
                            }
                        }
                    }
            }
        };
    }
}

如果希望在调用
AQuestions(…)
后可以访问
数据和
jsonBody
成员变量,则需要将它们设置为成员变量。jsonBody的类型可以是object,如果您希望它包含任何类型。。。但这使得它很难用于除存储数据以外的任何其他用途,以便通过反射进行遍历

public class JsonObjects
{
    public dynamic data;
    public AccountRoot jsonBody;

    public void AQuestions(string productId,Table table)
    {
        data = table.CreateDynamicInstance();
        jsonBody = new AccountRoot()
        {
            productId = productId,
            questions = new[]
            {
                new Questions() 
                {
                    questionId = "b2b-2.01",
                    question ="Q1",
                    answers = new []
                    {
                        new Answers()
                        {
                            answerValue = data.answerValue1
                        }
                    }
                }
            }
        };
    }
}

你希望复制什么
AQuestions
只设置在方法之外无法访问的局部变量。是的,这就是问题所在,变量只在局部设置,我希望在不同的文件中创建JsonBody,以便使用post操作。如果我在方法WhenImportoOperation中有JsonBody,则此方法有效