简体   繁体   English

通用对象C#

[英]Generic Objects C#

So I have been working with the Neo4jClient library for C# and I am fairly new to both worlds. 所以我一直在使用Neo4jClient库来处理C#,我对这两个世界都很陌生。

I have this POCO here: 我在这里有这个POCO:

public class SetEntity
{
    public string GUID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string CreatedDate { get; set; }
}

This object class is used in various methods, one in particular for creating a relationship between two nodes however I have to explicitly say which POCO is used to create it IRelationshipAllowingSourceNode<SetEntity> and IRelationshipAllowingTargetNode<EntityInstance> . 此对象类用于各种方法,特别是用于在两个节点之间创建关系,但是我必须明确说明使用哪个POCO来创建它IRelationshipAllowingSourceNode<SetEntity>IRelationshipAllowingTargetNode<EntityInstance> Below is the entire class that handles that. 下面是处理它的整个类。

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; }
        }
    }

Is there a way that I can pass <SetEntity> or any other objects into IRelationshipAllowingSourceNode<Object> . 有没有办法可以将<SetEntity>或任何其他对象传递给IRelationshipAllowingSourceNode<Object> I see it as unnecessary to create this class for every node type that will have a relationship with another node type. 我认为没有必要为每个与另一个节点类型有关系的节点类型创建这个类。

I'm not familiar with the Neo4jclient but can comment on generics in c#. 我不熟悉Neo4jclient,但可以在c#中评论泛型。

In c# you can define an interface with which is said to have an open generic type. 在c#中,您可以定义一个接口,据说该接口具有开放的泛型类型。 That is, the neo4jclient presumably declares an interface IRelationshipAllowingSourceNode<T> with some method on which presumably use an instance of T/returns T. 也就是说,neo4jclient可能会声明一个接口IRelationshipAllowingSourceNode<T> ,其中某个方法可能使用T /返回T的实例。

This is said to be an interface with an open generic type. 据说这是一个开放泛型类型的接口。

When you implement that interface you have close the open generic type by specifying the exact type you're working with. 实现该接口时,您可以通过指定正在使用的确切类型来关闭打开的泛型类型。 You can however, make your class use two open generic types as follows, and then close the generic types when you instantiate GraphRelationshipEntityInstanceToSetEntity. 但是,您可以使您的类使用两个打开的泛型类型,如下所示,然后在实例化GraphRelationshipEntityInstanceToSetEntity时关闭泛型类型。 See below. 见下文。

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; }
        }
    }

See here for another question which has been answered on generics: 请参阅此处以获取有关泛型的其他问题:

Generics -Open and closed constructed Types 泛型 - 打开和关闭构造的类型

Hope that this helps. 希望这会有所帮助。

Tim 蒂姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM