C# 通用对象#
因此,我一直在为C#使用Neo4jClient库,我对这两个领域都相当陌生 我这里有一个POCO:C# 通用对象#,c#,methods,neo4jclient,C#,Methods,Neo4jclient,因此,我一直在为C#使用Neo4jClient库,我对这两个领域都相当陌生 我这里有一个POCO: public class SetEntity { public string GUID { get; set; } public string Name { get; set; } public string Type { get; set; } public string CreatedDate { get; set; } } 此对象类用于各种方法,其中一种方法
public class SetEntity
{
public string GUID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string CreatedDate { get; set; }
}
此对象类用于各种方法,其中一种方法特别用于创建两个节点之间的关系,但是我必须明确说明使用哪个POCO创建它IRelationshipAllowingSourceNode
和IRelationshipAllowingTargetNode
。下面是处理该问题的整个类
class GraphRelationshipEntityInstanceToSetEntity : Relationship, IRelationshipAllowingSourceNode<EntityInstance>, IRelationshipAllowingTargetNode<SetEntity>
{
string RelationshipName;
public GraphRelationshipEntityInstanceToSetEntity(NodeReference targetNode)
: base(targetNode)
{
}
public GraphRelationshipEntityInstanceToSetEntity(string RelationshipName, NodeReference targetNode)
: base(targetNode)
{
this.RelationshipName = RelationshipName;
}
public override string RelationshipTypeKey
{
get { return RelationshipName; }
}
}
class GraphRelationshipEntityInstanceToTentity:关系、IRRelationshipAllowingSourceNode、IRRelationshipAllowingTargetNode
{
字符串关系名称;
公共GraphRelationshipEntityInstanceToSetEntity(节点引用目标节点)
:base(targetNode)
{
}
公共GraphRelationshipEntityInstanceToSetEntity(字符串关系ShipName、节点引用targetNode)
:base(targetNode)
{
this.RelationshipName=RelationshipName;
}
公共重写字符串RelationshipTypeKey
{
获取{return RelationshipName;}
}
}
是否有一种方法可以将
或任何其他对象传递到IRelationshipAllowingSourceNode
。我认为没有必要为每个与另一个节点类型有关系的节点类型创建此类。我不熟悉Neo4jclient,但可以在c#中对泛型进行注释
在c#中,您可以定义一个接口,该接口被称为具有开放泛型类型。也就是说,neo4jclient可能声明了一个接口IRelationshipAllowingSourceNode
,其中使用了一个T/returns T的实例
这被称为具有开放泛型类型的接口
当您实现该接口时,您已经通过指定正在使用的确切类型来关闭打开的泛型类型。但是,可以让类使用两个打开的泛型类型,如下所示,然后在实例化GraphRelationshipEntityInstanceToSetEntity时关闭泛型类型。见下文
class GraphRelationshipEntityInstanceToSetEntity<T, T1> : Relationship, IRelationshipAllowingSourceNode<T>, IRelationshipAllowingTargetNode<T1>
{
string RelationshipName;
public GraphRelationshipEntityInstanceToSetEntity(NodeReference targetNode)
: base(targetNode)
{
}
public GraphRelationshipEntityInstanceToSetEntity(string RelationshipName, NodeReference targetNode)
: base(targetNode)
{
this.RelationshipName = RelationshipName;
}
public override string RelationshipTypeKey
{
get { return RelationshipName; }
}
}
class GraphRelationshipEntityInstanceToTentity:关系、IRRelationshipAllowingSourceNode、IRRelationshipAllowingTargetNode
{
字符串关系名称;
公共GraphRelationshipEntityInstanceToSetEntity(节点引用目标节点)
:base(targetNode)
{
}
公共GraphRelationshipEntityInstanceToSetEntity(字符串关系ShipName、节点引用targetNode)
:base(targetNode)
{
this.RelationshipName=RelationshipName;
}
公共重写字符串RelationshipTypeKey
{
获取{return RelationshipName;}
}
}
有关泛型的另一个问题,请参见此处:
希望这有帮助
蒂姆谢谢@Tim,真是帮了大忙!