static bool CanInstantiateType (TypeDefinition type) { // type is static (>= 2.0) if (type.IsStatic ()) return false; if (type.IsSealed && type.HasMethods) { foreach (MethodDefinition ctor in type.Methods) { if (ctor.IsConstructor && ctor.IsVisible ()) return true; } return false; } return true; }
private static bool DoesTypeStealthilyImplementInterface (TypeDefinition type, TypeReference iface) { //ignore already uninteresting types below (self, enum, struct, static class) if (type == iface || type.IsEnum || type.IsValueType || type.IsStatic ()) return false; //if type has less methods than the interface no need to check further if (!type.HasMethods) return false; IList<MethodDefinition> mdc = iface.GetMethods ().ToList (); if (type.Methods.Count < mdc.Count) return false; //type already publicly says it implements the interface if (type.Implements (iface.FullName)) return false; foreach (MethodDefinition m in mdc) { //if any candidate fails we can return right away //since the interface will never be fully implemented MethodDefinition candidate = type.GetMethod (MethodAttributes.Public, m.Name); if (null == candidate || !candidate.IsPublic || candidate.IsStatic) return false; //ok interesting candidate! let's check if it matches the signature if (!AreSameElementTypes (m.ReturnType, candidate.ReturnType)) return false; if (!CompareParameters (m, candidate)) return false; } return true; }
public RuleResult CheckType (TypeDefinition type) { // rule applies only if the type isn't: an enum, an interface, a struct, a delegate or compiler generated if (type.IsEnum || type.IsInterface || type.IsValueType || type.IsDelegate () || type.IsGeneratedCode ()) return RuleResult.DoesNotApply; // it also does not apply if the type is static (2.0+ only) if (type.IsStatic ()) return RuleResult.DoesNotApply; if (!IsAllStatic (type)) return RuleResult.Success; // rule applies! foreach (MethodDefinition ctor in type.Methods) { if (ctor.IsConstructor && !ctor.IsStatic && ctor.IsVisible ()) { Runner.Report (ctor, Severity.Low, Confidence.High); } } return Runner.CurrentRuleResult; }
private static bool DoesTypeStealthilyImplementInterface (TypeDefinition type, TypeDefinition iface) { //ignore already uninteresting types below (self, enum, struct, static class) if (type == iface || type.IsEnum || type.IsValueType || type.IsStatic ()) return false; //if type has less methods than the interface no need to check further if (!type.HasMethods) return false; IList<MethodDefinition> mdc = iface.Methods; if (type.Methods.Count < mdc.Count) return false; //type already publicly says it implements the interface if (type.Implements (iface.Namespace, iface.Name)) return false; foreach (MethodDefinition m in mdc) { // FIXME: ignore methods with generic constraints if (HasConstraints (m)) return false; //if any candidate fails we can return right away //since the interface will never be fully implemented MethodDefinition candidate = type.GetMethod (MethodAttributes.Public, m.Name); if (null == candidate || !candidate.IsPublic || candidate.IsStatic) return false; //ok interesting candidate! let's check if it matches the signature if (!m.CompareSignature (candidate)) return false; // FIXME: ignore methods with generic constraints if (HasConstraints (candidate)) return false; } if (iface.HasInterfaces) { foreach (TypeReference tr in iface.Interfaces) { TypeDefinition td = tr.Resolve (); if (td == null) continue; if (!DoesTypeStealthilyImplementInterface (type, td)) return false; } } return true; }
public RuleResult CheckType (TypeDefinition type) { // rule applies only if the type isn't: an enum, an interface, a struct, a delegate or compiler generated if (type.IsEnum || type.IsInterface || type.IsValueType || !type.HasFields && GetMethodCount (type) == 0 || type.IsDelegate () || type.IsGeneratedCode () || type.BaseType != null && type.BaseType.FullName != "System.Object") return RuleResult.DoesNotApply; // success if the type is already static if (type.IsStatic ()) return RuleResult.Success; if (IsAllStatic (type)) { // no technical reason not to use a static type exists Runner.Report (type, Severity.Medium, Confidence.High); } return Runner.CurrentRuleResult; }
public RuleResult CheckType (TypeDefinition type) { // rule applies only if the type isn't: an enum, an interface, a struct, a delegate or compiler generated if (type.IsEnum || type.IsInterface || type.IsValueType || type.IsDelegate () || type.IsGeneratedCode () || type.BaseType != null && !type.BaseType.IsNamed ("System", "Object")) return RuleResult.DoesNotApply; // success if the type is already static or, before 2.0, is it's sealed if ((type.Module.Runtime >= TargetRuntime.Net_2_0) ? type.IsStatic () : type.IsSealed) return RuleResult.Success; if (IsAllStatic (type)) { // no technical reason not to use a static type exists Runner.Report (type, Severity.Medium, Confidence.High); } return Runner.CurrentRuleResult; }