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

标签 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): Error CS0314: The type `T' cannot be used as type parameter `T' in the generic type or method `….ClientFactory.CreateClient(string, string)'. There is no boxing or type parameter conversion from `T' to `System.ServiceModel.ClientBase' (CS0314)

最佳答案

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)

关于c# - 使用 Mono 的泛型类型参数没有装箱或类型参数转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020053/

相关文章:

c# - 如何在焦点输入控件上抑制 NotifyPropertyChanged 事件?

Java按泛型类型获取集合

c# - 将 String 转换为 Type,其中 Type 是一个变量

java - ArrayList<anyClassObject> 的动态初始化

c# - 从带格式的 datagridview 生成 Excel 的最快方法

c# - IEnumerable<T> 上的延迟 OrderBy

c# - 为什么 typeA == typeB 比 type == typeof(Type B) 慢?

.net - 充分了解 .NET 版本和向后兼容性

c# - Net Core 3.1 API的Swagger UI非常慢

c# - 关闭联锁保护程序