首页 > 解决方案 > c# 使用接口(IList)进行隐式转换

问题描述

我有返回DomainResult的隐式转换

public static implicit operator DomainResult<TEntity>(TEntity resultData)
{
    var result = new DomainResult<TEntity>
    {
        Result = resultData,
        Succeeded = true
    };

    return result;
}

当我尝试通过 使用隐式转换时IList,它给了我编译时错误说明

Cannot implicitly convert type 'System.Collections.Generic.IList<string>' to
'...DomainResult<System.Collections.Generic.IEnumerable<string>>'.
   An explicit conversion exists (are you missing a cast?)

.

        IList<string> str = new PagedList<string>() { "example" };
        DomainResult<IEnumerable<string>> dr = str; // I am getting compile time error

        List<string> str = new PagedList<string>() { "example" };
        DomainResult<IEnumerable<string>> dr = str; //this is valid

我怎样才能解决这个问题?

标签: c#implicit-conversion

解决方案


推荐阅读