简体   繁体   English

从具有类型约束的类继承-“不存在来自…的隐式引用转换”

[英]Inheriting from class with type constraints - “There is no implicit reference conversion from…”

I have a base class for databinding a single datasource like this: 我有一个用于对单个数据源进行数据绑定的基类,如下所示:

public abstract class DataControlBase<TContainer, TDataType> : Control
    where TDataType : new()
    where TContainer : TemplateContainerBase<TDataType>, new()
{
    public TDataType DataSource { get; set; }
}

The container class (TContainer) will always inherit from the container base: 容器类(TContainer)将始终从容器基类继承:

public abstract class TemplateContainerBase<TDataType> : Control
    where TDataType : new()
{
    public TemplateContainerBase() { }

    public TDataType DataItem { get; set; }
}

Finally I have a repeater base where I will bind several data items as the datasource: 最终,我有了一个转发器基础,我将在其中绑定几个数据项作为数据源:

public abstract class RepeaterBase<TContainer, TDataType> : DataControlBase<TContainer, List<TDataType>>
    where TDataType : new()
    where TContainer : TemplateContainerBase<TDataType>, new()
{

}

I get an error on the RepeaterBase saying this 我在RepeaterBase上收到一个错误,说这

Error 1 The type 'TContainer' cannot be used as type parameter 'TContainer' in the generic type or method 'WebTestHarness.Controls.DataControlBase'. 错误1类型'TContainer'不能用作通用类型或方法'WebTestHarness.Controls.DataControlBase'中的类型参数'TContainer'。 There is no implicit reference conversion from 'TContainer' to 'WebTestHarness.Controls.TemplateContainerBase>'. 没有从'TContainer'到'WebTestHarness.Controls.TemplateContainerBase>'的隐式引用转换。

I have read a few different threads describing similar issues but all seem to relate to methods whereas this is solely to do with class design. 我已经阅读了一些描述类似问题的不同线程,但似乎都与方法有关,而这仅与类设计有关。

You have 你有

RepeaterBase<TContainer, TDataType> : DataControlBase<TContainer, List<TDataType>>

so you've set List<TDataType> as the second type parameter Y for DataControlBase<X, Y> . 因此,您已将List<TDataType>设置为DataControlBase<X, Y>的第二个类型参数Y But your constraint for DataControlBase requires that X : TemplateContainerBase<Y>, new() . 但是您对DataControlBase的约束要求X : TemplateContainerBase<Y>, new() So, in your case, you would need the constraint 因此,在您的情况下,您将需要约束

where TContainer : TemplateContainerBase<List<DataType>>, new()

on TContainer , not TContainer ,不是

where TContainer : TemplateContainerBase<DataType>, new()

as you currently have. 如您目前所拥有的。

That is, just because you've used the same names for the type parameters in the varying classes does not mean that they represent the same types when you start parameterizing things. 也就是说,仅仅因为您在不同的类中为类型参数使用了相同的名称 ,并不意味着在开始对事物进行参数化时它们表示相同的类型。 That is 那是

class Foo<A> { }
class Bar<A> : Foo<List<A>> { }

The A in Bar is not the same as the A in Foo . ABar 一样的AFoo Instead, List<A> is playing the role of A for Foo<A> in the definition of Bar<A> . 相反,在Bar<A>的定义中, List<A>Foo<A>中扮演A的角色。

暂无
暂无

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

相关问题 使用继承接口的类型约束会导致隐式引用转换错误 - Using type constraints from inherited interface gives implicit reference conversion error 没有从“类型”到“接口”的隐式引用转换 - There is no implicit reference conversion from 'Type' to 'Interface' 用户定义类型从引用类型到接口的隐式引用转换 - Implicit Reference Conversion from reference type to interface for user defined types 没有从“type1”到“type2”的隐式引用转换 - There is no implicit reference conversion from 'type1' to 'type2' 在泛型中没有从“派生类型”到“基本类型”的隐式引用转换 - There is no implicit reference conversion from “Derive Type” to “Base type” in Generics c#类型推断 - 错误 - 没有隐式引用转换 - c# Type Inference - Error - There is no implicit reference conversion from 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 从ValueType继承引用类型 - Inheriting Reference Type from ValueType 没有从ApplicationUser到IdentityUser的隐式引用转换 - There is no implicit reference conversion from ApplicationUser to IdentityUser 类型&#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 ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM