类型约束C#Generic的无效转换

Bre*_*ias 7 .net c# generics

针对.net 4.0,我正在尝试构建以下类:

public class ConfigurationElementCollection<TElement, TParent> 
    where TElement : ParentedConfigElement<TParent>, new() 
    where TParent : class
{
    public TParent ParentElement { get; set; }

    protected ConfigurationElement CreateNewElement()
    {
        //**************************************************
        //COMPILER GIVES TYPE CONVERSION ERROR ON THIS LINE!
        //**************************************************
        return new TElement { ParentCollection = this };
    }
}

public class ParentedConfigElement<TParent> : ConfigurationElement where TParent : class
{
    internal ConfigurationElementCollection<ParentedConfigElement<TParent>, TParent> 
        ParentCollection { get; set; }

    protected TParent Parent
    {
        get
        {
            return ParentCollection != null ? ParentCollection.ParentElement : null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如上面的代码注释所示,编译器给出了一个错误:

Cannot implicitly convert type 'Shared.Configuration.ConfigurationElementCollection<TElement, TParent>' to 'Shared.Configuration.ConfigurationElementCollection<Shared.Configuration.ParentedConfigElement<TParent>,TParent>

我不指望这个错误,因为编译器也知道这TElementShared.Configuration.ParentedConfigElement<TParent>由于我指定的泛型类型约束.

不过,我想我会明确地说出类型来解决这个问题:

(ConfigurationElementCollection<ParentedConfigElement<TParent>,TParent>) this;
Run Code Online (Sandbox Code Playgroud)

不幸的是,我得到了相同的编译器错误.为什么会这样?我做错了什么?如果不采用dynamic类型,我该怎么做才能解决这个问题?

Eri*_*rik 2

您的问题是您有一个类型CEC<A, B>,并且尝试将其分配给您无法执行的类型的属性,这与即使派生自 也无法分配给类型的存储CEC<C<A>,B>大致相同。List<string>List<object>stringobject

不使用隐式运算符或动态的干净解决方案是使用接口:

public interface IConfigurationElementCollection<TParentedConfig, TParent> 
    where TParentedConfig : ParentedConfigElement<TParent>
    where TParent : class
{
    TParent ParentElement { get; }
}

public class ConfigurationElementCollection<TElement, TParent> : IConfigurationElementCollection<ParentedConfigElement<TParent>, TParent>
    where TElement : ParentedConfigElement<TParent>, new() 
    where TParent : class
{
    public TParent ParentElement { get; set; }

    protected ConfigurationElement CreateNewElement()
    {
        //**************************************************
        //COMPILER NO LONGER GIVES TYPE CONVERSION ERROR 
        //BECAUSE this IMPLEMENTS THE EXPECTED INTERFACE!
        //**************************************************
        return new TElement { ParentCollection = this };
    }
}

public class ParentedConfigElement<TParent> : ConfigurationElement where TParent : class
{
    internal IConfigurationElementCollection<ParentedConfigElement<TParent>, TParent> 
        ParentCollection { get; set; }

    protected TParent Parent
    {
        get
        {
            return ParentCollection != null ? ParentCollection.ParentElement : null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于 in 的属性ParentedConfigElement是内部的,因此您还可以将接口设置为内部,以避免将此实现细节暴露给任何使用者(如果您担心这类事情)。