2011-11-05 41 views
9

以下代碼有什麼問題?我看不到下面提到的錯誤的原因。我正在使用Mono,這可能是Mono中的一個錯誤,它會在VStudio中無誤地編譯嗎?沒有使用Mono的泛型類型參數的裝箱或類型參數轉換

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)

+1

我複製你的代碼到一個新的VC#2010項目,改變'/ *做的東西* /'來'客戶端=默認(T);'並用''「'替換兩個設置。編譯好,沒有編譯器錯誤。 – dtb

+0

那麼這可能是Mono中的一個錯誤嗎? – knittl

+0

您正在使用哪個版本的'dmcs'? – dtb

回答

2

TL; DR很可能是在您的版本中的錯誤:它在我的版本單的完美編譯。


下面的代碼編譯完美:

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) 
+1

這確實是單聲道中的一個錯誤。它在[commit'bf9a580867'](https://github.com/mono/mono/commit/bf9a580867cd573a398ed5c3ea5668c57e5b9b9b)中引入,並在[commit'8f96710cc'](https://github.com/mono/mono/commit/8f96710cca52860c47144592b9c82ea2fc4198b7) – knittl

相關問題