Test for generic method argument being a class?

The compiler will already do that for you - you should actually see: The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'blah. MyGenericMethod(T) at compile-time The tricky scenarios are: generics upon generics upon generics - all those type-constraints stack, so you end up with where T : class a lot Sometimes it is better to use runtime validation against T reflection ( MakeGenericMethod etc) - again, just check at runtime Also, note that where T : class doesn't actually mean T is a class it means it is a reference-type, which can include interfaces and delegates. Likewise where T : struct doesn't actually mean T is a struct it means it is a struct that is not Nullable.

The compiler will already do that for you - you should actually see: The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'blah. MyGenericMethod(T)' at compile-time. The tricky scenarios are: generics upon generics upon generics - all those type-constraints stack, so you end up with where T : class a lot.

Sometimes it is better to use runtime validation against T reflection (MakeGenericMethod, etc) - again, just check at runtime Also, note that where T : class doesn't actually mean T is a class - it means it is a reference-type, which can include interfaces and delegates. Likewise, where T : struct doesn't actually mean T is a struct - it means it is a struct that is not Nullable.

1 Nice explanation. – Adam Houldsworth Jun 3 at 8:57 Yes, this is exactly what happens, but I want to avoid the thousands of thrown and caught exceptions that this generates. I am actually calling the method with MakeGenericMethod but the type passed is dynamic (in a loop) and I was hoping to put a simple if in before calling the method to avoid the exception.

– Craig Jun 3 at 9:01 1 @Craig you never mentioned MakeGenericMethod ;p Just check against the type -!type. IsValueType is probably good enough, or type. IsClass || type.

IsInterface – Marc Gravell? Jun 3 at 9:11 perfect, IsClass and IsValueType is exactly what I didn't know about and was looking for. – Craig Jun 3 at 9:17.

You can not pass int because it is a value type and you have constrained your Method to accept only reference type. If you want to support any of those you need to remove the generics constrain like following private void MyGenericMethod(T arg) { if(arg.GetType(). IsValueType) { //T is value type } }.

This is the opposite of what I want. I want to only pass and process reference types. – Craig Jun 3 at 9:02 Same logic would work, check for arg.GetType().

IsValueType, and call the method accordingly. – Int3 á½° Jun 3 at 10:41.

You actually get compilation errors if the argument being passed is not a reference type. If you box a value type, however, you can get around that, but then it's a valid reference type. Using System; using System.Collections.

Generic; using System. Linq; using System. Text; namespace ConsoleApplication29 { class Program { static void Main(string args) { int I = 0; object o = (object)i; MyMethod(o); MyMethod(i); // Doesn't compile.

} static void MyMethod(T arg) where T : class { } } } If you are getting this error because you do something at runtime, such as use reflection to call the method, or you box value types, then you will simply need to check the parameter before you use it: static void MyMethod(T arg) where T : class { if (arg is ValueType) throw new ArgumentException(); } Note that this will catch all value types, whether they are boxed or not. Also note that using is also matches true for the type in it's hierarchy (base / derived classes), whereas checking GetType against typeof takes only the type at that level: int I = 0; bool be = I is object; be = i.GetType() == typeof(object); Obviously in your case you won't want to throw an ArgumentException and perhaps just do nothing.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions