2011-11-05 37 views
9

Có gì sai với mã sau? Tôi không thể thấy lý do cho lỗi được đề cập bên dưới. Tôi đang sử dụng Mono, đây có phải là một lỗi trong Mono, và nó sẽ biên dịch mà không có lỗi trong VStudio?Không chuyển đổi thông số quyền anh hoặc loại cho tham số Kiểu chung với 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; 
    } 
} 

Tôi nhận được lỗi biên dịch:

…/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

tôi sao chép mã của bạn để một dự án mới VC# 2010, thay đổi '/ * làm công cụ * /' với 'client = mặc định (T); 'và thay thế hai thiết lập bằng' "" '. Biên dịch tốt, không có lỗi trình biên dịch. – dtb

+0

Đây có phải là lỗi trong Mono không? – knittl

+0

Bạn đang sử dụng phiên bản 'dmcs' nào? – dtb

Trả lời

2

TL; DR nó có thể sẽ là một lỗi trong phiên bản của bạn: nó biên dịch một cách hoàn hảo trên phiên bản của tôi về Mono.


Các mã sau biên dịch một cách hoàn hảo:

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

Điều này thực sự là một lỗi trong mono. Nó được giới thiệu trong [commit 'bf9a580867'] (https://github.com/mono/mono/commit/bf9a580867cd573a398ed5c3ea5668c57e5b9b9b) và được sửa trong [commit' 8f96710cc'] (https://github.com/mono/mono/commit/8f96710cca52860c47144592b9c82ea2fc4198b7) – knittl

Các vấn đề liên quan