[英]How to get class type by its class name?
namespace Myspace
{
public class MyClass
{
}
} //This class is in another file.
using Myspace;
static void Main(string[] args)
{
Regex regexViewModelKey = new Regex(RegularExpr.ViewModelKeyPattern);
string viewModel = regexViewModelKey.Match(match.Value).Value;
//Now, vieModel is a string, and its value is "MyClass". So, I only know the class name, this is why I ask this question.
//Now, I'm only allowed to use the string form of this class name to get its type.
//I have tyied like this, but its no use.
Type t = Type.GetType(viewModel);
//it's return a null.
//Then I tyied another method like this, but there is an exception when calling Assembly.Load
Assembly assembly = Assembly.Load("Myspace");
Type ty = assembly.GetType("Myspace" + viewModel);
}
I hope my question is clear.我希望我的问题很清楚。 Can any one help me.THX I'm only allowed to use the string form of this class name to get its type.
任何人都可以帮助我。THX 我只允许使用此类名称的字符串形式来获取其类型。
thx everyone.谢谢大家。 I have solved this question by myself like this.
我已经像这样自己解决了这个问题。
{
Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
}
just use the function typeof().只需使用函数 typeof()。 The parameter is just that class name.
参数就是那个类名。
Type type = typeof(FIXProtoClientTest);
Generally speaking, you'll hardly ever need to do type comparisons unless you're doing something with reflection or interfaces.一般来说,除非您使用反射或接口做一些事情,否则您几乎不需要进行类型比较。 Nonetheless:
尽管如此:
If you know the type you want to compare it with, use the is
or as
operators:如果您知道要与之比较的类型,请使用
is
或as
运算符:
if( unknownObject is TypeIKnow ) { // run code here
The as
operator performs a cast that returns null
if it fails rather than an exception: as
运算符执行强制转换,如果失败而不是异常则返回null
:
TypeIKnow typed = unknownObject as TypeIKnow;
If you don't know the type and just want runtime type information, use the .GetType()
method:如果您不知道类型而只想要运行时类型信息,请使用
.GetType()
方法:
Type typeInformation = unknownObject.GetType();
// int is a value type
int i = 0;
// Prints True for any value of i
Console.WriteLine(i.GetType() == typeof(int));
// string is a sealed reference type
string s = "Foo";
// Prints True for any value of s
Console.WriteLine(s == null || s.GetType() == typeof(string));
// object is an unsealed reference type
object o = new FileInfo("C:\\f.txt");
// Prints False, but could be true for some values of o
Console.WriteLine(o == null || o.GetType() == typeof(object));
// Get the type of a specified class.
Type myType1 = Type.GetType("System.Int32");
Console.WriteLine("The full name is {0}.", 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 {0}.", myType2.FullName);
// FileSystemInfo is an abstract type
FileSystemInfo fsi = new DirectoryInfo("C:\\");
// Prints False for all non-null values of fsi
Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo));
Your line Type.GetType(model) will work if you use the fully qualified class name, including its namespace.如果您使用完全限定的类名,包括其命名空间,您的行 Type.GetType(model) 将起作用。
Furthermore, if it's in a different assembly from the code that makes the call you should use Assembly.GetType(typeName) when the assembly object referred to is an instance of the assembly containing the type.此外,如果它与进行调用的代码位于不同的程序集中,则当引用的程序集对象是包含该类型的程序集的实例时,您应该使用 Assembly.GetType(typeName)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.