예제 #1
0
        public static List <MethodDetail> GetMethodsCalledInsideMethod(MethodDefinition methodDefinition, Func <MethodReference, bool> condition)
        {
            try
            {
                var methodsCalled = new List <MethodDetail>();
                if (methodDefinition.Body != null)
                {
                    SequencePoint currentSequencePoint = null;
                    foreach (Instruction instruction in methodDefinition.Body.Instructions)
                    {
                        currentSequencePoint = instruction.SequencePoint ?? currentSequencePoint;
                        if (instruction.Operand != null)
                        {
                            if (instruction.Operand is MethodReference)
                            {
                                MethodReference operand = (instruction.Operand as MethodReference);

                                try
                                {
                                    operand = operand.Resolve();
                                }
                                catch (AssemblyResolutionException)
                                {
                                }

                                if (operand == null)
                                {
                                    operand = instruction.Operand as MethodReference;
                                }

                                if (!operand.FullName.Equals(methodDefinition.FullName) && condition(operand))
                                {
                                    var itemToBeAdded = MethodDetailBuilder.Create(operand);
                                    if (!methodsCalled.Contains(itemToBeAdded))
                                    {
                                        methodsCalled.Add(itemToBeAdded);
                                    }
                                }
                            }
                        }
                    }
                }

                return(methodsCalled);
            }
            catch (Exception ex)
            {
                Log.Exception(ex, "in CecilUtils.getMethodsCalledInsideMethod");
                return(null);
            }
        }
        public static MethodDetail Create(MethodReference reference, List <MethodDetail> usedMethods)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(reference.DeclaringType.FullName);
            builder.Append("::");
            builder.Append(reference.Name);
            if (reference.HasGenericParameters)
            {
                GetGenericMethodInstanceFullName(reference.GenericParameters, builder);
            }
            GetParameters(reference, builder);


            string fullName = reference.FullName;

            return(new MethodDetail(fullName, builder.ToString(), usedMethods, MethodDetailBuilder.Create(reference.DeclaringType)));
        }
예제 #3
0
 public static List <MethodDetail> GetMethodsInsideAssembly(String assemblyToLoad, bool includeMethodDefinition, string filterValue)
 {
     try
     {
         Func <MethodReference, bool> condition   = (item => item.FullName.Contains(filterValue ?? string.Empty));
         List <MethodDefinition>      definitions = GetMethodsInsideAssembly(assemblyToLoad, includeMethodDefinition, condition);
         var methodsCalled = new List <MethodDetail>();
         foreach (var item in definitions)
         {
             methodsCalled.Add(MethodDetailBuilder.Create(item));
         }
         return(methodsCalled);
     }
     catch (Exception ex)
     {
         Log.Exception(ex, "in CecilUtils.getMethodsCalledInsideAssembly");
         return(null);
     }
 }
예제 #4
0
 public static List <MethodDetail> GetMethodsCallingOperationContracts(string assemblyToLoad)
 {
     try
     {
         List <MethodDefinition> definitions = GetMethodsInsideAssembly(assemblyToLoad, false, item => true);
         var methodsCalled = new List <MethodDetail>();
         foreach (var item in definitions)
         {
             var operationContractsCalled = GetMethodsCalledInsideMethod(item, CecilUtil.IsOperationContract);
             if (operationContractsCalled != null && operationContractsCalled.Count > 0)
             {
                 methodsCalled.Add(MethodDetailBuilder.Create(item, operationContractsCalled));
             }
         }
         return(methodsCalled);
     }
     catch (Exception ex)
     {
         Log.Exception(ex, "in CecilUtils.GetMethodsCallingOperationContracts");
         return(null);
     }
 }
예제 #5
0
        public static Dictionary <MethodDetail, bool> GetServiceLevelOperationContracts(string assemblyToLoad, string filter)
        {
            try
            {
                //List<MethodDefinition> definitions = GetMethodsInsideAssembly(assemblyToLoad, false, item => item.FullName.Contains(filter ?? string.Empty) && (item as MethodDefinition).DeclaringType.Interfaces.Any(mItem=>CecilUtil.HasServiceContractAttribute(mItem.Resolve())));
                List <MethodDefinition>         definitions = GetMethodsInsideAssembly(assemblyToLoad, true, item => !(item as MethodDefinition).HasBody && item.FullName.Contains(filter ?? string.Empty) && CecilUtil.HasServiceContractAttribute((item as MethodDefinition).DeclaringType.Resolve()));
                Dictionary <MethodDetail, bool> result      = new Dictionary <MethodDetail, bool>();

                foreach (var item in definitions)
                {
                    if (!item.IsConstructor)
                    {
                        result.Add(MethodDetailBuilder.Create(item), CecilUtil.IsServiceLevelOperationContract(item));
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                Log.Exception(ex, "in CecilUtils.GetMethodsCallingOperationContracts");
                return(null);
            }
        }