C# 对于带有Mono的泛型类型参数,没有装箱或类型参数转换

C# 对于带有Mono的泛型类型参数,没有装箱或类型参数转换,c#,.net,generics,mono,C#,.net,Generics,Mono,以下代码有什么问题?我看不出下面提到的错误的原因。我使用的是Mono,这可能是Mono中的一个bug,它会在VStudio中编译而没有错误吗 public static class ClientFactory { public static T CreateClient<T, I>() /* error here */ where T : ClientBase<I>, I where I : class { return CreateC

以下代码有什么问题?我看不出下面提到的错误的原因。我使用的是Mono,这可能是Mono中的一个bug,它会在VStudio中编译而没有错误吗

public static class ClientFactory {
  public static T CreateClient<T, I>()
    /* error here */
    where T : ClientBase<I>, I
    where I : class {
    return CreateClient<T, I>(null, null);
  }

  public static T CreateClient<T, I>(string endpointConfigurationName)
    /* error here */
    where T : ClientBase<I>, I
    where I : class {
    return CreateClient<T, I>(endpointConfigurationName, null);
  }

  public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
    /* error here */
    where T : ClientBase<I>, I
    where I : class {
    return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
  }

  public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
    /* NO error here, this method compiles fine */
    where T : ClientBase<I>, I
    where I : class {

    T client;

    /* get client instance */
    /* do stuff with it */

    return client;
  } 
}
公共静态类ClientFactory{
公共静态T CreateClient()
/*这里出错*/
其中T:ClientBase,I
我在哪里上课{
返回CreateClient(null,null);
}
公共静态T CreateClient(字符串endpointConfigurationName)
/*这里出错*/
其中T:ClientBase,I
我在哪里上课{
返回CreateClient(endpointConfigurationName,null);
}
公共静态T CreateClient(字符串endpointConfigurationName,字符串remoteAddress)
/*这里出错*/
其中T:ClientBase,I
我在哪里上课{
返回CreateClient(endpointConfigurationName、remoteAddress、Settings.Default.UserName、Settings.Default.Password);
}
公共静态T CreateClient(字符串endpointConfigurationName、字符串remoteAddress、字符串用户名、字符串密码)
/*这里没有错误,这个方法编译得很好*/
其中T:ClientBase,I
我在哪里上课{
T客户;
/*获取客户端实例*/
/*用它做点什么*/
返回客户;
} 
}
我得到了编译错误:

…/ClientFactory.cs(14,14):错误CS0314:类型“T”不能用作泛型类型或方法“…..ClientFactory.CreateClient(string,string)”中的类型参数“T”。没有从'T'到'System.ServiceModel.ClientBase'的装箱或类型参数转换(CS0314)


TL;DR这可能是您版本中的一个bug:它在我的Mono版本上编译得非常完美


以下代码编译得非常完美:

using System;

namespace so_test
{

    public class ClientBase<T> {
        // whatever
    }

    public static class Settings {
        public static SettingValues Default;
    }

    public class SettingValues { 
        public string UserName;
        public string Password;
    }

    public static class ClientFactory {
        public static T CreateClient<T, I>()
        /* error here */
        where T : ClientBase<I>, I
        where I : class {
            return CreateClient<T, I>(null, null);
        }

        public static T CreateClient<T, I>(string endpointConfigurationName)
        /* error here */
        where T : ClientBase<I>, I
        where I : class {
            return CreateClient<T, I>(endpointConfigurationName, null);
        }

        public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
        /* error here */
        where T : ClientBase<I>, I
        where I : class {
            return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
        }

        public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
        /* NO error here, this method compiles fine */
        where T : ClientBase<I>, I
        where I : class {

            T client = default(T);

            /* get client instance */
            /* do stuff with it */

            return client;
        } 
    }
}

我将您的代码复制到一个新的VC#2010项目中,将
/*do stuff*/
更改为
client=default(T)
并将这两个设置替换为
“”
。编译很好,没有编译器错误。这可能是Mono中的错误吗?您使用的是哪个版本的
dmcs
?我在Visual Studio 2008中没有发现错误,目标是.NET 3.5(使用虚拟
设置
类,并将
/*do stuff*/
更改为
client=null;
)。这在gmcs 2.10.2.0中编译很好(在应用dtb的修改之后)。这确实是mono中的一个bug。它在中引入并在中修复
imac:~ sklivvz$ mono -V
Mono JIT compiler version 2.10.6 (tarball Fri Sep 16 00:13:06 EDT 2011)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
    TLS:           normal
    SIGSEGV:       normal
    Notification:  kqueue
    Architecture:  x86
    Disabled:      none
    Misc:          debugger softdebug 
    LLVM:          yes(2.9svn-mono)
    GC:            Included Boehm (with typed GC)