简体   繁体   English

如何创建一个需要一个继承抽象类的类的变量?

[英]How do I make a variable that requires a type of a class that inherits an abstract?

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. 在此示例中,变量“MyLetter”应该只包含LetterA,LetterB或继承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);

In this case you need to declare the variable with the data type LetterAbstract (the so called base class). 在这种情况下,您需要使用数据类型LetterAbstract (所谓的基类)声明变量。

LetterAbstract letter;

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

I think you need to read something about inheritance first, see " Inheritance (C# Programming Guide) ". 我想你首先需要阅读有关继承的内容,参见“ 继承(C#编程指南) ”。

:edit :编辑

Regarding 关于

I do not want an instance of LetterA. 我不想要LetterA的实例。 I want the class type LetterA. 我想要类型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

I do not want an instance of LetterA. 我不想要LetterA的实例。 I want the class type LetterA 我想要类型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. 请注意,如果要限制编码,则除非您声明变量Letter否则无法进行编码,但您可以确定变量是否是某种类型的子类。

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

Do you think about that TLetterAbstract class? 您是否考虑过TLetterAbstract类? 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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何声明从另一个抽象类继承的抽象泛型类? - How can I declare an abstract generic class that inherits from another abstract class? 如何定义继承通用抽象类的通用抽象类? - How to define a generic abstract class that inherits a generic abstract class? 如何创建从另一个继承并在CodeDom中传递类型参数的类? - How do I create a class that inherits from another and passes a type parameter in CodeDom? 继承自抽象 class 的泛型 class 类型的 C# - C# type of generic class that inherits from abstract class 如何将抽象类作为参数传递给类型的实例? - How do I pass abstract class as parameter for instance of type? 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 在运行时创建类型,继承抽象类并实现接口 - Create type at runtime that inherits an abstract class and implements an interface 继承自Passed Type Parameter成员的泛型Abstract类 - Generic Abstract class who inherits members from Passed Type Parameter 如果我的应用程序中没有其他类继承它,只是为了使其不可实例化,那么使类抽象是一种好习惯吗? - Is it good practice to make a class abstract if I no other class in my application inherits from it, just for the sake of making it non-instantiable? 如何为继承IdentityUser的类创建主键 - How do I create primary key for class that inherits IdentityUser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM