无法从 .winmd 文件获取类型
我想在给定路径的 .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 属性为:无法加载一种或多种请求的类型。检索 LoaderException 属性以获取更多信息。
具有基础异常(TypeLoadException)的 LoaderExceptions 属性。它的 Message 属性如下: Runtime Impl 属性使用不当。
有人知道为什么我无法读取类型吗?
谢谢。
注意:我知道我正在使用 .NET 4.0。但是,在 .NET 4.5(Windows 8 预览版中的版本)中,我无法在运行时从文件加载程序集。 Assembly 类中没有方法可以执行此操作。
I want to output the types in a .winmd file given its path.
I copied a winmd file from my Windows 8 Developer Preview machine to my dev machine.
I wrote a small test app (in C#, .NET 4.0, not 4.5) which tries to load an assembly at run time, given its path, and outputs the types in it.
Though the assembly was loaded, an exception occurred when I tried to get the types.
Here is the code:
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
}
}
The exception is of type ReflectionTypeLoadException. Its Message property is: Unable to load one or more of the requested types. Retrieve the LoaderExceptions
property for more information.
The LoaderExceptions property that has the underlying exception, a TypeLoadException. Its Message property reads:
Bad use of Runtime Impl attribute.
Anybody knows why I cannot read the types?
Thanks.
Note: I know I am using .NET 4.0. But, in .NET 4.5 (the one in the Windows 8 preview), I could not load an assembly from file at run time. There is no method in the Assembly class that does it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然 .winmd 文件使用 ECMA 355 文件格式,但它们不是 .Net 程序集,并且您不太可能直接使用 .Net 框架读取这些文件。
如果您使用开发者预览版附带的 ILDASM 版本,则可以直观地查看文件中的类型。您还可以使用 ildasm 转储包含元数据文件中的类型的文本文件。我相信.Net Reflector也能做到这一点。
如果您确实需要以编程方式枚举 winmd 文件中的类型,我建议您使用 非托管元数据读取 API。这就是我们在内部读取开发工具元数据文件的方式。
如果您运行的是开发人员预览版,我建议您查看 RoGetMetaDataFile API - 这是 Chakra javascript 引擎用来打开特定类型的元数据文件的 API。
While .winmd files use the ECMA 355 file format, they are NOT .Net assemblies, and it is highly unlikely that you'll be able to read the files using the .Net framework directly.
If you use the version of ILDASM shipped with the developer preview, you can view the types in the files visually. You can also use ildasm to dump a text file containing the types in the metadata file. I believe that .Net reflector can also do this.
If you DO need to enumerate types in a winmd file programmaticly, I suggest that you use the unmanaged metadata reading APIs. That's how we read metadata files for our development tools internally.
If you're running on the developer preview build, I'll suggest you look at the RoGetMetaDataFile API - this is the API used by the Chakra javascript engine to open the metadata file for a particular type.
WinMD 是仅元数据的程序集。使用 ReflectionOnlyLoadFrom 应该可以解决问题。
以下代码有效。
WinMDs are metadata only assemblies. Using ReflectionOnlyLoadFrom should do the trick.
The following code works.
要使用 *.winmd 文件,您必须在
PropertyGroup
内的项目文件中进行设置To work with *.winmd files you must set in your project file inside
PropertyGroup