'RestSharp.IRestResponse`1 '在没有引用的程序集中定义

问题描述:

我正在开发一个需要SMS网关服务的应用程序。我们公司使用Plivo服务。我沿着sample code。 当我试图建立的解决方案,我收到两个错误:'RestSharp.IRestResponse`1 <T0>'在没有引用的程序集中定义

错误1:The type 'RestSharp.IRestResponse'1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'RestSharp, Version=105.1.0.0, Culture=neutral, PublicKeyToken=null'

错误2:Cannot implicitly convert type 'RestSharp.IRestResponse'1<Plivo.API.MessageResponse>' to 'RestSharp.IRestResponse<Plivo.API.MessageResponse>'

我不明白这些错误的原因,因为我安装Plivo和RestSharp API的通过NuGet,并可以在解决方案资源管理器中看到dll。 第二个错误更令我困惑,因为奇怪的类型'RestSharp.IRestResponse'1

如果有人能就这个问题提出建议,我会非常感激。

我的源代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using RestSharp; 
using Plivo.API; 
using System.Reflection; 

namespace SMSGatewayTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string auth_id = "XXX";  // obtained from Plivo account dashboard 
      string auth_token = "YYY"; // obtained from Plivo account dashboard 

      RestAPI plivo = new RestAPI(auth_id, auth_token); 

      IRestResponse<MessageResponse> resp = plivo.send_message(new Dictionary<string, string>() 
      { 
       { "src", "37061549145" }, 
       { "dst", "37068824525" }, 
       { "text", "Hi, text from Plivo." }, 
      }); 


      if (resp.Data != null) 
      { 
       PropertyInfo[] proplist = resp.Data.GetType().GetProperties(); 
       foreach (PropertyInfo property in proplist) 
       Console.WriteLine("{0}: {1}", property.Name, property.GetValue(resp.Data, null)); 
      } 
      else 
      { 
       Console.WriteLine(resp.ErrorMessage); 
      } 
     } 
    } 
} 

附:如果我在写一个问题的时候错过了一些东西,请询问它 - 这是我第一次使用短信网关的经验

+0

RestSharp.IRestResponse'1'只是一个名为'IRestResponse'的通用类型,它带有一个通用类型参数。这就是它在IL中的命名方式 - 在C#中的等价物就像'IRestResponse '一样。 – Luaan

+0

@Luaan,谢谢,这是有道理的,尽管我仍然不明白为什么编译器试图转换smth,根据API doc send_message()方法具有相同的类型。 –

+0

其实,类型名称*是*可疑 - 你确定你正确地复制它?如果是C#或IRestResponse [1] [System.String]',但肯定不是'IRestresponse'1 ',它应该是'IRestResponse '。你确定你遵循了这封信的安装说明吗? VS为'plivo.send_message'方法显示什么样的返回类型? – Luaan

因此经过几个小时的调查后,我设法找出案件。似乎自动NuGet安装安装RestSharp版本100.0.0,并且您需要105.1.0.0。解决方法是在NuGet控制台中输入以下内容: 安装包RestSharp -Version 105.0.1

+0

我也有同样的问题。但是当我尝试该方法建议它没有工作,因为nuget说没有更新可用。这个问题似乎是Plivo版本2包使用这个依赖。 要修复它,我恢复到使用RestSharp 104.4的Plivo 1.3.7,它的工作原理 –

+0

@TopDev我们在2.0.1中也遇到了这个问题,但在1.3.7上工作正常,你碰巧使用了单声道吗? – vonec

+0

我刚刚尝试了v2.0.0,它工作正常,似乎问题是在nuget包中没有配置正确的RestSharp依赖项。我在这里记录了这个问题:https://github.com/plivo/plivo-dotnet/issues/25 – vonec