You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建可容纳不同泛型WorldObjectsProvider的数组问题咨询

Can I create an array of WorldObjectsProvider<T> with different generic types?

Absolutely doable! Let’s break down how to make this work, since different generic instances of WorldObjectsProvider<T> (where T : WorldObject) are technically distinct runtime types. We just need a non-generic "common denominator" interface or abstract class to unify all these providers into a single type our array can hold.

Step 1: Define a non-generic interface/base class

First, create a non-generic interface that declares the core functionality you need from all providers. Since all your generic providers create objects inheriting from WorldObject, we can define a method that returns the base WorldObject type:

// Your base WorldObject class
public class WorldObject { }

// The specific type you want to create
public class WorldObjectType : WorldObject { }

// Non-generic interface to unify all providers
public interface IWorldObjectsProvider
{
    WorldObject CreateWorldObject();
}

Step 2: Make your generic class implement the interface

Update your WorldObjectsProvider<T> to implement this non-generic interface. We’ll add the new() constraint here to simplify creating instances (you can adjust this if you use a different instantiation method):

public class WorldObjectsProvider<T> : IWorldObjectsProvider 
    where T : WorldObject, new()
{
    // Implement the non-generic interface method
    public WorldObject CreateWorldObject()
    {
        return new T();
    }

    // Optional: Keep a generic method for when you need the specific type
    public T CreateSpecificWorldObject()
    {
        return new T();
    }
}

Step 3: Create and use the array

Now you can create an array of IWorldObjectsProvider and populate it with any WorldObjectsProvider<T> instance where T inherits from WorldObject:

// Example concrete subtypes of WorldObjectType
public class Player : WorldObjectType { }
public class Enemy : WorldObjectType { }

// Usage
var playerProvider = new WorldObjectsProvider<Player>();
var enemyProvider = new WorldObjectsProvider<Enemy>();
var defaultProvider = new WorldObjectsProvider<WorldObjectType>();

// Array of the non-generic interface type
IWorldObjectsProvider[] providers = { playerProvider, enemyProvider, defaultProvider };

// Iterate through the array and use each provider
foreach (var provider in providers)
{
    WorldObject obj = provider.CreateWorldObject();
    Console.WriteLine($"Created object of type: {obj.GetType().Name}");
}

Bonus: Accessing specific generic types later

If you need to work with the specific generic type of a provider later, you can use pattern matching to cast back:

foreach (var provider in providers)
{
    if (provider is WorldObjectsProvider<Player> playerProv)
    {
        Player player = playerProv.CreateSpecificWorldObject();
        // Handle player-specific logic here
    }
}

This approach works because all your generic providers share the non-generic IWorldObjectsProvider type, allowing them to coexist in the same array while still retaining their individual type-specific functionality.

内容的提问来源于stack exchange,提问作者Damian Dudycz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:22:21