Sure it's possible. ILSpy does it for instance, you can have a look at
how they do it.

Jb

On Mon, Sep 17, 2012 at 6:17 PM, Dzmitry Lahoda <[email protected]> wrote:
> Yeah, I used ildasm to see 0 or 1 is used really.
> Seems possible to get methods that use enum and scan through IL of methods
> to search for enum member usage through Cecil API?  Worth to try?
>
> .method public hidebysig static void  Consume(valuetype
> [Test.ServiceProvider]Test.ServiceProvider.Enum1 e) cil managed
> {
>   // Code size       15 (0xf)
>   .maxstack  2
>   .locals init ([0] bool CS$4$0000)
>   IL_0000:  nop
>   IL_0001:  ldarg.0
>   IL_0002:  ldc.i4.0
>   IL_0003:  ceq
>   IL_0005:  ldc.i4.0
>   IL_0006:  ceq
>   IL_0008:  stloc.0
>   IL_0009:  ldloc.0
>   IL_000a:  brtrue.s   IL_000e
>   IL_000c:  nop
>   IL_000d:  nop
>   IL_000e:  ret
> } // end of method ClassEnum1Consumer::Consume
>
>
>
> On Monday, September 17, 2012 7:06:46 PM UTC+3, Jb Evain wrote:
>>
>> Hey Dzmitry,
>>
>> Most compilers inline the constant value of the enum, which is why you
>> don't find references of them in the IL.
>>
>> Jb
>>
>> On Mon, Sep 17, 2012 at 6:03 PM, Dzmitry Lahoda <[email protected]>
>> wrote:
>> > I am trying to build something like NDepend for my needs.
>> > I have enum and trying to find usages of its members by some of dlls.
>> > Only
>> > usage of whole enum can be finded.
>> > This is real case (host and 6 custom addins).
>> >
>> > Sample code:
>> >
>> >     public enum Enum1
>> >     {
>> >         Val1,
>> >         Val2
>> >     }
>> >
>> >     public class ClassEnum1Consumer
>> >     {
>> >         public static void Consume(Enum1 e)
>> >         {
>> >             if (e == Enum1.Val2)
>> >             {
>> >
>> >             }
>> >         }
>> >     }
>> >
>> >    [<Test>] member fixture.``One usage of enum by consumer`` ()=
>> >            let result =
>> >
>> > SharpDepend.findUsages("Test.ServiceProvider.Enum1","Val2",["Test.ServiceConsumer1"])
>> >            result.Length |> should equal 1
>> >
>> >
>> > let findUsages
>> > (usedType:string,usedMemeber:string,consumers:list<string>) =
>> >     let allAssemblies = consumers |> List.map (fun x-> List.ofArray
>> > (get_assemblies(x))) |> List.collect (fun x-> x)
>> >     let filter (assembly:AssemblyDefinition) =
>> >       assembly.MainModule.GetMemberReferences()
>> >       |> Seq.filter (fun x-> x.DeclaringType.FullName.Contains(usedType)
>> > &&
>> > x.Name.Contains(usedMemeber))
>> >     // for enums - enum members fail by mono cecil
>> >     let filter_type (assembly:AssemblyDefinition) =
>> >       assembly.MainModule.GetTypeReferences()
>> >       |> Seq.filter (fun x-> x.FullName.Contains(usedType) &&
>> > usedMemeber.Equals(String.Empty))
>> >     let usedTypes = allAssemblies |> List.filter ( fun x-> Seq.length
>> > (filter(x)) >0 || Seq.length (filter_type(x)) >0)
>> >     usedTypes
>> >
>> > Result:
>> >
>> > Expected: 1
>> >   But was:  0
>> >
>> > Previous code works for enum type or usual classes' members.
>> >
>> > Link:
>> > https://github.com/asd-and-Rizzo/SharpDepend
>> >
>> >
>> > --
>> > --
>> > mono-cecil
>
> --
> --
> mono-cecil

-- 
--
mono-cecil

Reply via email to