接口无法解析参数类型

时间:2013-04-01 06:00:33

标签: c#

我有以下课程:

namespace CommonInterfaces
{
    public interface INode
    {
        int? ParentNodeId { get; set; }
        int? ChildNodeId { get; set; }
    }
}

public class FieldDependencyModel : DataModel<int>, ICacheable
{

    #region Field

    private int _FieldDependencyId;
    private int? _FieldId;
    private int? _FieldParentId;

    #endregion  

    #region Properties

    public virtual int FieldDependencyId
    {
        get { return getPropertyValue("FieldDependencyId",_FieldDependencyId); }
        set { _FieldDependencyId = value; }
    }

    public virtual int? ChildNodeId
    {
        get { return getPropertyValue("FieldId",_FieldId); }
        set { _FieldId = value; }
    }

    public virtual int? ParentNodeId
    {
        get { return getPropertyValue("FieldParentId",_FieldParentId); }
        set { _FieldParentId = value; }
    }

    #endregion
}


    public interface IFieldDependencyService
    {
        IList<FieldDependency> GetAllFieldDependencies();
    }

    public class FieldDependency : FieldDependencyModel, INode
    {
    }


    public class FieldDependencyDataService : BaseDataService, IFieldDependencyService
    {
        public FieldDependencyDataService(DataAccessHandle serviceHandler) : base(serviceHandler) {}

        public IList<FieldDependency> GetAllFieldDependencies()
        {
            return _accessService.loadAll<FieldDependency>();
        }
    }

    public class TreeService
    {
        private IList<INode> _relationships;
        private INode _rootNode;    


        public TreeService(INode rootNode, IList<INode> nodeRelationships)
        {
            _relationships = nodeRelationships;
            _rootNode = rootNode;
        }


        public Tree<INode> BuildTree()
        {
            Tree<INode> tree;

            tree = new Tree<INode>(_rootNode, _relationships);

            return tree;
        }
    }

我尝试发送一个暗示 INode 的类型,当我尝试将fieldDependencies传递给新的TreeService实例时,它仍然会显示以下错误:

Error   5   Argument 2: cannot convert from 'System.Collections.Generic.IList<FieldDependency>' to 'System.Collections.Generic.IList<CommonInterfaces.INode>'   

这是我的调用代码,我收到此错误:

            IList<FieldDependency> fieldDependencies = _service.GetAllFieldDependencies();

            FieldDependency rootDependency = fieldDependencies.First(d => d.ParentNodeId == null);
 var service = new TreeService(rootDependency, fieldDependencies); // fails here, doesn't like fieldDependencies

4 个答案:

答案 0 :(得分:1)

试试这个

IList<INode> fieldDependencies = _service.GetAllFieldDependencies()
.First(d => d.ParentNodeId == null).Select(d => (INode)d);

答案 1 :(得分:1)

您不能这样做,因为您尝试将IList<FieldDependency>转换为IList<INode>,因此您尝试转换的实际对象是IList相关类型作为其泛型类型。您可以采取一些行动来解决这个问题。

IList<FieldDependency>替换为IList<INode>

您可以更改代码,以便IList<FieldDependency>IList<INode>

public interface IDataFieldDependencyService
{
    IList<INode> GetAllFieldDependencies();
}

public class FieldDependencyDataService : BaseDataService, IFieldDependencyService
{
    public FieldDependencyDataService(DataAccessHandle serviceHandler) : base(serviceHandler) {}

    public IList<INode> GetAllFieldDependencies()
    {
        return _accessService.loadAll<FieldDependency>();
    }
}

用法

IList<INode> fieldDependencies = _service.GetAllFieldDependencies();

INode rootDependency = fieldDependencies.First(d => d.ParentNodeId == null);
var service = new TreeService(rootDependency, fieldDependencies);

Select并投射为INode

您还可以选择_service.GetAllFieldDependencies()中的所有元素,并将其列入INode

IList<INode> fieldDependencies = _service.GetAllFieldDependencies()
    .Select(e => (INode)e)
    .ToList();

INode rootDependency = fieldDependencies.First(d => d.ParentNodeId == null);
var service = new TreeService(rootDependency, fieldDependencies);

协方差

如果你使用IEnumerable s,那么你可以做你想做的事情using covariance(我认为是.NET 4)。

public class TreeService
{
    private IEnumerable<INode> _relationships;
    private INode _rootNode;    


    public TreeService(INode rootNode, IEnumerable<INode> nodeRelationships)
    {
        _relationships = nodeRelationships;
        _rootNode = rootNode;
    }


    public Tree<INode> BuildTree()
    {
        Tree<INode> tree;

        tree = new Tree<INode>(_rootNode, _relationships);

        return tree;
    }
}

用法

IEnumerable<INode> fieldDependencies = _service.GetAllFieldDependencies();

INode rootDependency = fieldDependencies.First(d => d.ParentNodeId == null);
var service = new TreeService(rootDependency, fieldDependencies);

答案 2 :(得分:0)

您已将字段依赖关系定义为FieldDependency列表,即

fieldDependencies.GetType() == typeof<IList<FieldDependency>>

TreeService构造函数的第二个参数需要tyepof(IList<INode>>)

您应该只能将变量fieldDependencies的定义方式更改为IList<INode>

IList<INode> fieldDependencies = _service.GetAllFieldDependencies()

答案 3 :(得分:0)

尝试

var service = new TreeService(rootDependency, fieldDependencies.Cast<INode>().ToList());

从类A派生的类B不保证您能够将类型参数B的对象泛型集合类型转换为类型参数为A的泛型集合类型。

您可以阅读有关逆变herehere的更多信息。