[英]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.