【发布时间】:2018-04-17 18:38:25
【问题描述】:
作为一个简单的例子,试图让它在控制台应用程序中工作。 我正在使用来自 nuget 的 Json.Net 和 DalSoft.RestClient 包。我没有与其中任何一个结婚,我只是想完成工作。接受替代方案。
http://jsonplaceholder.typicode.com/users 返回有效的 JSON。
我想提前列举出未知的属性。以下似乎可行,但因 System.AggregateException - RuntimeBinderException:无法在空引用上执行运行时绑定而失败 我觉得我错过了一些简单的事情。
using System;
using System.Threading.Tasks;
using DalSoft.RestClient;
using Newtonsoft.Json.Linq;
namespace ImTired
{
class Program
{
static void Main(string[] args)
{
DoThingsAsync().Wait();
Console.ReadLine();
}
private static async Task<object> DoThingsAsync()
{
dynamic client = new RestClient("http://jsonplaceholder.typicode.com");
var user = await client.Users.Get();
var foo = user[0].name; // grab the first item (Works to this point)
//JProperty item = new JProperty(user);
var item = user[0];
foreach (JProperty property in item.Properties()) //(fails here I've missed a cast or conversion)
{
Console.WriteLine(property.Name + " - " + property.Value);
}
Console.WriteLine("Call Complete: " + foo);
return null;
}
}
}
跟进编辑:为任何潜伏者添加了此内容。 我需要将程序集添加到 GAC 以使其可用于scripting tool。这是您的强名称信息。
我知道可能还有另一种更好的方法来引用非 GAC 程序集。我只是没有时间弄清楚。也许我会在这里提出一个有意义的问题。
【问题讨论】:
-
应该
item.Properties()是item.properties()还是item.properties?该错误听起来像是.Properties()是错误的调用方式,因为这将是您创建类的新实例的方式,而不是访问属性或方法的方式。 -
item.GetType().FullName返回的item的实际类型是什么?client.Users.Get()返回的声明类型是什么?最后,异常的完整ToString()输出是什么,包括异常类型、消息、回溯和内部异常(如果有)?
标签: c# json rest nuget-package dalsoft.restclient