Exemple #1
0
        public static void Analyze(INameService service, ICollection <ModuleDefMD> modules, TypeDef type)
        {
            if (type.IsInterface)
            {
                return;
            }

            var vTbl = service.GetVTables()[type];

            foreach (var ifaceVTbl in vTbl.InterfaceSlots.Values)
            {
                foreach (var slot in ifaceVTbl)
                {
                    if (slot.Overrides == null)
                    {
                        continue;
                    }
                    Debug.Assert(slot.Overrides.MethodDef.DeclaringType.IsInterface);
                    // A method in base type can implements an interface method for a
                    // derived type. If the base type/interface is not in our control, we should
                    // not rename the methods.
                    bool baseUnderCtrl      = modules.Contains(slot.MethodDef.DeclaringType.Module as ModuleDefMD);
                    bool interfaceUnderCtrl = modules.Contains(slot.Overrides.MethodDef.DeclaringType.Module as ModuleDefMD);
                    if (!baseUnderCtrl && interfaceUnderCtrl || !service.CanRename(slot.MethodDef))
                    {
                        service.SetCanRename(slot.Overrides.MethodDef, false);
                    }
                    else if ((baseUnderCtrl && !interfaceUnderCtrl) || (!service.CanRename(slot.Overrides.MethodDef)))
                    {
                        service.SetCanRename(slot.MethodDef, false);
                    }

                    // Now it is possible that the method implementing the interface, belongs to the base class.
                    // If that happens the methods analyzing the methods will not pick up on this. We'll mark that
                    // case here.
                    if (!TypeEqualityComparer.Instance.Equals(slot.MethodDef.DeclaringType, type))
                    {
                        SetupOverwriteReferences(service, modules, slot, type);
                        //CreateOverrideReference(service, slot.MethodDef, slot.Overrides.MethodDef);
                    }

                    // For the case when method in base type implements an interface method for a derived type
                    // do not consider method parameters to make method name the same in base type, derived type and interface
                    var methodDef  = slot.MethodDef;
                    var typeDef    = type.BaseType?.ResolveTypeDef();
                    var baseMethod = typeDef?.FindMethod(methodDef.Name, methodDef.Signature as MethodSig);
                    if (baseMethod != null)
                    {
                        string unifiedName = service.GetNormalizedName(slot.Overrides.MethodDef);
                        service.SetNormalizedName(slot.MethodDef, unifiedName);
                        service.SetNormalizedName(baseMethod, unifiedName);
                    }
                }
            }
        }