从程序集中提取特定 IL(.NET 中间语言)签名的机制
我在 Microsoft .NET 程序集 mscorlib.dll 中找到了大约 25 种类型的列表,我需要在其中提取类及其成员的 IL 签名。我想要每种类型一个文件,每个签名占一行。因此,例如,采用
System.Collections.Generic.Comparer<T>
我想要从该类型的程序集中提取以下内容的类型(有一些我不需要的私有成员,但如果需要,我可以手动处理)。
.class public abstract auto ansi serializable beforefieldinit System.Collections.Generic.Comparer`1<T> extends System.Object implements System.Collections.IComparer, class System.Collections.Generic.IComparer`1<!T>
.method family hidebysig specialname rtspecialname instance void .ctor() cil managed
.method public hidebysig newslot abstract virtual instance int32 Compare(!T x, !T y) cil managed
.method private hidebysig newslot virtual final instance int32 System.Collections.IComparer.Compare(object x, object y) cil managed
.property class System.Collections.Generic.Comparer`1<!T> Default() { .get class System.Collections.Generic.Comparer`1<!T> System.Collections.Generic.Comparer`1::get_Default() }
因此,我考虑了四种实现此目的的方法:
手动将每种类型的每个签名剪切并粘贴到 ildasm 的文本文件中(直接或通过 mscorlib.dll 转储)。有些类型非常长,因此这可能会变得乏味。如果我需要做更多事情,这绝对不是一个好的长期解决方案。
编写一个使用反射来尝试获取所有签名的工具。我在较高层面上考虑过使用类似下面的伪代码 [1] 的东西。
编写一个工具,在 ildasm 的 IL 转储上使用字符串解析来查找类型并获取签名
- < p>使用现有的工具来完成任务。
有谁知道现有的工具或机制可以帮助我完成任务?如果没有,关于制作这样一个工具的方向有什么建议吗?如果您对如何完成它有任何代码想法,我将不胜感激。
[1]
System.Reflection.GetType(string)
Type.GetMethods() For each item in
MethodInfo, GetMethodBody()
GetILAsByteArray().ToString()
Determine a way to get just the signature from the IL
I have a list of about 25 types found in the Microsoft .NET assembly mscorlib.dll where I need to extract the IL signatures of the class and its members. I want one file per type, with each signature on one line. So, for example, take the type
System.Collections.Generic.Comparer<T>
I want to extract the following from the assembly for that type (there are some private members I won't need, but I can handle that manually if needed).
.class public abstract auto ansi serializable beforefieldinit System.Collections.Generic.Comparer`1<T> extends System.Object implements System.Collections.IComparer, class System.Collections.Generic.IComparer`1<!T>
.method family hidebysig specialname rtspecialname instance void .ctor() cil managed
.method public hidebysig newslot abstract virtual instance int32 Compare(!T x, !T y) cil managed
.method private hidebysig newslot virtual final instance int32 System.Collections.IComparer.Compare(object x, object y) cil managed
.property class System.Collections.Generic.Comparer`1<!T> Default() { .get class System.Collections.Generic.Comparer`1<!T> System.Collections.Generic.Comparer`1::get_Default() }
So, I have thought about four ways of accomplishing this:
Manually cut and paste each signature from each type by hand into the text files from ildasm (either directly or via a dump of mscorlib.dll). Some of the types are pretty long, so this could get tedious. And this is definitely not a good long-term solution if I need to do more.
Write a tool that uses Reflection to try to get all of the signatures. I have, at a high level, thought about using something like the psuedo-code [1] below.
Write a tool that uses string parsing on the IL dump of ildasm to find the types and get the signatures
Use an already existing tool to accomplish the task.
Does anyone know of an existing tool or mechanism that might help me accomplish my task? If not, any suggestions on which direction to go to get such a tool made? And if you have any code ideas about how to get it done, I would appreciate hearing them.
[1]
System.Reflection.GetType(string)
Type.GetMethods() For each item in
MethodInfo, GetMethodBody()
GetILAsByteArray().ToString()
Determine a way to get just the signature from the IL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以根据 MethodInfo 中的信息重建方法签名。您不需要 GetMethodBody - 那里没有签名信息。 MemberInfo.Name、MemberInfo.ReturnType、MemberInfo.GetParameters() 等都在那里。
You can reconstruct the method signature from the information found in MethodInfo. You don't need GetMethodBody - there's no signature info in there. MemberInfo.Name, MemberInfo.ReturnType, MemberInfo.GetParameters(), etc. It's all there.
红色门反射器可以使剪切和粘贴速度更快。
red gate reflector could make cut and paste go a lot quicker.
如果您决定从 IL 获取签名,则需要编写某种签名解析器。 David Broman 在 此处 上发布了一个示例。它的功能并不完整(具体来说,您必须添加对泛型的支持),但它确实为您提供了一个良好的开端。
If you decide to get the signature from the IL, you will need to write some kind of signature parser. David Broman has a sample one posted up on MSDN code gallery here. It's not fully functional (specifically, you'll have to add support for generics), but it does give you a head start.