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

本文关键字:类型参数 转换 Mono 泛型类型参数 使用 | 更新日期: 2023-09-27 18:13:32

下面的代码有什么问题?我看不出下面提到的错误的原因。我正在使用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中的类型参数' T'。CreateClient(字符串,字符串)"。没有装箱或从' T'到' System.ServiceModel '的类型参数转换。ClientBase"(CS0314)

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

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)