c#使用接口的隐式转换(IList)

时间:2019-02-18 13:50:46

标签: c# implicit-conversion

我有隐式转换,用于返回 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

我该如何解决?

0 个答案:

没有答案