我有以下课程:
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
答案 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)