不能为我的泛型方法使用接口
Cannot use interface for my generic method
我有一小段代码正在遍历列表以检查重叠:
private List<INode> _nodes = new List<INode>();
private List<ISegment> _segments = new List<ISegment>();
public IReadOnlyList<INode> Nodes => _nodes;
public IReadOnlyList<ISegment> Segments => _segments;
private bool Overlaps<T>(ref Vector3 point, in IReadOnlyList<T> collection, out T obj) where T : INode, ISegment
{
obj = default;
for (int i = 0; i < collection.Count; i++)
{
if (collection[i].Overlaps(ref point))
return true;
}
return false;
}
public bool Overlaps(ref Vector3 point, out INode node){
return Overlaps(ref point, _nodes, out node);
}
public bool Overlaps(ref Vector3 point, out ISegment segment){
return Overlaps(ref point, _segments, out segment);
}
通用方法只能接受两种类型,INode 或 ISegment,这是 where
子句的用途,但我收到此错误:
The type 'Graphs.INode' cannot be used as type parameter 'T' in the generic type or
method 'Graph.Overlaps<T>(ref Vector3, in IReadOnlyList<T>, out T)'. There is no
implicit reference conversion from 'Graphs.INode' to 'Graphs.ISegment'.
不确定我是否理解它认为我正在转换的原因,我在这里使用的 where
关键字有误吗?不确定如何让它工作。
接口定义:
public interface INode{
bool Overlaps(ref Vector3 point);
}
public interface ISegment{
bool Overlaps(ref Vector3 point);
}
where
关键字表示您的泛型类型必须实现 INode
AND ISegment
.
INode 和 ISegments 似乎具有相同的契约,您可以基于此构建接口继承。
public interface INode{
bool Overlaps(ref Vector3 point);
}
public interface ISegment : INode { }
//OR
public interface ISegment {
bool Overlaps(ref Vector3 point);
}
public interface INode : ISegment { }
更新
更好的方法是使用公共共享接口
public interface IOverlaps {
bool Overlaps(ref Vector3 point);
}
public interface INode : IOverlaps { }
public interface ISegment : IOverlaps { }
... where T : IOverlaps { ... }
我有一小段代码正在遍历列表以检查重叠:
private List<INode> _nodes = new List<INode>();
private List<ISegment> _segments = new List<ISegment>();
public IReadOnlyList<INode> Nodes => _nodes;
public IReadOnlyList<ISegment> Segments => _segments;
private bool Overlaps<T>(ref Vector3 point, in IReadOnlyList<T> collection, out T obj) where T : INode, ISegment
{
obj = default;
for (int i = 0; i < collection.Count; i++)
{
if (collection[i].Overlaps(ref point))
return true;
}
return false;
}
public bool Overlaps(ref Vector3 point, out INode node){
return Overlaps(ref point, _nodes, out node);
}
public bool Overlaps(ref Vector3 point, out ISegment segment){
return Overlaps(ref point, _segments, out segment);
}
通用方法只能接受两种类型,INode 或 ISegment,这是 where
子句的用途,但我收到此错误:
The type 'Graphs.INode' cannot be used as type parameter 'T' in the generic type or
method 'Graph.Overlaps<T>(ref Vector3, in IReadOnlyList<T>, out T)'. There is no
implicit reference conversion from 'Graphs.INode' to 'Graphs.ISegment'.
不确定我是否理解它认为我正在转换的原因,我在这里使用的 where
关键字有误吗?不确定如何让它工作。
接口定义:
public interface INode{
bool Overlaps(ref Vector3 point);
}
public interface ISegment{
bool Overlaps(ref Vector3 point);
}
where
关键字表示您的泛型类型必须实现 INode
AND ISegment
.
INode 和 ISegments 似乎具有相同的契约,您可以基于此构建接口继承。
public interface INode{
bool Overlaps(ref Vector3 point);
}
public interface ISegment : INode { }
//OR
public interface ISegment {
bool Overlaps(ref Vector3 point);
}
public interface INode : ISegment { }
更新
更好的方法是使用公共共享接口
public interface IOverlaps {
bool Overlaps(ref Vector3 point);
}
public interface INode : IOverlaps { }
public interface ISegment : IOverlaps { }
... where T : IOverlaps { ... }