© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
26 replies
sp00ktober

❔ nested generic interface throws cast exception when added to list

Im working on a mod project and for this i need to handle different updates which are represented by individual classes that all inherit from the same generic interfaces.
To accomplish the handling i also wrote a generic interface which can then be implemented by a class that should handle one specific class.
In the end i want to have a list of handlers over which i can iterate to process the updates.

This is my interface signature
internal interface IComponentUpdateHandler<T, H, C> where T : IComponentMetaclass where H : IComponentUpdate<T> where C : IComponentData<T>
internal interface IComponentUpdateHandler<T, H, C> where T : IComponentMetaclass where H : IComponentUpdate<T> where C : IComponentData<T>

These are the interfaces for the update and data interfaces
public interface IComponentUpdate<C> where C : IComponentMetaclass
public interface IComponentData<C> where C : IComponentMetaclass
public interface IComponentUpdate<C> where C : IComponentMetaclass
public interface IComponentData<C> where C : IComponentMetaclass

This is the signature of one of my handler classes
internal class InventoryModificationState_Handler : IComponentUpdateHandler<InventoryModificationState, InventoryModificationState.Update, InventoryModificationState.Data>
internal class InventoryModificationState_Handler : IComponentUpdateHandler<InventoryModificationState, InventoryModificationState.Update, InventoryModificationState.Data>

These are the signatures of the update and data classes
public class Update : IComponentUpdate<InventoryModificationState>
public class Data : IComponentData<InventoryModificationState>
public class Update : IComponentUpdate<InventoryModificationState>
public class Data : IComponentData<InventoryModificationState>

This is the signature of the base class
public class InventoryModificationState : IComponentMetaclass, IComponentFactory
public class InventoryModificationState : IComponentMetaclass, IComponentFactory

This is how i create the List and fill it with values
 private List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>> handler { get; set; }
 private ComponentUpdateManager()
 {
     handler = new List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>>
     {
         new InventoryModificationState_Handler(),
         new PlayerCraftingInteractionState_Handler()
     };
 }
 private List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>> handler { get; set; }
 private ComponentUpdateManager()
 {
     handler = new List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>>
     {
         new InventoryModificationState_Handler(),
         new PlayerCraftingInteractionState_Handler()
     };
 }


Im happy for any input as im stuck on this for some hours now 😄
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

HTTP Request throws exception when invoking controller
C#CC# / help
4y ago
✅ Is it possible to convert a generic interface to a generic interface?
C#CC# / help
3y ago
Interface implementing generic parameters
C#CC# / help
2y ago
help with generic interface
C#CC# / help
2y ago