函数作为变量抛出错误NotImplementedException未处理 [英] Function as variable throwing error NotImplementedException unhandled

查看:649
本文介绍了函数作为变量抛出错误NotImplementedException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经为我的c#脚本添加了一个函数来获取ip地址并将输出作为字符串发送到变量中继承我的源代码。

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Collections.Specialized;
使用System.Net;
使用System.IO;


命名空间ConsoleApplication1
{
class程序
{
static void Main(string [] args)
{
string URL =http://localhost/test2.php;
WebClient webClient =新的WebClient();

NameValueCollection formData = new NameValueCollection();
formData [var1] = formData [var1] = string.Format(MachineName:{0},System.Environment.MachineName);
formData [var2] = stringGetPublicIpAddress();
formData [var3] =DGPASS;

byte [] responseBytes = webClient.UploadValues(URL,POST,formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(responsefromserver);
webClient.Dispose();
System.Threading.Thread.Sleep(5000);


private static string stringGetPublicIpAddress()
{
throw new NotImplementedException();

private string GetPublicIpAddress()
{
var request =(HttpWebRequest)WebRequest.Create(http://ifconfig.me);

request.UserAgent =curl; //这个模拟curl linux命令

string publicIPAddress;

request.Method =GET;
using(WebResponse response = request.GetResponse())
{
using(var reader = new StreamReader(response.GetResponseStream()))
{
publicIPAddress = reader.ReadToEnd();
}
}

return publicIPAddress.Replace(\\\
,);




$ / code>

基本上我是已经创建了这个函数
$ b $ pre $ code private static string stringGetPublicIpAddress()

,我将它作为变量发送

  formdata [var2 ] = stringGetPublicIpAddress(); 

我收到这个错误

  throw new NotImplementedException(); === NotImplementedException未处理


解决方案

您... didn不实施该方法。你有这个:

pre $私人静态字符串stringGetPublicIpAddress()
{
throw new NotImplementedException();
}

当然,每当您调用该方法时,它都会抛出该异常。它看起来像你确实实现了你想要的方法,但是:

  private string GetPublicIpAddress() 
{
//其余的代码
}

也许这是一些复制/粘贴错误的结果?尝试摆脱引发异常的小方法,并将实现的方法更改为静态:

  private static string GetPublicIpAddress() 
{
//其余的代码
}

然后更新任何你调用它的地方:

  stringGetPublicIpAddress(); b 




GetPublicIpAddress();

这看起来像是以奇怪的方式出现错误的复制/粘贴错误。或者你正在努力解决静态和实例方法之间的区别?也许你实现了这个方法,但是编译器提示你需要一个静态方法?有很多需要了解的静态或实例方法/成员,我不会在这里进入。这是面向对象编程中的一个重要概念。

在这种特殊情况下,由于您处于 Main ,你从该类的 Main 调用的任何东西( Program 类)也需要静态的,除非你创建一个

  var program = new Program(); 

这将允许您调用 Program

  program.SomeNonStaticMethod(); 

但是对于像这样的小应用程序来说,这并不是必须的。


okay so I have added a function to my c# script to fetch ip address and send the output as string in a variable heres my source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string URL = "http://localhost/test2.php";
        WebClient webClient = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
        formData["var2"] = stringGetPublicIpAddress();
        formData["var3"] = "DGPASS";

        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();
        System.Threading.Thread.Sleep(5000);
    }

    private static string stringGetPublicIpAddress()
    {
        throw new NotImplementedException();
    }
        private string GetPublicIpAddress()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me");

        request.UserAgent = "curl"; // this simulate curl linux command

        string publicIPAddress;

        request.Method = "GET";
        using (WebResponse response = request.GetResponse())
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                publicIPAddress = reader.ReadToEnd();
            }
        }

        return publicIPAddress.Replace("\n", "");

    }
    }
    }

basically I have created this function

private static string stringGetPublicIpAddress()

and I am sending it as a variable

formdata["var2"] = stringGetPublicIpAddress();

I am getting this error

throw new NotImplementedException();  === NotImplementedException was unhandled

解决方案

You... didn't implement the method. You have this:

private static string stringGetPublicIpAddress()
{
    throw new NotImplementedException();
}

So of course any time you call that method, it's going to throw that exception. It looks like you did implement the method you want here, though:

private string GetPublicIpAddress()
{
    // the rest of your code
}

Maybe this is the result of some copy/paste error? Try getting rid of the small method that throws the exception and changing the implemented method to be static:

private static string GetPublicIpAddress()
{
    // the rest of your code
}

Then update anywhere you call it from this:

stringGetPublicIpAddress();

to this:

GetPublicIpAddress();

This really just looks like copy/paste errors gone wrong in strange ways. Or perhaps you're struggling with the difference between static and instance methods? Maybe you implemented the method, but the compiler suggested that you needed a static method? There's a lot to read about static vs. instance methods/members which I won't really get into here. It's a significant concept in object-oriented programming.

In this particular case, since you're in the context of a static method in Main, anything you call from Main on that class (the Program class) also needs to be static, unless you create an instance of Program like this:

var program = new Program();

This would allow you to call instance methods on Program:

program.SomeNonStaticMethod();

But for a small application such as this, that isn't really necessary.

这篇关于函数作为变量抛出错误NotImplementedException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆