What about just looking AssemblyReferences and MemberReferences?

On Wed, Jul 16, 2008 at 11:43 AM, Lev <[EMAIL PROTECTED]> wrote:

>
> Hi, guys,
>
> While working on one of my projects I came up with a code to check if
> one type
> depends on another type, so I made an extension method out of this
> code.
>
> The thing is that I'm pretty new to Cecil and, since there is no
> documentation
> available, I figured my way out with Object Browser and debugger.
>
> So I want to show the code to more experienced cecil programmers, like
> you all,
> and ask for your opinion. Moreover, if this functionality is useful
> and does not
> violate the goal of cecil, it might end up as a patch to cecil.
>
> What do you think JB?
>
> Currently the method checks the dependency with the following
> definition:
> Type A depends on type B if:
> 1. A is direct subtype of B, or implements B, or subclasses generic
> collection of B
> 2. A declares field of type B
> 3. A contains a method or constructor that:
>        a. returns B
>        b. has parameter of type B
>        c. throws an exception of type B
>        d. calls a method declared in B
>        e. accessess a field declared in B
>
> The code in generics aware, so cases when, e.g. a generic collection
> of type B is declared
> or return type is generic collection of B, will also be considered as
> dependant.
>
> Another bonus extension method is IsSubType which might be useful :)
>
> ===============================================================================
> public static class TypeDefinitionExtensions
> {
>    public static bool DependsOn(this TypeDefinition thistype,
> TypeDefinition type)
>    {
>        if ((thistype == type) || type == null) return false;
>
>        // Check if thistype is subtype of type, or implements type
>        // (if type is an interface)
>        if (thistype.IsSubType(type)) return true;
>
>        // Check if thistype has fields referencing type
>        foreach (FieldDefinition field in thistype.Fields)
>        {
>            if (CheckTypes(field.FieldType, type)) return true;
>        }
>
>        // Check if methods in thistype depend on type
>        return ProcessMethods(thistype, type);
>    }
>
>    /// <summary>
>    /// Returns true if current type is a subtype of type, or
> implements type,
>    /// otherwise returns true
>    /// </summary>
>    public static bool IsSubType(this TypeDefinition thistype,
> TypeDefinition type)
>    {
>        if (type.IsInterface && thistype.Interfaces.Contains(type))
> return true;
>
>        return CheckTypes(thistype.BaseType, type);
>    }
>
>    /// <summary>
>    /// Returns true if any methods or constructors in type1 have
> references to type,
>    /// otherwise returns false;
>    /// </summary>
>    private static bool ProcessMethods(TypeDefinition type1,
> TypeReference type2)
>    {
>        var methods = new List<MethodDefinition>();
>        methods.AddRange(type1.Methods.Cast<MethodDefinition>());
>        methods.AddRange(type1.Constructors.Cast<MethodDefinition>());
>
>        foreach (var method in methods)
>        {
>            // Check if type1 has methods with return types of type2
>            if (CheckTypes(method.ReturnType.ReturnType, type2))
> return true;
>
>            // Check if type1 has methods with parameters of type2
>            foreach (ParameterDefinition parameter in
> method.Parameters)
>            {
>                if (CheckTypes(parameter.ParameterType, type2)) return
> true;
>            }
>
>            // Check if method body has any references to type
>            if (!method.HasBody) continue;
>
>            foreach (Instruction instruction in
> method.Body.Instructions)
>            {
>                var methodOp = instruction.Operand as MemberReference;
>                if (methodOp != null && methodOp.DeclaringType ==
> type2) return true;
>            }
>        }
>
>        return false;
>    }
>
>    /// <summary>
>    /// Returns true if type1 is a GenericInstance type and type2 is
>    /// in generic arguments list for type1, or type1 is type2,
>    /// otherwise returns false.
>    /// </summary>
>    private static bool CheckTypes(TypeReference type1, TypeReference
> type2)
>    {
>        var genType = type1 as GenericInstanceType;
>
>        if (genType != null &&
> genType.GenericArguments.Contains(type2)) return true;
>
>        return type1 == type2;
>    }
> }
>
> ===============================================================================
>
> Thanks and regards,
> Lev
>
> PS. Sorry for inline code, I did not find a way to attach a file in
> google-groups web interface.
> >
>


-- 
Jair Cazarin Villanueva
http://jaircazarin.blogspot.com

--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to