簡體   English   中英

沒有使用Mono的通用Type參數的裝箱或類型參數轉換

[英]No boxing or type parameter conversion for generic Type parameter with 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)

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM