1

I want a variable to only contain types of classes that inherit a specific abstract. In this example, the variable "MyLetter" is supposed to only contain the types LetterA, LetterB, or anything that inherits LetterAbstract.

How would I accomplish this?

abstract class LetterAbstract
{
    public char Letter;
}

class LetterA : LetterAbstract
{
    void LetterA()
    {
        Letter = 'A';
    }
}

// valid
TLetterAbstract MyLetter = typeof(LetterA);

// invalid
TLetterAbstract MyLetter = typeof(string);
Josh
  • 3,258
  • 2
  • 19
  • 31

3 Answers3

3

In this case you need to declare the variable with the data type LetterAbstract (the so called base class).

LetterAbstract letter;

letter = new LetterA(); // valid
letter = "asdasd"; // invalid

I think you need to read something about inheritance first, see "Inheritance (C# Programming Guide)".

:edit

Regarding

I do not want an instance of LetterA. I want the class type LetterA.

you could fiddle something with generics but i dunno what the use case might be:

class TypeVar<A>
  where A: LetterAbstract
{
  public void SetValue<B>()
    where B: A
  {
    mValue = typeof(B);
  }

  public System.Type GetValue()
  {
    return mValue;
  }

  private System.Type mValue;
}

var x =new TypeVar<LetterAbstract>();
x.SetValue<LetterA>(); // valid
x.SetValue<string>(); // invalid
x.GetValue(); // get the type
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
  • I do not want an instance of LetterA. I want the class type LetterA. – Josh Dec 01 '17 at 06:49
  • 3
    @Josh you don‘t seem to be clear about what types, classes are on the one hand and instances, objects on the other hand. You should inform yourself about that first. Your comment makes no sense. – Sefe Dec 01 '17 at 06:54
0

I do not want an instance of LetterA. I want the class type LetterA

Though what you want doesn't seem clear for me. Try this.

Note that if you want to restrict your coding then it is not possible unless you declare a variable Letter but you can determine if a variable is a subclass of some type.

static bool IsLetter(object obj)
{
    Type type = obj.GetType();

    return type.IsSubclassOf(typeof(Letter)) || type == typeof(Letter);
}

Sample:

class Foo
{

}

abstract class Letter
{

}

class LetterA : Letter
{

}

class LetterAA : LetterA
{

}

class LetterAB : LetterA
{

}

Usage:

LetterAA aa = new LetterAA();
LetterAB ab = new LetterAB();
Foo foo = new Foo();

bool isAA = IsLetter(aa);
bool isAB = IsLetter(ab);
bool isLetter = IsLetter(foo);

Console.WriteLine(isAA); // True
Console.WriteLine(isAB); // True
Console.WriteLine(isLetter); // False
jegtugado
  • 5,081
  • 1
  • 12
  • 35
0

Do you think about that TLetterAbstract class? You could define it like:

public class TLetterAbstract : Type
{
    private Type _T;
    public TLetterAbstract(object o)
    {
        if (o is LetterAbstract)
        {
            _T = o.GetType();
        }
        throw new ArgumentException("Wrong input type");
    }
    public TLetterAbstract(Type t)
    {
        if (t.IsInstanceOfType(typeof(LetterAbstract)))
        {
            _T = t;
        }
        throw new ArgumentException("Wrong input type");
    }

    public override object[] GetCustomAttributes(bool inherit)
    {
        return _T.GetCustomAttributes(inherit);
    }
    // and some more abstract members of Type implemented by simply forwarding to the instance
}

Then you have to change your code from

TLetterAbstract MyLetter = typeof(LetterA);

to

TLetterAbstract MyLetter = new TLetterAbstract(typeof(LetterA));
Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33