المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Variable of type



C# Programming
04-20-2010, 11:41 PM
How do you store a type in a variable?

Let's say I have the following base class, and a couple of derived classes:

public abstract class MyBase
{
public abstract string Test();

}

public class class1: MyBase
{
public override string Test()
{
return "Class1";
}
}

public class class2 : MyBase
{
public override string Test()
{
return "Class2";
}
}
Now, I want to store the "types" of class1 and class2 in an array, so I can instance the classes, something like this:

MyArray[0] TmpClass1 = new MyArray[0];
MyArray[1] TmpClass2 = new MyArray[1];
Obviously this doesn't work. And I've tried using typeof() and System.Type to do this, but end up getting compiler errors no matter what I try.

So how is it done?