0

Explained below in the code snippet, I can't figure out the syntax for this:

object tcsObject = new TaskCompletionSource<Apple>();
Apple apple = new Apple();

// How do I do this?
TaskCompletionSource<apple.GetType>() tsc = tscObject as TaskCompletionSource<apple.GetType>()

edit Should note the particular issue I am trying to figure is how to do this generics cast without initially knowing that the the type is Apple. The type could end up being anything, so I am looking to do this operation from what is returned via GetType.

rygo6
  • 1,929
  • 22
  • 30
  • You can use reflection, but the problem is that all further code that will be using it needs to use reflection as well. Wouldn't it be better to mask it as a `TaskCompletionSource` ? – Lasse V. Karlsen Dec 08 '20 at 09:29
  • 1
    You can't pass dynamic types into generics. Generics are a compile time concept, GetType runs at runtime – Liam Dec 08 '20 at 09:29
  • 1
    You can't. The compiler needs to know the type of each variable at *compile time*. Either with a known fixed type or with your own in-scope generic type parameter. – Damien_The_Unbeliever Dec 08 '20 at 09:29
  • In that example, the type is always `Apple` so why don't you just use `TaskCompletionSource`? – Liam Dec 08 '20 at 09:30
  • You could create a method with all the code in it that needs to use the `TaskCompletionSource`, and then use reflection to call that method, then you would at least minimize the amount of reflection needed. – Lasse V. Karlsen Dec 08 '20 at 09:30
  • Does this answer your question? [Converting a string to a class name](https://stackoverflow.com/questions/493490/converting-a-string-to-a-class-name) –  Dec 08 '20 at 09:35
  • Also this duplicate (there are some others like that): https://stackoverflow.com/questions/687363/how-do-i-create-a-generic-class-from-a-string-in-c –  Dec 08 '20 at 09:36
  • Holup.... Why do you need to do this? This is bordering on an X/Y problem. If you explain why you are in this situation, there might be other approaches. – TheGeneral Dec 08 '20 at 09:38

1 Answers1

1

If you don't mind sacrificing compile-time type safety, you could make use of Convert.ChangeType() and the dynamic keyword like so:

static dynamic ConvertTo(this object source, Type targetType)
{
    return (dynamic)Convert.ChangeType(source, targetType)
}

public void YourActualCode()
{
    object tcsObject = new TaskCompletionSource<Apple>();
    Apple apple = new Apple();

    TaskCompletionSource<Apple> tsc = ConvertTo(tscObject, typeof(TaskCompletionSource<Apple>))
}

The nice thing about this approach is you can defer determination of the parameter type until runtime:

var wrapperType = typeof(TaskCompletionSource<>);
var paramType = typeof(Apple);

var concreteType = wrapperType.MakeGenericType(paramType);

TaskCompletionSource<Apple> tsc = ConvertTo(tscObject, concreteType);
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you for this. The thing I was really trying to figure out was how to make the generic type from GetType. Specifically the apple.GetType. As the issue I am struggling with is I do not know the type that apple may end up being. – rygo6 Dec 08 '20 at 22:48
  • @rygo6 then you definitely want something like `typeof(TaskCompletionSource<>).MakeGenericType(apple.GetType())` :) – Mathias R. Jessen Dec 08 '20 at 22:50