I have a List which must contain IInteract Objects. But IInteract is a generic interface which requires 2 type arguments.
My main idea is iterate through a list of Objects and “Interact” one with another if they didn’t interact yet.
So i have this object
List<IObject> WorldObjects = new List<IObject>();
and this one:
private List<IInteract> = new List<IInteract>();
Except I can’t compile the last line because IInteract requires 2 type arguments. But I don’t know what the arguments are until I add them. I could add interactions between Objects of Type A and A… or Objects of Type B and C.
I want to create “Interaction” classes which do something with the “acting” object and the “target” object, but I want them to be independent from the objects… so I could add an Interaction between for instance… “SuperUltraClass” and… an “integer”.
Am I using the wrong approach?
Assuming IInteract is defined as something like
and you are using it for a field of a class Foo:
Then if you want to defer the decision of what types to bind to the IInteract type arguements you need to parameterize the container class:
The type arguments to IInteract here will be bound when you define a concrete instantiation of the container class, like:
var x = new Foo<int, double>().This will cause the IInteract field to be of typeIInteract<int, double>for that particular instantiation of the Foo generic type.