C# 通过提供类名作为字符串来获取引用程序集中的类型?

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

Get type in referenced assembly by supplying class name as string?

c#.netreflection.net-assemblygettype

提问by SventoryMang

These are similar questions: How-to: Load a type from a referenced assembly at runtime using a string in Silverlight, GetType on a class in a referenced assembly failsbut neither answer works.

这些类似的问题:操作方法:从加载类型使用Silverlight中的字符串在运行时引用的程序集在引用汇编失败一类的GetType但是没有答案的作品。

I've got an MVC project that pulls data from a database that includes the plain types as strings. These types are in a referenced assembly, not in the MVC project.

我有一个 MVC 项目,它从包含普通类型作为字符串的数据库中提取数据。这些类型在引用的程序集中,而不是在 MVC 项目中。

So for example let's say my Referenced Assembly Name is MyFrameworkand the plain type name Car, the full type name could be MyFramework.Cars.Caror MyFramework.Vehicles.Cars.Caror some other variation. All I have are the referenced assembly name and plain class name as strings. How can I get the type regardless of the full type name?

例如,假设我的引用程序集名称是MyFramework和普通类型名称Car,完整类型名称可以是MyFramework.Cars.CarMyFramework.Vehicles.Cars.Car或其他一些变体。我所拥有的只是作为字符串的引用程序集名称和普通类名称。无论完整的类型名称如何,如何获取类型?

Finally, could I write a function in the referenced assembly that calls GetType() and use that in the MvC project so I could forego including the assembly name? I want to remove knowing the assembly name so I thought I could write a Util IN the referenced assembly like:

最后,我可以在引用的程序集中编写一个调用 GetType() 的函数并在 MvC 项目中使用它,这样我就可以放弃包含程序集名称吗?我想删除知道程序集名称的信息,所以我想我可以在引用的程序集中编写一个 Util ,例如:

namespace MyFramework //the referenced assembly
{
  public static class TypeUtil
  {
    public static Type GetFrameworkType(string typeName)
    {
        return Type.GetType(typeName);
    }
  }
}

And then in my MVC project I could call it without needing the assembly as a string name. Is that possible or will I always need the assembly name?

然后在我的 MVC 项目中,我可以在不需要程序集作为字符串名称的情况下调用它。这是可能的还是我总是需要程序集名称?

采纳答案by Jony Adamit

Maybe the referenced assembly isn't loaded at the time. Also, I understand from your question that you do not have the full type name, only the class name.
You should try something along this line then:

也许当时没有加载引用的程序集。另外,我从您的问题中了解到您没有完整的类型名称,只有类名。
你应该尝试沿着这条线做一些事情:

Type type = Assembly.Load("YourAssemblyName").GetTypes().First(t => t.Name == "ShortTypeName");

Hope I understood you correctly.

希望我理解正确。

回答by jon

For the first question, you could do something like

对于第一个问题,你可以做类似的事情

Type t = AppDomain.CurrentDomain.GetAssemblies()
                                .Where(a => a.FullName == "MyFramework")
                                .SelectMany(a => a.GetTypes())
                                .Where(t => t.Name == "Car")
                                .FirstOrDefault();

I am not sure what you mean by the second question.

我不确定你说的第二个问题是什么意思。