mardi 10 décembre 2019

Mono.Cecil : Get generic parameter name

We are coding a project that check if there are references problems within our nuget packages. So we decided to use Mono.cecil to extract every method calls, and then check if we find a method that suits the call.

While extracting every method calls, I obtain for example

string fullName = !!0 Extensions::MinBy(System.Collections.Generic.IEnumerable`1<!!0>,System.Func`2<!!0,!!1>)

Where !!0 and !!1 are generics arguments for the method :

public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> selector)

I was wondering if there was a way to obtain instead :

TSource Extensions::MinBy(System.Collections.Generic.IEnumerable`1<TSource>,System.Func`2<TSource, TKey>)
                AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path);
                foreach (ModuleDefinition module in assembly.Modules)
                {
                    foreach (TypeDefinition type in module.Types)
                    {
                        foreach (MethodDefinition method in type.Methods.Where(x => x.HasBody))
                        {
                            foreach (var il in method.Body.Instructions)
                            {
                                if (il.OpCode == OpCodes.Call || il.OpCode == OpCodes.Calli || il.OpCode == OpCodes.Callvirt)
                                {
                                    var mRef = il.Operand as MethodReference;

                                    string fullName = mRef.GetElementMethod().FullName; // This is where i get !!0 Extensions::MinBy(System.Collections.Generic.IEnumerable`1<!!0>,System.Func`2<!!0,!!1>)

                                    **//TODO : Find a way to obtain parameters names**

                                }
                            }
                        }
                    }
                }

Thanks a lot





Aucun commentaire:

Enregistrer un commentaire