[英]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. 在我看来,由于
ApprenticePage
从HomePage
继承的,而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>
ApprenticePage
是BasePage<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.