If I am passed an instance of a class which implements an interface, I do not necessarily know the exact type. If I want to create an array of this class (whatever it may be), I can do so like this:
IWhatever[] ArrayOfClass = (IWhatever[])Array.CreateInstance(InstanceOfClass.GetType(), length);
As long as InstanceOfClass is an instance of a class which implements IWhatever, this works.
The problem is that I can’t seem to figure out how to initialise the members of the array. I thought new IWhatever() might work, but it doesn’t.
How do I construct the members of array?
You’d need to initialize each member to an instance of some class which implements the interface.
If you wanted the same type as
InstanceOfClass, and if that type has a default constructor, you could use something like: