问题
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
回答1:
red gate reflector could make cut and paste go a lot quicker.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/3622760/mechanism-to-extract-specific-il-net-intermediate-language-signatures-from-an