none
List of Generic Classes RRS feed

  • Question

  • I have an interface like this:

    public interface IMyInterface<T> {
       T Get();
       void Function1(T val)
       void Function2(T val)
    }

    I now need a list of classes that implement this interface and then access my functions, i.e. something like:

    List<IMyInterface> myList;
    myList[0].Function1(val);
    Some people suggest to create a base class like MyBase, then have a generic like MyClass<T> : MyBase and then List<MyBase>. However, that wouldn't help me because all the functions I have are generic and as such wouldn't be in MyBase, so myList[0].Function1(val) wouldn't be possible. Is there a better way to achieve what I want to do?


    Friday, February 2, 2018 6:38 PM

Answers

  • If you define a ‘List<dynamic> myList’, then ‘myList[0].Function1(val)’ should work, assuming that the type of val is suitable for this call.

    • Marked as answer by hulamula Friday, February 2, 2018 10:28 PM
    Friday, February 2, 2018 8:45 PM

All replies

  • If you define a ‘List<dynamic> myList’, then ‘myList[0].Function1(val)’ should work, assuming that the type of val is suitable for this call.

    • Marked as answer by hulamula Friday, February 2, 2018 10:28 PM
    Friday, February 2, 2018 8:45 PM
  • Viorel_, that's EXACTLY what I needed. That's really neat. Thanks a lot!
    Friday, February 2, 2018 10:28 PM