使用IMetadataImport时如何获取枚举值 [英] How to get an enumeration value when using IMetadataImport

查看:120
本文介绍了使用IMetadataImport时如何获取枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 IMetadataImport 时,如何从*.winmd文件中获取与枚举关联的数值?

How to you get the numeric value associated with an enum from a *.winmd file when using IMetadataImport?

一个很好的例子是 ApplicationHighContrastAdjustment 枚举:

A good example is ApplicationHighContrastAdjustment enumeration:

//Windows.UI.Xaml.ApplicationContrastMode (@020000006)
public enum ApplicationHighContrastAdjustment : uint
{
    None = 0u,
    Auto = 4294967295u
}

大多数枚举是0, 1, 2, ....但这枚举成员上还指定了其他值:

Most enumerations are 0, 1, 2, .... But this one has other values specified on the enum members:

  • 0
  • 4294967295

如何阅读以获取那些 UInt32

注意:该问题不必仅适用于WinRT. C#世界中使用相同的接口来检查.NET托管程序集. WinRT恰好使用相同的汇编文件格式.

Note: The question doesn't have to apply to just WinRT. The same interfaces are used in the C# world to inspect .NET managed assemblies. WinRT happens to use the same assembly file format.

我正在使用IMetadataImport来读取*.winmd(用于WinRT应用程序的TLB的现代版本)的内容.但是这个问题同样适用于读取有关.NET托管程序集的元数据.

I'm using IMetadataImport to read the contents of an *.winmd (the modern version of TLBs for WinRT applications). But the question applies equally to reading metadata about a .NET managed assembly.

关于如何启动并运行读取winmd元数据文件的精简版:

The abridged version of how to get up and running reading winmd metadata file:

// Create your metadata dispenser:
IMetadataDispsener dispener;
MetaDataGetDispenser(CLSID_CorMetaDataDispenser, IMetaDataDispenser, out dispenser);

//Open the winmd file we want to dump
String filename = "C:\Windows\System32\WinMetadata\Windows.UI.Xaml.winmd";

IMetaDataImport reader; //IMetadataImport2 supports generics
dispenser.OpenScope(filename, ofRead, IMetaDataImport, out reader); //"Import" is used to read metadata. "Emit" is used to write metadata.

获取有关枚举的信息(自动,无)

我们现在有一个读者.除了枚举程序集中的类型外,我还可以直接跳到这个问题的有趣的地方:0x02000006:

//Get metadata for enum Windows.UI.Xaml.ApplicationHighContrastAdjustment
mdToken tokenID = 0x02000006; //Windows.UI.Xaml.ApplicationHighContrastAdjustment

//btw, this is all hypothetical code that is vaguely C#/Java-like.

Pointer enum = null;
mdToken memberID;
int nCount;
while (reader.EnumMembers(ref enum, tokenID, out memberID, 1, out nCount) == S_OK)
{
   //out MemberID receives the TokenID of each member of the enumeration
}
reader.CloseEnum(enum);

EnumMembers的调用返回了枚举的三个成员:

The call to EnumMembers returns us the three members of the enumeration:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆