在C#中调用静态方法

MJC*_*der 11 c# static-methods

如何调用静态方法?我想从我创建的类中调用它,我想从IP获取位置.我已经宣布了它,但我需要做的是调用方法......如static...

为了跟你说实话,我很困惑在这里,我需要实例化address,city等等?

到目前为止我已经这样做了;

LocationTools.cs

public static class LocationTools
    {
        public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude, out double? longitude)
        {
Run Code Online (Sandbox Code Playgroud)

Home.cs

   public string IPAPIKey
    {
       get
        {
            return WebConfigurationManager.AppSettings["IPAPIKey"];
        }
    }

    ////To get the ip address of the machine and not the proxy use the following code
    static void GetLocationFromIP()
    {
        string strIPAddress = Request.UserHostAddress.ToString();
        strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIPAddress == null || strIPAddress == "")
        {
            strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Pet*_*ton 6

你去吧

static void GetLocationFromIP()
{
    string strIPAddress = Request.UserHostAddress.ToString();
    strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (strIPAddress == null || strIPAddress == "")
    {
        strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    string city = string.Empty;
    string region = string.Empty;
    string country = string.Empty;
    double latitude = -1.00;
    double longitude = -1.00;

    LocationTools.GetLocationFromIP(strIPAddress, out city, out region, out country, out latitude, out longitude)
}
Run Code Online (Sandbox Code Playgroud)


pan*_*321 6

当您想要提供某些实用程序时,通常会使用静态类,因此您不必创建这些类的对象.您可以通过简单地按类名调用并调用成员函数来从其他类调用这些方法.

例如,您可以在此处调用LocationTools.GetLocationFromIP();

希望能帮助到你!