没有使用Mono的通用Type参数的装箱或类型参数转换

时间:2011-11-05 12:03:36

标签: c# .net generics mono

以下代码有什么问题?我看不出下面提到的错误的原因。我正在使用Mono,这可能是Mono中的一个错误,它会在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.cs(14,14):错误CS0314:类型`T'不能用作泛型类型或方法`... .ClientFactory.CreateClient(string,string)'中的类型参数`T'。从“T”到“System.ServiceModel.ClientBase”(CS0314)没有装箱或类型参数转换

1 个答案:

答案 0 :(得分:2)

TL; DR 它可能是您版本中的错误:它在我的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;
        } 
    }
}

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)