public RuleResult CheckType(TypeDefinition type) { // This rule only applies to public interfaces with methods and explicit ComVisible. if (!type.IsInterface || !type.IsPublic || !type.HasMethods || !type.HasCustomAttributes) return RuleResult.DoesNotApply; // If no explicit ComVisible is found, we still need to check. // If we're explicitly invisible, we can assume success. bool wasExplicit; if (!type.IsComVisible (out wasExplicit) && wasExplicit) return RuleResult.Success; methods.Clear (); foreach (MethodDefinition method in type.Methods) { if (methods.Contains (method.Name) && !(!method.IsComVisible(out wasExplicit) && wasExplicit)) Runner.Report (method, Severity.Critical, Confidence.Total); else methods.Add (method.Name); } return Runner.CurrentRuleResult; }
public RuleResult CheckType (TypeDefinition type) { // Only check for value types and types with fields. // But also for types with attributes, since FXCop only issues a warning if ComVisible is explicitly defined. if (!type.IsValueType || !type.HasCustomAttributes || !type.HasFields) return RuleResult.DoesNotApply; // Ensure the defining assembly is not null, then perform the same check for attributes on the assembly. AssemblyDefinition assembly = type.GetAssembly (); if (assembly != null && !assembly.HasCustomAttributes) return RuleResult.DoesNotApply; // Iterate through attributes on the type and assembly to ensure that ComVisible is false on the assembly, // and true on the type. bool exp; if (assembly != null) { if (assembly.IsComVisible (out exp) && exp) return RuleResult.Success; } if (!type.IsComVisible (out exp) && exp) return RuleResult.Success; // If we find any, low severity as the code works, but it's bad practice. foreach (FieldDefinition field in type.Fields) if (!field.IsPublic) Runner.Report (field, Severity.Low, Confidence.Total); return Runner.CurrentRuleResult; }
public RuleResult CheckType (TypeDefinition type) { if (!type.IsValueType || !type.HasCustomAttributes || (!type.IsPublic && !type.IsNestedPublic) || type.HasGenericParameters) return RuleResult.DoesNotApply; AssemblyDefinition assembly = type.Module.Assembly; bool expl; // return if assembly is ComVisible // or not visible but was not marked explicitly if (assembly.IsComVisible (out expl) || !expl) return RuleResult.DoesNotApply; // type should be explicitly marked as ComVisible, return otherwise if (!(type.IsComVisible (out expl) && expl)) return RuleResult.DoesNotApply; if (type.IsAutoLayout) Runner.Report (type, Severity.High, Confidence.High); return Runner.CurrentRuleResult; }