C# 使用 Assembly.GetType("MyCompany.Class1.Class2") 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376105/
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 01:09:16  来源:igfitidea点击:

Using Assembly.GetType("MyCompany.Class1.Class2") returns null

c#.net

提问by Kevin

I'm attempting to use Assembly.GetType("MyCompany.Class1.Class2") to dynamically get a type from a string.

我正在尝试使用 Assembly.GetType("MyCompany.Class1.Class2") 从字符串中动态获取类型。

Assembly.GetType("MyCompany.Class1");

works as expected.

按预期工作。

If I embed a class within another class such as:

如果我在另一个类中嵌入一个类,例如:

namespace MyCompany
{
  public class Class1
  {
     //.....
     public class Class2
     {
        //.....
     }
  }
}

and try to get the type Class2

并尝试获取 Class2 类型

Assembly.GetType("MyCompany.Class1.Class2") 

will return a null.

将返回空值。

I'm using the .NET Frameworks 3.5 SP1

我正在使用 .NET Frameworks 3.5 SP1

Does anyone know what I'm doing incorrectly and what I can do to fix this?

有谁知道我做错了什么以及我能做些什么来解决这个问题?

Thanks in advance

提前致谢

Kevin D. Wolf Tampa, FL

凯文 D. 沃尔夫佛罗里达州坦帕市

采纳答案by CMS

You need the Plus sign to get Nested Classes to be mapped using Assembly.GeType.

您需要加号才能使用 Assembly.GeType 映射嵌套类。

 Assembly.GetType("MyCompany.Class1+Class2");

回答by plinth

I think it's named MyComnpany.Class1+Class2.

我认为它被命名为 MyComnpany.Class1+Class2。

If I run this code on a similar structure, that's what I see:

如果我在类似的结构上运行此代码,这就是我所看到的:

Assembly assem = Assembly.GetExecutingAssembly();
Type[] types = assem.GetTypes();

example Types to see the names.

示例类型以查看名称。

回答by BFree

You need to use plus signs. Something like "MyAssembly.Class1+NestedClass".

您需要使用加号。类似于“MyAssembly.Class1+NestedClass”。