Skip to main content
added 52 characters in body
Source Link
CodesInChaos
  • 109.2k
  • 26
  • 224
  • 267

First of all, a is T only works if the left side is an instance, not a type. So you need to apply typeof(T) on both sides. And then either use == if you want to check if the types are identical, or IsAssignableFrom(...) to check if one is derived from the other. In Your case you probably want ==.

The casts only work if you insert a known base type. Which can be object or BaseClass.

public static T Create<T>() where T : BaseClass
{
    if( typeof(T) == typeof(DerivedClass1 )) return (T)(BaseClass)new DerivedClass1();
    if( typeof(T) == typeof(DerivedClass2 )) return (T)(BaseClass)new DerivedClass2();

    return null;
}

But I'm not sure if this is a good idea in the first place. This code isn't really generic since it only works for a few classes you hardcoded.

First of all, a is T only works if the left side is an instance, not a type. So you need to apply typeof(T) on both sides. And then either use == if you want to check if the types are identical, or IsAssignableFrom(...) to check if one is derived from the other. In Your case you probably want ==.

The casts only work if you insert a known base type. Which can be object or BaseClass.

public static T Create<T>() where T : BaseClass
{
    if( typeof(T) == typeof(DerivedClass1 )) return (T)(BaseClass)new DerivedClass1();
    if( typeof(T) == typeof(DerivedClass2 )) return (T)(BaseClass)new DerivedClass2();

    return null;
}

But I'm not sure if this is a good idea in the first place. This code isn't really generic.

First of all, a is T only works if the left side is an instance, not a type. So you need to apply typeof(T) on both sides. And then either use == if you want to check if the types are identical, or IsAssignableFrom(...) to check if one is derived from the other. In Your case you probably want ==.

The casts only work if you insert a known base type. Which can be object or BaseClass.

public static T Create<T>() where T : BaseClass
{
    if( typeof(T) == typeof(DerivedClass1 )) return (T)(BaseClass)new DerivedClass1();
    if( typeof(T) == typeof(DerivedClass2 )) return (T)(BaseClass)new DerivedClass2();

    return null;
}

But I'm not sure if this is a good idea in the first place. This code isn't really generic since it only works for a few classes you hardcoded.

Post Undeleted by CodesInChaos
Post Deleted by CodesInChaos
Source Link
CodesInChaos
  • 109.2k
  • 26
  • 224
  • 267

First of all, a is T only works if the left side is an instance, not a type. So you need to apply typeof(T) on both sides. And then either use == if you want to check if the types are identical, or IsAssignableFrom(...) to check if one is derived from the other. In Your case you probably want ==.

The casts only work if you insert a known base type. Which can be object or BaseClass.

public static T Create<T>() where T : BaseClass
{
    if( typeof(T) == typeof(DerivedClass1 )) return (T)(BaseClass)new DerivedClass1();
    if( typeof(T) == typeof(DerivedClass2 )) return (T)(BaseClass)new DerivedClass2();

    return null;
}

But I'm not sure if this is a good idea in the first place. This code isn't really generic.