Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generics in C# Pin
srikumarrampa21-May-13 18:04
srikumarrampa21-May-13 18:04 
AnswerRe: Generics in C# Pin
Alan N22-May-13 4:21
Alan N22-May-13 4:21 
I can think of two ways to solve this problem. In the first the generic type definition is extracted from the object (see GenericTypeDefinition method in the example) and tested against actual definition e.g. typeof(Class1<>).

In the second, which I prefer (see Class2 in the example), the generic class must inherit a non generic abstract base class. Any Class2<T> may then be tested against the base class using the is operator.
C#
namespace GenericTypeTest {
  class Class1<T> { }

  abstract class BaseClass { }

  class Class2<T> : BaseClass { }

  internal class App {
    static void Main() {
      IsClass1(new Class1<int>());
      IsClass1(new Class1<string>());
      IsClass1(new Class2<string>());
      IsClass1(42);          // int
      IsClass1("Tiger");      // string


      Class2<string> strObj = new Class2<string>();
      if (strObj is BaseClass) {
        Console.WriteLine("{0} is derived from BaseClass", strObj);
      }
      Console.ReadLine();
    }

    static void IsClass1(Object obj) {
      if (GenericTypeDefinition(obj) == typeof(Class1<>)) {
        Console.WriteLine("{0} was constructed from {1}", obj.GetType(), typeof(Class1<>));
      } else {
        Console.WriteLine("{0} was NOT constructed from {1}", obj.GetType(), typeof(Class1<>));
      }
      Console.WriteLine();
    }

    static Type GenericTypeDefinition(Object obj) {
      Type result = null;
      Type test = obj.GetType();
      // Check if obj is a closed generic type      
      if (test.IsGenericType && !test.IsGenericTypeDefinition) {
        result = test.GetGenericTypeDefinition();
      }
      // null or the generic type definition
      return result;
    }
  }
}

GeneralRe: Generics in C# Pin
srikumarrampa22-May-13 19:37
srikumarrampa22-May-13 19:37 
AnswerRe: Generics in C# Pin
BillWoodruff23-May-13 2:23
professionalBillWoodruff23-May-13 2:23 
QuestionPassing Cursor Coordinates as Parameters Pin
ASPnoob20-May-13 21:52
ASPnoob20-May-13 21:52 
AnswerRe: Passing Cursor Coordinates as Parameters Pin
Pete O'Hanlon20-May-13 22:01
mvePete O'Hanlon20-May-13 22:01 
GeneralRe: Passing Cursor Coordinates as Parameters Pin
ASPnoob20-May-13 22:05
ASPnoob20-May-13 22:05 
AnswerRe: Passing Cursor Coordinates as Parameters Pin
OriginalGriff20-May-13 22:52
mveOriginalGriff20-May-13 22:52 
GeneralRe: Passing Cursor Coordinates as Parameters Pin
ASPnoob21-May-13 16:38
ASPnoob21-May-13 16:38 
GeneralRe: Passing Cursor Coordinates as Parameters Pin
lukeer21-May-13 21:47
lukeer21-May-13 21:47 
GeneralRe: Passing Cursor Coordinates as Parameters Pin
OriginalGriff21-May-13 22:21
mveOriginalGriff21-May-13 22:21 
QuestionPropertyGrid Multiple Objects Selection Pin
vvino202020-May-13 20:48
professionalvvino202020-May-13 20:48 
AnswerRe: PropertyGrid Multiple Objects Selection Pin
Eddy Vluggen20-May-13 22:32
professionalEddy Vluggen20-May-13 22:32 
AnswerRe: PropertyGrid Multiple Objects Selection Pin
Simon_Whale20-May-13 22:52
Simon_Whale20-May-13 22:52 
AnswerRe: PropertyGrid Multiple Objects Selection Pin
lukeer21-May-13 21:58
lukeer21-May-13 21:58 
QuestionNot able to use Modern UI Charts for Windows 8 Pin
Raghavendra Reddy C20-May-13 18:40
professionalRaghavendra Reddy C20-May-13 18:40 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Abhinav S20-May-13 18:49
Abhinav S20-May-13 18:49 
GeneralRe: Not able to use Modern UI Charts for Windows 8 Pin
Raghavendra Reddy C20-May-13 20:30
professionalRaghavendra Reddy C20-May-13 20:30 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Pete O'Hanlon20-May-13 21:40
mvePete O'Hanlon20-May-13 21:40 
GeneralRe: Not able to use Modern UI Charts for Windows 8 Pin
Raghavendra Reddy C20-May-13 22:35
professionalRaghavendra Reddy C20-May-13 22:35 
GeneralRe: Not able to use Modern UI Charts for Windows 8 Pin
Pete O'Hanlon20-May-13 22:54
mvePete O'Hanlon20-May-13 22:54 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Raghavendra Reddy C20-May-13 22:53
professionalRaghavendra Reddy C20-May-13 22:53 
AnswerRe: Not able to use Modern UI Charts for Windows 8 Pin
Richard MacCutchan20-May-13 22:54
mveRichard MacCutchan20-May-13 22:54 
QuestionPROBLEM with SEND DATA to LOCAL PORT C# Pin
Andrew Nguyen20-May-13 17:18
Andrew Nguyen20-May-13 17:18 
AnswerRe: PROBLEM with SEND DATA to LOCAL PORT C# Pin
Ron Beyer20-May-13 17:56
professionalRon Beyer20-May-13 17:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.