不能将类型'XXX'隐式转换为'T' [英] Cannot implicitly convert type 'XXX' to 'T'

查看:102
本文介绍了不能将类型'XXX'隐式转换为'T'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用工厂模式来使用泛型来获取类的实例。不知道我错过了下面的代码。

 命名空间ConsoleApplication1 
{
public interface IResult
{
}

public class ResultA:IResult
{
public string P1 {get;组; }
}

public interface IService< T> T:IResult
{
T DoWork();
}

public class ServiceA< T> :IService< T>其中T:ResultA,IResult,new()
{
public T DoWork()
{
var t = new T();
t.P1 =value1;
return t;



public interface IFactory
{
T GetService< T>(string documentType)其中T:IService< IResult> ;;
}

public class MyFactory:IFactory
{
public T GetService< T>(string documentType)其中T:IService< IResult>
{
//基于documenet类型I将返回此处的服务实例
//我在此行发生错误
返回新的ServiceA< ResultA>(); } b




$ blockquote

错误1无法将类型
'ConsoleApplication1.ServiceA'隐式转换为
'T'


Update1

这也不起作用

  public interface IFactory 
{
IService< IResult> GetService(string documentType);
}

public class MyFactory:IFactory
{
public IService< IResult> GetService(string documentType)
{
返回新的ServiceA< ResultA>();



$解析方案

你知道在每一个IServiceA中,T都实现了IResult,你不需要制作你的Factory Generic,而只需要返回IService(如前面的答案所示)。但是,为了编译代码,您需要在MyFactory.GetService中投入返回值,如下所示:

  ... 
public interface IFactory
{
IService< IResult> GetService(string documentType);
}

public class MyFactory:IFactory
{
public IService< IResult> GetService(字符串文档类型)
{
//演示需要解决错误...
return(IService< IResult>)new ServiceA< ResultA>();


$ / code $ / pre

这段代码应该等同于你想要的做,并应该正确编译。


I trying to use factory pattern to get instance of a class using generics. Not sure what I'm missing the code below.

namespace ConsoleApplication1
{
    public interface IResult
    {
    }

    public class ResultA : IResult
    {
        public string P1 { get; set; }
    }

    public interface IService<T> where T : IResult
    {
        T DoWork();
    }

    public class ServiceA<T> : IService<T> where T : ResultA, IResult, new()
    {
        public T DoWork()
        {
            var t = new T();
            t.P1 = "value1";
            return t;
        }
    }

    public interface IFactory
    {
        T GetService<T>(string documentType) where T : IService<IResult>;
    }

    public class MyFactory : IFactory
    {
        public T GetService<T>(string documentType) where T : IService<IResult>
        {
            // based on documenet type I will be returning the instances of service here
            // im getting error at this line
            return new ServiceA<ResultA>();                }
    }

}

Error 1 Cannot implicitly convert type 'ConsoleApplication1.ServiceA' to 'T'

Update1
this didn't work either

public interface IFactory
{
    IService<IResult> GetService(string documentType);
}

public class MyFactory : IFactory
{
    public IService<IResult> GetService(string documentType)
    {
        return new ServiceA<ResultA>();
    }
}

解决方案

Given that you know that in every IServiceA, T implements IResult, you don't need to make your Factory Generic, but just bound it to return IService (as indicated by the prev. answer). But, in order to make the code compile, you'll need to cast the return in MyFactory.GetService, as such:

    ...
    public interface IFactory
    {
        IService<IResult> GetService(string documentType);
    }

    public class MyFactory : IFactory
    {
        public IService<IResult> GetService(string documentType)
        {
            //Cast needed to address the error...
            return (IService<IResult>) new ServiceA<ResultA>();
        }
    }

This code should be equivalent to what you are trying to do, and should compile correctly.

这篇关于不能将类型'XXX'隐式转换为'T'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆