0

I have names of class a and b:

string nameTypeInput = activityNamespace + "." + className;
string nameTypeOutput = activityNamespace + "." + (className.Replace("Request","Response"));

Type typeInput = Type.GetType(nameTypeInput);
Type typeOutput = Type.GetType(nameTypeOutput);

I have to do something like this:

var rsp = (object)request as DaDaDocsActivityBase<typeInput, typeOutput>;

But I have the following error:

'typeOutput' is a variable but is used like a type

What am I doing wrong?

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Leonid Zolotarov
  • 191
  • 1
  • 10
  • I may be wrong, but I don't think there is a way to do this for introspected/reflected types like you're using, since they're runtime and casts are compile-time. – AKX Sep 11 '18 at 08:20
  • That is pretty complex, maybe someone takes the time to write it down, but it's doable via reflection. Read up generics and reflection. There is no compile time safe operation for what you're trying to accomplish – Mafii Sep 11 '18 at 08:20
  • '(object)request' it's only a suggestion – Leonid Zolotarov Sep 11 '18 at 08:24
  • See duplicate, you can use reflection, but then you can't do anything meaningful with the result, as you don't have a compile-time type that you can use. – CodeCaster Sep 11 '18 at 08:25
  • `I have to do something like this:` doesn't really describe your problem. – jegtugado Sep 11 '18 at 08:25
  • Where possible, have a method in your own class that's generic in enough type parameters and put most of the code in there. The use reflection to get the types you need and conjure up an `Expression` that invokes your generic method appropriately. Once you can invoke that, you suddenly get to the "other side of the mirror" and can work as normal without doing more reflection. – Damien_The_Unbeliever Sep 11 '18 at 08:28
  • Combine with https://stackoverflow.com/questions/1398796/casting-with-reflection – Jodrell Sep 11 '18 at 08:28

0 Answers0