none
How check whether specific type of variable is used/defined in a function or not using Reflection? RRS feed

  • Question

  • I have one .Net Assembly ie. exe.

    I am using Reflection to list classes, methods, properties, etc.

    Now i want to check whether specific type of variable is used/defined in a function or not.

    Actually i want to know whether one class is uses instance of other class or not. So that i can know dependency between two classes.

    So, is it possible using Reflection in .net?

    Thursday, February 21, 2013 6:46 AM

Answers

  • Hi,

    There's one thing really easy to do and one pretty hard. Using reflection you can easily enumerate the fields, properies and methods of the class: 

    •  GetFields (Public | NonPublic, ...)
    • GetProperties (...)
    • GetMethods(...)

    You will get objects that have properties/methods that give you access to the Type. For exemple when you use GetField(), you retreive a 'FieldInfo' that have a property 'FieldType' and is this type is equal to 'typeof(SomeClass)' you know your dependency. Quite the same thing for the Properties. 

    From the GetMethod() you will get a "MethodInfo" that have functions GetParameter, that will return a 'ParameterInfo' which will give you the information of type.

    Now, if you want to detect that in a method, there is an instruction 'new SomeClass', this is the hard thing to do. You will need to analyze analyze the code starting with taking IL and get the code from IL. I've done this a long time ago using a tier library. But I don't remember which one...

    Jojo

    Thursday, February 21, 2013 1:10 PM