构建可容纳不同泛型WorldObjectsProvider的数组问题咨询
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

