本文整理汇总了C#中Mono.Cecil.MethodDefinition.IsGeneratedCode方法的典型用法代码示例。如果您正苦于以下问题:C# MethodDefinition.IsGeneratedCode方法的具体用法?C# MethodDefinition.IsGeneratedCode怎么用?C# MethodDefinition.IsGeneratedCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.MethodDefinition
的用法示例。
在下文中一共展示了MethodDefinition.IsGeneratedCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// ingore methods without body and generated code
if (!method.HasBody || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
int num = method.Body.MaxStackSize;
if (num <= MaximumStackSize)
return RuleResult.Success;
string msg = String.Format ("Found {0} maximum stack size (maximum {1}).", num, MaximumStackSize);
Runner.Report (method, Severity.High, Confidence.High, msg);
return RuleResult.Failure;
}
示例2: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule does not apply to properties, events, without parameters or for generated code
if (method.IsSpecialName || !method.HasParameters || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
foreach (ParameterDefinition parameter in method.Parameters) {
if (parameter.ParameterType.FullName != "System.Object&")
continue;
// suggest using generics
Runner.Report (parameter, Severity.Medium, Confidence.High);
}
return Runner.CurrentRuleResult;
}
示例3: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule only applies if the method has a body
// rule doesn't not apply to generated code (out of developer's control)
if (!method.HasBody || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// check if the method contains a Pop instruction
if (!OpCodeEngine.GetBitmask (method).Get (Code.Pop))
return RuleResult.DoesNotApply;
foreach (Instruction instruction in method.Body.Instructions) {
if (instruction.OpCode.Code == Code.Pop) {
CheckForViolation (method, instruction.Previous);
}
}
return Runner.CurrentRuleResult;
}
示例4: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule does not apply to properties, events, without parameters or for generated code
if (method.IsSpecialName || !method.HasParameters || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// exclude the "bool Try* (ref)" pattern from the rule
if (method.Name.StartsWith ("Try", StringComparison.Ordinal) && method.ReturnType.IsNamed ("System", "Boolean"))
return RuleResult.DoesNotApply;
foreach (ParameterDefinition parameter in method.Parameters) {
if (!parameter.ParameterType.IsNamed ("System", "Object&"))
continue;
// suggest using generics
Runner.Report (parameter, Severity.Medium, Confidence.High);
}
return Runner.CurrentRuleResult;
}
示例5: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// ingore methods without body and generated code
if (!method.HasBody || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// special case for System.Windows.Forms since it's designer does not
// mark this code as generated :-(
if (method.Name == "InitializeComponent") {
if (method.DeclaringType.Inherits ("System.Windows.Forms.Form"))
return RuleResult.DoesNotApply;
}
int num = method.Body.Variables.Count;
if (num <= MaximumVariables)
return RuleResult.Success;
string msg = String.Format ("Found {0} local variables (maximum {1}).", num, MaximumVariables);
Runner.Report (method, Severity.High, Confidence.High, msg);
return RuleResult.Failure;
}
示例6: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule does not apply if there's no IL code
// or if it was generated by the compiler or tools
if (!method.HasBody || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// is there a Newobj *and* a Throw instruction in this method
if (!bitmask.IsSubsetOf (OpCodeEngine.GetBitmask (method)))
return RuleResult.DoesNotApply;
int n = 0;
bool cond_branch = false;
foreach (Instruction inst in method.Body.Instructions) {
// check if the code is linear or with branches
if (FlowControl.Cond_Branch == inst.OpCode.FlowControl)
cond_branch = true;
// if there are branch and it's long enough then assume it is implemented
if (cond_branch && (++n > 10))
break;
// check for "throw new NotImplementedException (...)"
if (inst.OpCode.Code != Code.Newobj)
continue;
MethodReference ctor = (MethodReference) inst.Operand;
if ("System.NotImplementedException" != ctor.DeclaringType.FullName)
continue;
if (inst.Next.OpCode.Code != Code.Throw)
continue;
// the defect is more severe if the method is visible outside it's assembly
Severity severity = method.IsPublic ? Severity.High : Severity.Medium;
Runner.Report (method, severity, Confidence.Normal);
return RuleResult.Failure;
}
return RuleResult.Success;
}
示例7: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// ignore constructors (.ctor or .cctor) and compiler/tool-generated code
if (method.IsConstructor || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// don't consider private add / remove on events
if ((method.IsAddOn || method.IsRemoveOn) && method.IsPrivate)
return RuleResult.DoesNotApply;
string name = method.Name;
MethodSemanticsAttributes attrs = method.SemanticsAttributes;
if ((attrs & mask) != 0) {
// it's something special
int underscore = name.IndexOf ('_');
if (underscore != -1)
name = name.Substring (underscore + 1);
} else if (method.IsSpecialName) {
return RuleResult.Success;
}
// like types, methods/props should all be PascalCased, too
if (!IsPascalCase (name)) {
string errorMessage = String.Format ("By existing naming conventions, all the method and property names should all be pascal-cased (e.g. MyOperation). Rename '{0}' to '{1}'.",
name, PascalCase (name));
Runner.Report (method, Severity.Medium, Confidence.High, errorMessage);
}
// check parameters
if (method.HasParameters) {
foreach (ParameterDefinition param in method.Parameters) {
// params should all be camelCased
if (!IsCamelCase (param.Name)) {
string errorMessage = String.Format ("By existing naming conventions, the parameter names should all be camel-cased (e.g. myParameter). Rename '{0}' parameter to '{1}'.",
param, CamelCase (param.Name));
Runner.Report (method, Severity.Medium, Confidence.High, errorMessage);
}
}
}
return Runner.CurrentRuleResult;
}
示例8: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule does not apply to external methods (e.g. p/invokes)
// and generated code (by compilers or tools)
if (!method.HasBody || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
var variables = method.Body.Variables;
int count = variables.Count;
if (count == 0)
return RuleResult.Success;
if (used == null) {
used = new BitArray (Math.Max (DefaultLength, count));
} else if (count > used.Length) {
used = new BitArray (count);
}
used.SetAll (false);
foreach (Instruction ins in method.Body.Instructions) {
VariableDefinition vd = ins.GetVariable (method);
if (vd != null)
used [vd.Index] = true;
}
for (int i = 0; i < count; i++) {
if (!used [i]) {
// sometimes the compilers generates some locals without really
// using them (e.g. assign only a constant). In this case we need
// to determine if the variable is "genuine" or a compiler
// (*) seen in a while (true) loop over a switch
VariableDefinition variable = variables [i];
string var_name = variable.Name;
if (var_name.StartsWith ("V_") || var_name.Contains ("$"))
continue;
string s = String.Format ("Variable '{0}' of type '{1}'",
var_name, variable.VariableType.FullName);
Runner.Report (method, Severity.Low, Confidence.Normal, s);
}
}
return Runner.CurrentRuleResult;
}
示例9: IsGeneratedDispose
// looking for 'protected override void Dispose (bool disposing)' with generated code
bool IsGeneratedDispose(MethodDefinition method)
{
if (!method.IsFamily || !method.IsVirtual || method.IsNewSlot || !method.HasParameters || !method.HasBody)
return false;
return ((method.Name == "Dispose") && method.IsGeneratedCode ());
}
示例10: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// exclude constrcutors, non-visible methods and generated code
if (method.IsConstructor || !method.IsVisible () || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// the rule does not apply if the code is an interface to COM objects
if (UsedForComInterop (method.DeclaringType as TypeDefinition))
return RuleResult.DoesNotApply;
// check the method name
if (!CheckName (method.Name, method.IsSpecialName))
Runner.Report (method, Severity.Medium, Confidence.High);
if (method.HasParameters) {
foreach (ParameterDefinition parameter in method.Parameters) {
if (!CheckName (parameter.Name, false))
Runner.Report (parameter, Severity.Medium, Confidence.High);
}
}
return Runner.CurrentRuleResult;
}
示例11: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule does not apply if
// - the method has no body (e.g. p/invokes, icalls don't)
// - the method is static
// - the method was generated by the compiler or a tool
if (!method.HasBody || method.IsStatic || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// avoid looping if we're sure there's no call in the method
if (!OpCodeEngine.GetBitmask (method).Get (Code.Stsfld))
return RuleResult.DoesNotApply;
// *** ok, the rule applies! ***
foreach (Instruction ins in method.Body.Instructions) {
// look for stsfld instructions
if (ins.OpCode.Code == Code.Stsfld) {
FieldReference fr = (ins.Operand as FieldReference);
if (CheckField (fr)) {
string text = String.Format ("The static field '{0}', of type '{1}'. is being set in an instance method.", fr.Name, fr.FieldType);
Runner.Report (method, ins, Severity.Medium, Confidence.High, text);
}
}
}
return Runner.CurrentRuleResult;
}
示例12: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
if (!method.HasBody)
return RuleResult.DoesNotApply;
// Don't want to count the code generated by yield statements.
if (method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
if (Preflight (method)) {
Log.WriteLine (this);
Log.WriteLine (this, "-----------------------------------------");
Log.WriteLine (this, method);
call_using_this = false;
field_access_using_this = false;
creates_exception = false;
has_dispose_check = false;
CheckBody (method);
if ((call_using_this || field_access_using_this) && !creates_exception) {
if (!has_dispose_check) {
Runner.Report (method, Severity.Medium, Confidence.High);
}
}
}
return Runner.CurrentRuleResult;
}
示例13: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// catch abstract, pinvoke and icalls - where rule does not apply
if (!method.HasBody)
return RuleResult.DoesNotApply;
// skip methods without parameters
if (!method.HasParameters)
return RuleResult.DoesNotApply;
// rule doesn't apply to virtual, overrides or generated code
// doesn't apply to code referenced by delegates (note: more complex check moved last)
if (method.IsVirtual || method.HasOverrides || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// Also EventArgs parameters are often required in method signatures,
// but often not required. Reduce "false positives"(*) for GUI apps
// (*) it's more a "don't report things outside developer's control"
if (method.IsEventCallback () || IsReferencedByDelegate (method))
return RuleResult.DoesNotApply;
// methods with [Conditional] can be empty (not using any parameter) IL-wise but not source-wise, ignore them
if (method.HasCustomAttributes) {
if (method.CustomAttributes.ContainsType ("System.Diagnostics.ConditionalAttribute"))
return RuleResult.DoesNotApply;
}
// rule applies
// we limit ourselves to the first 64 parameters (so we can use a bitmask)
IList<ParameterDefinition> pdc = method.Parameters;
int pcount = pdc.Count;
if (pcount > 64)
pcount = 64;
ulong mask = 0;
// scan IL to see which parameter is being used
foreach (Instruction ins in method.Body.Instructions) {
ParameterDefinition parameter = ins.GetParameter (method);
if (parameter == null)
continue;
mask |= ((ulong)1 << (parameter.GetSequence () - 1));
}
// quick out based on value - i.e. every parameter is being used
int shift = 64 - pcount;
if ((mask << shift) == (UInt64.MaxValue << shift))
return RuleResult.Success;
for (int i = 0; i < pcount; i++) {
if ((mask & ((ulong) 1 << i)) == 0) {
ParameterDefinition parameter = pdc [i];
string text = String.Format ("Parameter '{0}' of type '{1}' is never used in the method.",
parameter.Name, parameter.ParameterType);
Runner.Report (parameter, Severity.Medium, Confidence.Normal, text);
}
}
return Runner.CurrentRuleResult;
}
示例14: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// rule applies only if the method has generic type parameters
if (!method.HasGenericParameters || method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// look if every generic type parameter...
foreach (GenericParameter gp in method.GenericParameters) {
Severity severity = Severity.Medium;
bool found = false;
string nspace = gp.Namespace;
string name = gp.Name;
// ... is being used by the method parameters
foreach (ParameterDefinition pd in method.Parameters) {
if (IsGenericType (pd.ParameterType, nspace, name)) {
found = true;
break;
}
}
if (!found) {
// it's a defect when used only for the return value - but we reduce its severity
if (IsGenericType (method.ReturnType, nspace, name))
severity = Severity.Low;
}
if (!found) {
string msg = String.Format (CultureInfo.InvariantCulture,
"Generic parameter '{0}' is not used by the method parameters.", name);
Runner.Report (method, severity, Confidence.High, msg);
}
}
return Runner.CurrentRuleResult;
}
示例15: CheckMethod
public RuleResult CheckMethod (MethodDefinition method)
{
// we only check non static, non virtual methods and not constructors
if (method.IsStatic || method.IsVirtual || method.IsConstructor)
return RuleResult.DoesNotApply;
// we only check methods with a body
if (!method.HasBody)
return RuleResult.DoesNotApply;
// we also don't check event callbacks, as they usually bring a lot of false positive,
// and that developers are tied to their signature
if (method.IsEventCallback ())
return RuleResult.DoesNotApply;
// that aren't compiler generated (e.g. anonymous methods) or generated by a tool (e.g. web services)
if (method.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// methods with [Conditional] can be empty (not using 'this') IL-wise but not source-wise, ignore them
if (method.HasCustomAttributes) {
if (method.HasAttribute ("System.Diagnostics", "ConditionalAttribute"))
return RuleResult.DoesNotApply;
}
// rule applies
// if we find a use of the "this" reference, it's ok
// in most (all?) case "this" is used with the ldarg_0 instruction
if (OpCodeEngine.GetBitmask (method).Get (Code.Ldarg_0))
return RuleResult.Success;
// but it's also valid to have an ldarg with a 0 value operand
foreach (Instruction instr in method.Body.Instructions) {
if (instr.OpCode.Code == Code.Ldarg) {
ParameterDefinition pd = (instr.Operand as ParameterDefinition);
if (pd.Index == -1)
return RuleResult.Success;
}
}
Runner.Report (method, Severity.Low, Confidence.Total);
return RuleResult.Failure;
}