Following up with another question - suppose that i do not have the control to supply the assembly resolver, how can i compare 2 MethodDefinitions that may be a result of the same assembly being loaded twice (reference equality won't help in that case) ?
Is there any way of "importing" the Method into one assembly and then check for equality ? On Fri, Nov 28, 2014 at 12:57 PM, Lior Tal <[email protected]> wrote: > Thanks! that did the trick. > > On Fri, Nov 28, 2014 at 11:44 AM, Jb Evain <[email protected]> wrote: > >> To add more detail: >> >> Basically, whenever you write AssemblyDefinition.ReadAssembly >> yourself, the assembly is not given to the resolver, as it wouldn't >> know what to do. >> >> Rule of thumb for the pattern to work: >> >> * If you're calling ReadAssembly yourself, make sure to cache the >> assembly in the resolver if you want it to be used directly. Otherwise >> the resolver is free to open another instance. >> >> Jb >> >> On Fri, Nov 28, 2014 at 10:40 AM, Jb Evain <[email protected]> wrote: >> > The pattern should work, I use it almost everywhere :) >> > >> > There's one thing to note: when an AssemblyResolver is created, no >> > assemblies are automatically registered with it. >> > >> > I take it you're using: >> > >> > >> https://github.com/mono/mono-tools/blob/master/gendarme/framework/Gendarme.Framework/AssemblyResolver.cs >> > >> > So just write: >> > >> > AssemblyResolver.CacheAssembly (original); >> > >> > And now assembly resolving from AssemblyResolver.Resolver will resolve >> > using the cached original.dll. >> > >> > Jb >> > >> > On Fri, Nov 28, 2014 at 10:07 AM, Lior Tal <[email protected]> wrote: >> >> I tried to do what you suggested but it doesn't work: >> >> >> >> // Get a MethodDefinition from this assembly >> >> var assembly = AssemblyDefinition.ReadAssembly ("Original.dll", new >> >> ReaderParameters { AssemblyResolver = AssemblyResolver.Resolver }); >> >> >> >> .... >> >> >> >> // Get MethodReferences from this assembly, resolve them and compare >> using >> >> reference equality. >> >> var otherAssembly = AssemblyDefinition.ReadAssembly ("Scanned.dll", new >> >> ReaderParameters { AssemblyResolver = ar }); >> >> >> >> However i am getting no matches (no 2 instances that i compare are >> equal). >> >> >> >> On Thu, Nov 27, 2014 at 11:11 AM, Lior Tal <[email protected]> >> wrote: >> >>> >> >>> Yes i figured that's an ugly hack. I will follow your tips to do it >> >>> properly. >> >>> >> >>> >> >>> >> >>> On Thu, Nov 27, 2014 at 11:08 AM, Jb Evain <[email protected]> wrote: >> >>>> >> >>>> Yeah it doesn't :) >> >>>> >> >>>> Two obvious cases: >> >>>> >> >>>> 1) type will the same full name can be defined in multiple >> assemblies. >> >>>> 2) the name of generic parameter doesn't cross assemblies boundaries, >> >>>> so if you work on the defining assembly you can see an instantiation >> >>>> of Dictionary<TKey, TValue>, but in another assembly you'll see a >> >>>> Dictionary<!0, !1> for instance. >> >>>> >> >>>> And probably more I forgot right now. >> >>>> >> >>>> Jb >> >>>> >> >>>> >> >>>> On Thu, Nov 27, 2014 at 10:03 AM, Lior Tal <[email protected]> >> wrote: >> >>>> > Thanks i will look into that. >> >>>> > >> >>>> > As a hack for now i compare the ToString() of both types as it >> seems to >> >>>> > return the same method signature :) not sure whether that holds >> for all >> >>>> > cases... >> >>>> > >> >>>> > On Thu, Nov 27, 2014 at 10:59 AM, Jb Evain <[email protected]> >> wrote: >> >>>> >> >> >>>> >> Hey, >> >>>> >> >> >>>> >> Comparing metadata for equality is pretty hard, as not everyone >> has >> >>>> >> the same definition (there's a pretty good thread on this group >> about >> >>>> >> it). >> >>>> >> >> >>>> >> The way I usually implement it is as follow: >> >>>> >> >> >>>> >> * I maintain a shared assembly resolver to make sure that >> assemblies >> >>>> >> are never loaded twice. >> >>>> >> * This way when you encounter a MethodReference, you can Resolve >> it. >> >>>> >> * Then you can compare the MethodDefinition by reference equality. >> >>>> >> * For generic instances, you will need to still compare the >> resolved >> >>>> >> MethodDefinition by reference, and on top of that compare the >> >>>> >> instantiation. You can do the same trick to compare resolved >> types by >> >>>> >> identity. >> >>>> >> >> >>>> >> Jb >> >>>> >> >> >>>> >> On Wed, Nov 26, 2014 at 10:41 PM, Lior Tal <[email protected]> >> >>>> >> wrote: >> >>>> >> > Hey, >> >>>> >> > >> >>>> >> > I have a MethodReference that i'd like to search for in other >> >>>> >> > assemblies. >> >>>> >> > >> >>>> >> > I am iterating over all types, methods with bodies and >> instructions >> >>>> >> > to >> >>>> >> > find >> >>>> >> > the MethodReference usage as the instruction's operand. >> >>>> >> > >> >>>> >> > How do i compare it with the original MethodReference instance >> that >> >>>> >> > i >> >>>> >> > already have, to see that they match ? >> >>>> >> > >> >>>> >> > Reference equality does not work obviously, what is the proper >> way >> >>>> >> > of >> >>>> >> > doing >> >>>> >> > that ? >> >>>> >> > >> >>>> >> > --Lior >> >>>> >> > >> >>>> >> > -- >> >>>> >> > -- >> >>>> >> > -- >> >>>> >> > mono-cecil >> >>>> >> > --- >> >>>> >> > You received this message because you are subscribed to the >> Google >> >>>> >> > Groups >> >>>> >> > "mono-cecil" group. >> >>>> >> > To unsubscribe from this group and stop receiving emails from >> it, >> >>>> >> > send >> >>>> >> > an >> >>>> >> > email to [email protected]. >> >>>> >> > For more options, visit https://groups.google.com/d/optout. >> >>>> >> >> >>>> >> -- >> >>>> >> -- >> >>>> >> -- >> >>>> >> mono-cecil >> >>>> >> --- >> >>>> >> You received this message because you are subscribed to the Google >> >>>> >> Groups >> >>>> >> "mono-cecil" group. >> >>>> >> To unsubscribe from this group and stop receiving emails from it, >> send >> >>>> >> an >> >>>> >> email to [email protected]. >> >>>> >> For more options, visit https://groups.google.com/d/optout. >> >>>> > >> >>>> > >> >>>> > -- >> >>>> > -- >> >>>> > -- >> >>>> > mono-cecil >> >>>> > --- >> >>>> > You received this message because you are subscribed to the Google >> >>>> > Groups >> >>>> > "mono-cecil" group. >> >>>> > To unsubscribe from this group and stop receiving emails from it, >> send >> >>>> > an >> >>>> > email to [email protected]. >> >>>> > For more options, visit https://groups.google.com/d/optout. >> >>>> >> >>>> -- >> >>>> -- >> >>>> -- >> >>>> mono-cecil >> >>>> --- >> >>>> You received this message because you are subscribed to the Google >> Groups >> >>>> "mono-cecil" group. >> >>>> To unsubscribe from this group and stop receiving emails from it, >> send an >> >>>> email to [email protected]. >> >>>> For more options, visit https://groups.google.com/d/optout. >> >>> >> >>> >> >> >> >> -- >> >> -- >> >> -- >> >> mono-cecil >> >> --- >> >> You received this message because you are subscribed to the Google >> Groups >> >> "mono-cecil" group. >> >> To unsubscribe from this group and stop receiving emails from it, send >> an >> >> email to [email protected]. >> >> For more options, visit https://groups.google.com/d/optout. >> >> -- >> -- >> -- >> mono-cecil >> --- >> You received this message because you are subscribed to the Google Groups >> "mono-cecil" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > -- -- -- mono-cecil --- You received this message because you are subscribed to the Google Groups "mono-cecil" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
