【发布时间】:2012-02-03 23:02:30
【问题描述】:
我想在给定路径的 .winmd 文件中输出类型。 我将一个 winmd 文件从我的 Windows 8 Developer Preview 机器复制到我的开发机器。 我编写了一个小型测试应用程序(在 C#,.NET 4.0,而不是 4.5 中),它尝试在运行时加载程序集,给定它的路径,并输出其中的类型。 虽然程序集已加载,但当我尝试获取类型时发生异常。
代码如下:
static void Main(string[] args)
{
if (args.Length != 1) return;
var path = args[0];
if (!System.IO.File.Exists(path))
{
Console.WriteLine("file not found : " + path);
return;
}
var asm = System.Reflection.Assembly.LoadFrom(path); // load successful.
Console.WriteLine("loaded ");
string name = asm.GetName().Name;
Console.WriteLine(name);
System.Type[] types = asm.GetTypes(); // exception occurs here
foreach(var type in types)
{
// output type name
}
}
异常类型为 ReflectionTypeLoadException。它的 Message 属性是:无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。
具有基础异常 TypeLoadException 的 LoaderExceptions 属性。它的 Message 属性为: Runtime Impl 属性使用不当。
有人知道我为什么看不懂这些类型吗?
谢谢。
注意:我知道我使用的是 .NET 4.0。但是,在 .NET 4.5(Windows 8 预览版中的那个)中,我无法在运行时从文件中加载程序集。 Assembly 类中没有方法可以做到这一点。
【问题讨论】:
标签: .net reflection windows-runtime