关于类的 GetType 的 C# 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/629087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:58:07  来源:igfitidea点击:

C# question about GetType of class

c#

提问by SyncMaster

I have an assembly asdf.dll and it has a class 'Class1'.

我有一个程序集 asdf.dll,它有一个类“Class1”。

How can I get the type of Class1?

如何获得 Class1 的类型?

string a = "Class1"; //Class1 is the name of class in asdf.dll
string typeString = typeof(Class1).FullName; // here I only have the string Class1 and not Class Class1
AssemblyName assemblyName = AssemblyName.GetAssemblyName("asdf.dll");
Type type = Type.GetType(typeString + ", " + assemblyName);

How can I get the type of a class from a string holding the class name?

如何从包含类名的字符串中获取类的类型?

采纳答案by leppie

Type t = Type.GetType("MyDll.MyClass,Mydll")

where MyDll.MyClass is the class Location of your desire class/Form. Mydll is the your dll name. which u want to Call.

其中 MyDll.MyClass 是您想要的类/表单的类位置。Mydll 是您的 dll 名称。你想打电话。

回答by leppie

typeof(Class1).FullNameis already the fully qualified name.

typeof(Class1).FullName已经是完全限定名称。

Try just passing that, or using the Type.Name property instead.

尝试只传递它,或者改用 Type.Name 属性。