无方法重载的c#多态性(对于webmethod)

无方法重载的c#多态性(对于webmethod),c#,C#,好的,在写了一些重载方法之后,我意识到webmethods不能重载(我所说的“意识到”是指VS抛出了一个paddy,不允许我更新服务引用)。我试着像这样绕过它: public string DoPing<T>(T IP) { if (typeof(T) == typeof(string)) { return DoPingString(IP); } if (typeof(T) =

好的,在写了一些重载方法之后,我意识到webmethods不能重载(我所说的“意识到”是指VS抛出了一个paddy,不允许我更新服务引用)。我试着像这样绕过它:

    public string DoPing<T>(T IP)
    {

        if (typeof(T) == typeof(string))
        { 
            return DoPingString(IP);
        }
        if (typeof(T) == typeof(IPAddress))
        {
            return DoPingIP(IP);
        }
        throw new Exception("Programmer Error");
    }
公共字符串(IP)
{
if(typeof(T)=typeof(string))
{ 
返回摄影管柱(IP);
}
if(typeof(T)=typeof(IPAddress))
{
返回DoPingIP(IP);
}
抛出新异常(“程序员错误”);
}
但是当我调用相应的(重命名的)方法时,我得到了一个无法从T转换为string/iPAddress的错误。 有人能解释一下为什么它不起作用,或者可能修复它,或者给我一个替代的解决方案吗? 提前谢谢

编辑:这一点很好,泛型可能有些过分(我尝试了另一种解决方案,但在尝试之前失败了)。 DoPingString(字符串字符串)和DoPingIP(IP地址)是签名。
明天我会给最接近的答案打勾。我用另一种方法解决了这个问题。

尝试使用
Convert.ChangeType
方法,然后像往常一样强制转换:

if (typeof(T) == typeof(string))
{ 
    return DoPingString((string)Convert.ChangeType(IP, typeof(string)));
}
if (typeof(T) == typeof(IPAddress))
{
   return DoPingIP((IPAddress)Convert.ChangeType(IP, typeof(IPAddress)));
}

尝试使用
Convert.ChangeType
方法,然后像往常一样强制转换:

if (typeof(T) == typeof(string))
{ 
    return DoPingString((string)Convert.ChangeType(IP, typeof(string)));
}
if (typeof(T) == typeof(IPAddress))
{
   return DoPingIP((IPAddress)Convert.ChangeType(IP, typeof(IPAddress)));
}

我最好这样做:

public string DoPing(IPAddress ip)
{
    ...logic...
}

public string DoPing(String ip)
{
    ...logic...
}

尽量避免不必要的模式和模板。

我最好这样做:

public string DoPing(IPAddress ip)
{
    ...logic...
}

public string DoPing(String ip)
{
    ...logic...
}

尽量避免不必要的模式和模板。

您没有按照预期的方式使用泛型。编译器不知道T是什么类型,所以它不会让您隐式或显式地强制转换为String或IPAddress。 以下是一个有效的版本:

    public string DoPing<T>(T IP)
    {
        if (typeof(T) == typeof(string))
        {
            return DoPingString((String)Convert.ChangeType(IP, typeof(String)));
        }
        if (typeof(T) == typeof(IPAddress))
        {
            return DoPingIP((IPAddress)Convert.ChangeType(IP, typeof(IPAddress)));
        }
        throw new Exception("Programmer Error");
    }
当然,更简单、更容易的方法是简单地重载函数,即:

public string DoPing(string IP)
{
    return DoPingString(IP);
}
public string DoPing(IPAddress IP)
{
    return DoPingIP(IP);
}

您没有按预期的方式使用泛型。编译器不知道T是什么类型,所以它不会让您隐式或显式地强制转换为String或IPAddress。 以下是一个有效的版本:

    public string DoPing<T>(T IP)
    {
        if (typeof(T) == typeof(string))
        {
            return DoPingString((String)Convert.ChangeType(IP, typeof(String)));
        }
        if (typeof(T) == typeof(IPAddress))
        {
            return DoPingIP((IPAddress)Convert.ChangeType(IP, typeof(IPAddress)));
        }
        throw new Exception("Programmer Error");
    }
当然,更简单、更容易的方法是简单地重载函数,即:

public string DoPing(string IP)
{
    return DoPingString(IP);
}
public string DoPing(IPAddress IP)
{
    return DoPingIP(IP);
}

您可以使用
WebMethodAttribute

[WebMethod(MessageName = "DoPingIPAddress")]
public string DoPing(IPAddress ip) { }

[WebMethod(MessageName = "DoPingString")]
public string DoPing(string ip) { }
但是从客户端来看,您将有两种不同的方法:web服务/SOAP约束


您可以使用
WebMethodAttribute

[WebMethod(MessageName = "DoPingIPAddress")]
public string DoPing(IPAddress ip) { }

[WebMethod(MessageName = "DoPingString")]
public string DoPing(string ip) { }
但是从客户端来看,您将有两种不同的方法:web服务/SOAP约束


听起来您希望在WCF中提供
KnownTypeAttribute
。但是既然你不能在这里使用它,你肯定只需要一种方法,那么我认为唯一的方法就是只提供两个参数。只需添加一些验证来检查空值,并将它们传递给您的私有助手方法

[WebMethod] 
public string DoPing(string ipString, IPAddress ipAdress)
{
    if(ipString != null)
    {
        DoPing(ipString);
    }
    if(ipAdress != null)
    {
        DoPing(ipAdress);
    }
}

听起来您希望在WCF中提供
KnownTypeAttribute
。但是既然你不能在这里使用它,你肯定只需要一种方法,那么我认为唯一的方法就是只提供两个参数。只需添加一些验证来检查空值,并将它们传递给您的私有助手方法

[WebMethod] 
public string DoPing(string ipString, IPAddress ipAdress)
{
    if(ipString != null)
    {
        DoPing(ipString);
    }
    if(ipAdress != null)
    {
        DoPing(ipAdress);
    }
}


XML Web服务对吗?如果不支持重载,那么您怎么会猜到支持泛型呢?据我所知,您确实需要创建不同的方法签名,这意味着不同的方法名称,并且没有泛型…而且,这里的泛型与使用
对象
参数相比没有特别的好处,真的。请显示DoPingString和DoPingIP的签名。web方法中不能有泛型参数。所有内容都作为字符串发送,因此框架必须知道所需的类型。它无法猜测可能将字符串转换为什么数据类型。您不能在web服务中公开泛型,但可以使用
WebMethodAttribute
。XML web服务,对吗?如果不支持重载,那么您怎么会猜到支持泛型呢?据我所知,您确实需要创建不同的方法签名,这意味着不同的方法名称,并且没有泛型…而且,这里的泛型与使用
对象
参数相比没有特别的好处,真的。请显示DoPingString和DoPingIP的签名。web方法中不能有泛型参数。所有内容都作为字符串发送,因此框架必须知道所需的类型。它无法猜测可能将字符串转换为什么数据类型。您不能在web服务中公开泛型,但可以使用
WebMethodAttribute
。这与重载非常相似。你确定你读过这个问题吗?看起来很像重载。你确定你读过这个问题吗?OP说他先尝试重载,但他的RPC库不起作用。你的第二个答案与我最终的答案类似。OP说他先尝试重载,但是他的RPC库不起作用。你的第二个答案与我最终的答案类似。想法是从客户端使用一个函数到服务器端的多个函数。不过谢谢你,我学到了一些新东西。好的,恐怕你不能用web服务来实现这一点。也许使用WCF您可以找到符合需求的东西:-)想法是从客户端使用一个函数到服务器端的多个函数。不过谢谢你,我学到了一些新东西。好的,恐怕你不能用web服务来实现这一点。也许使用WCF您可以找到符合要求的内容:-)