简体   繁体   English

如何将具有约束参数类型的 class 的实例引用传递给该约束类型的实例?

[英]How do I pass the reference of an instance from a class with a constrained parameter type, to an instance of that constraint type?

Let's say I have these two classes:假设我有这两个类:

public abstract class ASpawnedObject {}
public abstract class ASpawner<T> where T : ASpawnedObject {}

The ASpawner class creates some ASpawnedObject instances. ASpawner class 创建了一些ASpawnedObject实例。 I want it to pass itself to the ASpawnedObject constructor and keep a reference of it as a member variable:我希望它将自己传递给ASpawnedObject构造函数并将其引用作为成员变量:

public abstract class ASpawnedObject {}
{
    ASpawner<ASpawnedObject> spawner;

    public void Init(ASpawner<ASpawnedObject> spawner)
    {
        this.spawner = spawner;
    }
}
public abstract class ASpawner<T> where T : ASpawnedObject
{
    public void Spawn()
    {
        T spawnedObject = System.Activator.CreateInstance<T>();
        spawnedObject.Init(this);
    }
}

BUT: spawnedObject.Init(this) is causing a conversion error in ASpawner .但是: spawnedObject.Init(this)导致ASpawner中的转换错误。 It can't convert ASpawner<T> to ASpawner<ASpawnedObject> even with the type constraint.即使有类型约束,它也无法将ASpawner<T>转换为ASpawner<ASpawnedObject> I don't get it.我不明白。 Doesn't it follow the polymorphism principles?它不遵循多态性原则吗?

This test class compiles and runs without any exceptions本次测试 class 编译运行无任何异常

[TestClass]
public class Test
{
    [TestMethod]
    public void X()
    {
        var ghostSpawner = new GhostSpawner();
        ghostSpawner.Spawn();
    }
}

What you have to do is constrain your spawner and your spawned object in terms of your spawned object.您需要做的是根据您生成的 object 来限制您的生成器和生成的 object。 Yes, that means for the latter, in terms of itself:是的,就后者而言,这意味着:

public abstract class ASpawnedObject<T> where T : ASpawnedObject<T>
{
    ASpawner<T> spawner;
    public void Init(ASpawner<T> spawner) => this.spawner = spawner;
}

public abstract class ASpawner<T> where T : ASpawnedObject<T>
{
    public void Spawn()
    {
        T spawnedObject = Activator.CreateInstance<T>();
        spawnedObject.Init(this);
    }
}

As you can see, that means the ASpawnedObject base class must be made generic.如您所见,这意味着ASpawnedObject基础 class 必须是通用的。 You can then create non-generic concrete classes that make the test pass.然后,您可以创建使测试通过的非泛型具体类。

public class GhostSpawner : ASpawner<Ghost>
{}

public class Ghost : ASpawnedObject<Ghost>
{}

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

相关问题 如何将抽象类作为参数传递给类型的实例? - How do I pass abstract class as parameter for instance of type? 如何从类型参数创建类实例? - How can I create class instance from type parameter? 如何使用类型列表的参数创建泛型类型的实例 - How do I create an instance of a generic type with a parameter of type list 通用类型约束:如何创建 C# 文档中描述的 class 的实例 - Generic type constraint: How do I create an instance of the class described in this C# documentation 将泛型类型的实例作为参数传递给泛型类的动态实例 - Pass an instance of a generic type as a parameter on a dynamic instance of a generic class 在通用方法中检查非类约束类型参数的实例是否为null - Checking instance of non-class constrained type parameter for null in generic method 从其泛型参数类型的实例创建 class 的实例 - Create an instance of a class from an instance of the type of its generic parameter 如何使用受约束的泛型类型参数将 class 实例化为派生接口 - How to instantiate a class as the interface that it derives from with constrained generic type parameter 如何获取类实例的类型(不是类,而是类) - How do I get the type (not of a class but that) of an instance of class 如何创建通用类型类的新实例 - How do I create new instance of Generic Type Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM