Edit: Changed the wrong term boxing to casting.
I have the following problem:
If I create a new Delegate of type Action or Func it will be casted to a type of Delegate.
var @delegate = Delegate.CreateDelegate(type, @object, methodInfo);
But I need for a generic class the right casted object.
Consider following example:
class Example<T> {
Type GenericType() {
return typeof(T);
}
}
static Example<T> Create<T>(T @delegate) {
return new Example<T>();
}
Example.Create(@delegate).GenericType();
This will return Delegate as type, since this was the type of the casted object (@delegate).
One solution could be to cast the delegate like so:
if(@delegate is Action)
Example.Create((Action)@delegate).GenericType();
But since Delegate.CreateDelegate could also create Action or Func delegates, it is impossible to check all variations.
I can’t change the generic class, so i must cast the delegate.
I hope i was able to explain my problem. I am not a native English speaker…
Edit: The Problem is that typeof(T) not return the “real” type of the object. But i’m afraid there is no solution.
To answer my own question: It is not possible. 🙁