Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/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# Visual Studio/RestSharp-无法导航到插入符号下的符号(CS0118)_C#_Visual Studio_Restsharp - Fatal编程技术网

C# Visual Studio/RestSharp-无法导航到插入符号下的符号(CS0118)

C# Visual Studio/RestSharp-无法导航到插入符号下的符号(CS0118),c#,visual-studio,restsharp,C#,Visual Studio,Restsharp,我现在只编写了大约两年的代码(断断续续),但是我非常熟悉VS的多个版本中的这个神秘错误。我知道这个问题不仅仅与RestSharp有关 这个“插入错误”一直是我VS经验的祸根,所以这就是我来这里的原因 环境: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sys

我现在只编写了大约两年的代码(断断续续),但是我非常熟悉VS的多个版本中的这个神秘错误。我知道这个问题不仅仅与RestSharp有关

这个“插入错误”一直是我VS经验的祸根,所以这就是我来这里的原因

环境:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;

namespace newDawn2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new RestClient("https://api.twitter.com/1.1");
            client.Authenticator = new HttpBasicAuthenticator("username", "password");

            var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);

            var response = client.Get(request);
        }
    }
}
Microsoft Visual Studio Community 2019版本16.8.1 RestClient v4.0.30319 Windows 10专业版

我想做什么:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;

namespace newDawn2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new RestClient("https://api.twitter.com/1.1");
            client.Authenticator = new HttpBasicAuthenticator("username", "password");

            var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);

            var response = client.Get(request);
        }
    }
}
我遵循RestClient上非常简单的说明。我已经安装了RestClient,添加了引用,但当我尝试开始时,我会遇到可怕的插入符号错误

除了对RestClient的引用之外,这些都不是我的代码,这可能不合适(我不知道)

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;

namespace newDawn2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new RestClient("https://api.twitter.com/1.1");
            client.Authenticator = new HttpBasicAuthenticator("username", "password");

            var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);

            var response = client.Get(request);
        }
    }
}
谢谢你的阅读

using RestClient;
这里您声明使用的是名称空间RestClient,而不是对象

var client = new RestClient(...
此处出现错误是因为您正试图创建刚才声明使用的命名空间的实例

对象与其命名空间的一部分共享名称是不常见且容易混淆的,因此通常不应期望看到与创建对象时使用的名称相同的using语句。 名称空间用于分离、组织类并限制编译时加载的类。
相同名称的类可以存在于不同的名称空间中。 您可以创建自己的名为RestClient的类,并在这个项目中使用它(可能用于测试),只要它们存在于不同的名称空间中。您现在使用的类的完整声明是RestSharp.RestClient。您可以使用完整声明Facundo.Test.RestClient创建一个类,并在同一项目中使用它们。但是,在本例中,您不能使用using语句,每次您想要实例化其中一个语句时,都需要完全声明名称空间

RestSharp.RestClient client = new RestSharp.RestClient(...);


我不建议在实践中将它们命名为相同的名称,只是尝试说明名称空间。

您在using语句中将RestClient声明为名称空间,然后尝试实例化它,这就是错误所在。查看链接的示例,需要使用语句删除RestClient。一个类与它的部分名称空间共享一个名称是不常见的,也是令人困惑的。正如@Adam所提到的,并且方向与他相同,您应该避免使用与类型完全相同的名称空间。在RestSharp站点提供的示例中,没有使用RestClient;。如果您出于任何原因创建了自定义类型,请尽量避免以完全相同的方式调用该类型。例如:NameSpace:MyDomian.MyCustomRestClient.Thank you,但我只是在遇到此错误(绝望)后才添加了该语句。我现在已经删除了命名空间中的RestClient声明,没有效果。snip:当您使用语句删除RestSharp并完全声明客户机时会发生什么:RestSharp.RestClient client=new RestSharp.RestClient(“);发生了好事!但是为了使其余代码正常工作(RestRequest),我不得不用RestSharp补充说:但是谢谢!我想知道我是如何在这里投票的。