public RuleResult CheckType (TypeDefinition type) { if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; ClassInterfaceType? attributeValue = GetClassInterfaceAttributeValue (type); bool fromAssembly = false; // attribute not found on class, try assembly instead if (attributeValue == null) { attributeValue = GetClassInterfaceAttributeValue (type.Module.Assembly); fromAssembly = true; } // not found on assembly as well, set default value if (attributeValue == null) attributeValue = ClassInterfaceType.AutoDispatch; if (attributeValue == ClassInterfaceType.AutoDual) { if (fromAssembly) Runner.Report (type, Severity.High, Confidence.High, "Attribute was set on assembly level"); else Runner.Report (type, Severity.High, Confidence.High); return RuleResult.Failure; } else return RuleResult.Success; }
public RuleResult CheckType(TypeDefinition type) { // Check only for reference types with attributes. if (!type.IsClass || type.IsValueType || !type.HasCustomAttributes) return RuleResult.DoesNotApply; // Ensure class is explicitly ComVisible. if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; // Report success if a default public constructor is found or no parameterized constructor is found. bool hasParameterizedCtor = false; bool hasDefaultCtor = false; foreach (var ctor in type.Methods) { if (!ctor.IsConstructor) continue; if (ctor.IsPublic && ctor.HasParameters) { hasParameterizedCtor = true; continue; } if (ctor.IsPublic) hasDefaultCtor = true; } if (!hasParameterizedCtor || hasDefaultCtor) return RuleResult.Success; Runner.Report (type, Severity.Medium, Confidence.Total); 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; if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; // 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.BaseType == null) return RuleResult.DoesNotApply; // Checks whether specific type is COM visible or not // considering nested types, assemblies attributes and default values if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; TypeDefinition baseType = type.BaseType.Resolve (); if ((baseType != null) && !baseType.IsTypeComVisible ()) { Runner.Report (type, Severity.High, Confidence.Total, String.Format ("Type is derived from invisible from COM type {0}", baseType.FullName)); } return Runner.CurrentRuleResult; }
public RuleResult CheckType(TypeDefinition type) { if (type.IsEnum || !type.IsValueType || !type.HasCustomAttributes || (!type.IsPublic && !type.IsNestedPublic) || type.HasGenericParameters) return RuleResult.DoesNotApply; if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; if (type.IsAutoLayout) Runner.Report (type, Severity.High, Confidence.High); return Runner.CurrentRuleResult; }
public RuleResult CheckType (TypeDefinition type) { if (type.HasGenericParameters || !type.IsVisible () || !type.IsTypeComVisible ()) return RuleResult.DoesNotApply; bool foundRegister = false; // type level variables bool foundUnregister = false; foreach (MethodDefinition method in type.Methods) { if (!method.HasCustomAttributes) continue; bool foundRegisterUnregisterMethod = false; // method level variable foreach (CustomAttribute attribute in method.CustomAttributes) { var name = attribute.AttributeType.FullName; if (!foundRegister && name == comRegister) { foundRegister = true; foundRegisterUnregisterMethod = true; } if (!foundUnregister && name == comUnregister) { foundUnregister = true; foundRegisterUnregisterMethod = true; } } if (foundRegisterUnregisterMethod && method.IsVisible ()) { Runner.Report (method, Severity.High, Confidence.High, "Method is marked with the ComRegisterFunctionAttribute or with the ComUnregisterFunctionAttribute and is externally visible"); } } if (foundRegister ^ foundUnregister) { // only one of them is true if (foundRegister) Runner.Report (type, Severity.High, Confidence.High, "Type contains has a method with ComRegisterFunctionAttribute but it doesn't contain a method with ComUnregisterFunctionAttribute"); if (foundUnregister) Runner.Report (type, Severity.High, Confidence.High, "Type contains has a method with ComUnregisterFunctionAttribute but it doesn't contain a method with ComRegisterFunctionAttribute"); } return Runner.CurrentRuleResult; }
public RuleResult CheckType(TypeDefinition type) { // This rule only applies to public interfaces with methods and explicit ComVisible. if (!type.IsInterface || !type.IsPublic || !type.HasMethods) return RuleResult.DoesNotApply; if (!type.IsTypeComVisible ()) return RuleResult.DoesNotApply; methods.Clear (); foreach (MethodDefinition method in type.Methods) { var name = method.Name; if (methods.Contains (name) && (method.IsComVisible () ?? true)) Runner.Report (method, Severity.Critical, Confidence.Total); else methods.Add (name); } return Runner.CurrentRuleResult; }