我有很多对象,每个对象都有关于其类型的字符串信息。
喜欢:
string stringObjectType = "DateTime";
在运行时,我没有对象本身。
所以我无法测试它 typeof (object)
如何在运行时获取对象的类型:
typeof (stringObjectType)
最佳答案
try
{
// Get the type of a specified class.
Type myType1 = Type.GetType("System.DateTime");
Console.WriteLine("The full name is {myType1.FullName}.");
// Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
Type myType2 = Type.GetType("NoneSuch", true);
Console.WriteLine("The full name is {myType2.FullName}.");
}
catch(TypeLoadException e)
{
Console.WriteLine(e.Message);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
关于c# - 类型 : how to get type from string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15108786/