简体   繁体   English

尝试使用通用扩展方法时没有隐式引用转换

[英]No implicit reference conversion when attempting to use generic extension method

Even having browsed stack overflow for similar answers I'm struggling to understand the concepts behind why I'm experiencing issues. 即使已经浏览了堆栈溢出以获取类似的答案,我仍在努力理解为什么遇到问题的概念。

My code is below: 我的代码如下:

class Program {
    static void Main(string[] args) {
        HomePage page = new HomePage();
        page.Load(); //No Error
        LoginPage page2 = new LoginPage();
        page2.Load(); //No Error
        ApprenticePage page3 = new ApprenticePage();
        page3.Load(); //No implicit reference conversion
        ApprenticeEPA page4 = new ApprenticeEPA();
        page4.Load(); //No implicit reference conversion
        Console.ReadLine();
    }
}

public abstract class BasePage<T> where T : BasePage<T> {
    public abstract bool EvaluateLoad();
}

public class HomePage : BasePage<HomePage>
{
    public override bool EvaluateLoad()
    {
        Console.WriteLine("In HomePage");
        return true;
    } 
}

public class LoginPage : BasePage<LoginPage>
{
    public override bool EvaluateLoad()
    {
        Console.WriteLine("In LoginPage");
        return true;
    }
}

public class ApprenticePage : HomePage
{
    public override bool EvaluateLoad()
    {
        Console.WriteLine("In ApprenticePage");
        return false;
    }
}

public class ApprenticeEPA : ApprenticePage
{
    public override bool EvaluateLoad()
    {
        Console.WriteLine("In ApprenticeEPA");
        return false;
    }
}

public static class Helper {
    public static T Load<T>(this T page) where T : BasePage<T> {
        if (page.EvaluateLoad()) {
            Console.WriteLine("It's true!");
            return page;
        } else {
            Console.WriteLine("It's false!");
            return default(T);
        }        
    }
}

I'm getting the following error: 我收到以下错误:

The type 'IndexerTest.ApprenticePage' cannot be used as type parameter 'T' in the generic type or method 'Helper.Load(T)'. 类型'IndexerTest.ApprenticePage'不能在通用类型或方法'Helper.Load(T)'中用作类型参数'T'。 There is no implicit reference conversion from 'IndexerTest.ApprenticePage' to 'IndexerTest.BasePage<IndexerTest.ApprenticePage>'. 从'IndexerTest.ApprenticePage'到'IndexerTest.BasePage <IndexerTest.ApprenticePage>'没有隐式引用转换。

In my mind, as ApprenticePage inherits from HomePage , which inherits from BasePage<T> , using the load method would be okay but obviously it isn't. 在我看来,由于ApprenticePageHomePage继承的,而HomePage是从BasePage<T>继承的,所以使用load方法可以,但显然不是。

How would I fix my Load method so that it could be called on all instances of the classes in my code with that classes specific implementation of EvaluateLoad() ? 我将如何修复Load方法,以便可以使用特定于类的EvaluateLoad()实现在代码中的类的所有实例上调用它?

If you examine the error closely, it says the following: 如果您仔细检查该错误,则会显示以下内容:

There is no implicit reference conversion from 'ApprenticePage' to 'BasePage<ApprenticePage>' 从“ ApprenticePage”到“ BasePage <ApprenticePage>”没有隐式引用转换

This is correct; 这是对的; ApprenticePage is a BasePage<HomePage> , it's not a BasePage<ApprenticePage> ApprenticePageBasePage<HomePage> ,不是BasePage<ApprenticePage>

What you're trying to do is create an extension method which works on anything that implements BaseType<T> . 您要尝试做的是创建一个扩展方法,该方法可用于实现BaseType<T> So, your extension method should look like this: 因此,您的扩展方法应如下所示:

public static BasePage<T> Load<T>(this BasePage<T> page) where T : BasePage<T>
{
    if (page.EvaluateLoad())
    {
        Console.WriteLine("It's true!");
        return page;
    }
    else
    {
        Console.WriteLine("It's false!");
        return null; // No need for default() here - default() of a reference type is
                     // always going to be null
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "调用扩展方法时无法进行隐式转换" - Implicit conversion when calling an extension method not possible 具有泛型类的泛型函数:无隐式引用转换 - Generic function with generic class: no implicit reference conversion 类型&#39;&#39;不能用作通用类型或方法&#39;&#39;中的类型参数&#39;&#39;。 没有从“到”的隐式引用转换 - The type '' cannot be used as type parameter '' in the generic type or method ''. There is no implicit reference conversion from '' to '' 类型 &#39;&#39; 不能用作泛型类型或方法 &#39;&#39; 中的类型参数 &#39;T&#39;。 没有从 &#39;&#39; 到 &#39;&#39; 的隐式引用转换 - The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to '' 该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换 - The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to 该类型不能用作泛型类型或方法中的类型参数“TTo”。 没有隐式引用转换 - The type cannot be used as type parameter 'TTo' in the generic type or method. There is no implicit reference conversion 通用存储库和通用DbContext中没有隐式引用转换错误 - no implicit reference conversion error in a generic repository and generic DbContext 没有来自……通用查询处理程序cqrs的隐式引用转换。 - There is no implicit reference conversion from… generic query handler cqrs C# 通用抽象类 - 没有隐式引用转换 - C# Generic Abstract Class - There is no implicit reference conversion 通用类型:没有从ToolStripStatusLabel到Control的隐式引用转换 - Generic Types: There is no implicit reference conversion from ToolStripStatusLabel to Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM