Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No boxing or type parameter conversion for generic Type parameter with Mono

What's wrong with the following code? I cannot see the reason for the error mentioned below. I'm using Mono, could this be a bug in Mono, and will it compile without errors in 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;
  } 
}

I'm getting the compile error:

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

like image 242
knittl Avatar asked Nov 05 '11 12:11

knittl


1 Answers

TL;DR it's likely to be a bug in your version: it compiles perfectly on my version of Mono.


The following code compiles perfectly:

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)
like image 143
Sklivvz Avatar answered Oct 08 '22 01:10

Sklivvz