C# 如何使用GoogleAPI.NET客户端库构建预测请求

C# 如何使用GoogleAPI.NET客户端库构建预测请求,c#,google-api-dotnet-client,C#,Google Api Dotnet Client,我试图实例化一个列表,每个列表项只包含一个字符串。我将GoogleAPI库用于他们的预测API,当我设置输入体(InputValue.CsvInstance)时,它需要一种IList类型 InputValue.CsvInstance是一个虚拟IList,描述为输入功能列表,可以是字符串或双精度 var body = new Google.Apis.Prediction.v1_6.Data.Input(); IList<string> list = new List<strin

我试图实例化一个列表,每个列表项只包含一个字符串。我将GoogleAPI库用于他们的预测API,当我设置输入体(InputValue.CsvInstance)时,它需要一种IList类型

InputValue.CsvInstance是一个虚拟IList,描述为输入功能列表,可以是字符串或双精度

var body = new Google.Apis.Prediction.v1_6.Data.Input();

IList<string> list = new List<string>();
list.Add("this is some text in english!");

body.InputValue.CsvInstance = (IList<object>)list;  //fails here
var body=new Google.api.Prediction.v1_6.Data.Input();
IList list=新列表();
添加(“这是一些英文文本!”);
body.InputValue.CsvInstance=(IList)列表//这里失败了
我尝试只创建IList,但在尝试将其强制转换为IList时,我得到一个无效的强制转换异常

我尝试创建一个IList,并用字符串填充它,但在运行时它会抛出一个Null异常错误

有人能给我举个例子,说明如何创建这个IList,以便设置CSV状态吗

编辑: 所以我不确定这与我如何创建列表有关。我想我没有正确使用客户端库

我正在尝试调用此函数:

看起来请求的主体需要是Google.api.Prediction.v1_6.Data.Input()类型,这就是我在设置InputValue时遇到的问题。csv立场:

一个
IList
与一个
IList
不同,基本上它们是完全不同的类

试试这个:

IList<object> list = new List<object>();
list.Add("this is some text in english!");

body.InputValue.CsvInstance = list;
IList list=new list();
添加(“这是一些英文文本!”);
body.InputValue.CsvInstance=列表;

我对这方面不太熟悉,但我以前看过。我会仔细阅读,看看是否有帮助。如果我读的是正确的,那么您应该能够允许它隐式转换,因为您将强制转换为派生较少的类型(即协方差)

更新: 我做了一些测试。转换IList不是协变的,因此它不允许您进行转换,但IEnumerable是。如果您需要将列表存储为字符串而不是对象,那么您可以选择以相当简洁的方式从中创建一个新的对象列表,尽管可能会带来相当好的性能影响。请参见下面的我的代码:

        List<string> strings = new List<string>();
        strings.Add("test1");

        IList<object> objectList = strings; // Error at compile time
        IEnumerable<object> objectEnumerable = strings; // Works fine, but is not an IList<object>

        IList<object> stringObjects = new List<object>(strings); // Works, but creates a whole new list to do it.
List strings=newlist();
添加(“test1”);
IList objectList=strings;//编译时出错
IEnumerable objectEnumerable=字符串;//工作正常,但不是IList
IList stringObjects=新列表(字符串);//工作,但创建一个全新的列表来完成。
我发现这是可行的:

IList<object> vals = "0,-0.25,430,-1.63,250,-1.75".Split(',');
Input input = new Input();
input.InputValue = new Input.InputData();
input.InputValue.CsvInstance = vals;
IList vals=“0,-0.25430,-1.63250,-1.75”。拆分(',');
输入=新输入();
input.InputValue=新的input.InputData();
input.InputValue.CsvInstance=VAL;

这可以很好地编译,但它在运行时抛出NullReferenceException(对象引用未设置为对象的实例)。添加更多信息,它在哪里抛出异常?你是舒尔吗?这与这部分代码有关吗?这就是我要设置的属性。我不确定是不是我的代码出了问题。我试过:body.InputValue.CsvInstance.Add(“这是一些文本”);它也会失败,出现Null Ref异常。是否可能CSV状态尚未实例化??Google的.NET库的文档记录非常糟糕,所以我似乎找不到任何例子。它被抛出到设置CsvInstance的地方。我在问题中添加了更多信息。我想我对实际问题的理解是错误的。好吧,问题是InputValue为null,所以必须实例化InputValue,设置CsvInstance属性,然后将该实例设置为body.InputValue(如果body不是null…)