-1

Possible Duplicate:
How to use reflection to call generic Method?

how can i do the following?

class A {}

void DoSomething<T, U> where T : List<U> {}

object o = new List<A>();

DoSomething(o); // cannot be inferred from usage error

I don't know the exact type of A until runtime.

Just to clarify. I have no access to change the DoSomething method. I know the following call will work

DoSomething(o as List<A>);

but I don't know A until runtime. Although I do know that all As will have a common sub class.

Community
  • 1
  • 1
mfc
  • 3,018
  • 5
  • 31
  • 43
  • 1
    You may want to consider constraining your types to some interfaces, and then casting to those interfaces so you can take whatever actions you need. – Maurice Reeves Dec 31 '12 at 05:57
  • OK - sounds like duplicate to me. I think the only option is to use reflection to find type and than call method as described in linked question. Note that for chance to take advantage of "As will have a common sub class" you will still need to change `DoSomething` to allow list of items or derived classes... or accept `IEnumerable`. – Alexei Levenkov Dec 31 '12 at 07:15

1 Answers1

-1

Well, then WHAT USE IS DoSOmething?

There are 2 possibilities:

  • you know more than "object" in order to call methods there, then you have to know that when writing the method.
  • you do not know, then you only do generic stuff that is defined on Object, List, then you don't need to know the type.
BenMorel
  • 34,448
  • 50
  • 182
  • 322
TomTom
  • 61,059
  • 10
  • 88
  • 148