In Java, it's convenient for Class to have the generic parameter, X. But C#'s Type class doesn't have this.
So in C#, how does one do the equivalent of the following Java code?:
public <X> X methodThatReturns(Class<X> clazz) { ... }
There doesn't seem to be a way in C# to connect that return values, and the passed Type.
Clarification
Several answers are suggesting the method parameter isn't necessary, because the method could simply be defined as methodThatReturns<X>()
.
But if you do have some unknown Type variable, t
, there's basically no way to call such a generic method so that it will return an object of Type t
?
In Java, you're free to pass around Class<X>
variables without losing the type information, but it seems that in C# if you pass around the equivalent Type
variables, you can run into limitations, because you can't use them when you need to call generic methods.
public X methodThatReturns<X>(Class<X> clazz) { ... }
Also keep in mind that in C# there is no type erasure so you can do things like typeof(T)
without worries in case Class is meant to be the java "Class" object rather than a "some class" placeholder:
public X methodThatReturns<X>(X value)
{
Type x = typeof(X); // that's fine
if (value is SomeType) { } // that's fine too
return (X)someObject; // I think you get the point
}
Edit:
Again, since the generic type information is not lost after compilation you don't need to pass in the type explicitly:
public X methodThatReturns<X>()
{
Type xType = typeof(X); // Type is to C#/.Net what Class<X> is to Java.
}
In C# it would be
public X methodThatReturns<X>() { ... }
and you can get type of X using typeof(X)
instead of using the parameter clazz
I'm not 100% up on Java generics... Are you trying to declare a method that returns the same type as it was passed?
public T MethodThatReturns<T>(T input) { ... }
This method will be generic on any type, and return the same type it was passed. You can call this using:
AnyTypeAtAll foo = new AnyTypeAtAll();
AnyTypeAtAll bar = MethodThatReturns(foo);
Note that there's no <AnyTypeAtAll>
on the call to MethodThatReturns, the compiler can figure it out given the parameter you passed. However, note that it's the compiler doing that, not runtime, so it'll only use the type of variable foo, not the type of object that foo points to.
On the other hand, if that's Java syntax for a method that takes no 'real' parameters, then simply:
public T MethodThatReturns<T>()
{
Type clazz = typeof(T);
....
}
Within this method, you can treat T the same as you would any other class, and it will refer specifically to the type that the method was called with, not to object
or anything like that. You may want to put where T : class
to allow comparison to null in that method (which does prevent you from using int
as the generic type), or where T : new()
to restrict the generic type to have a default constructor, so you can do T foo = new T();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With