zoukankan      html  css  js  c++  java
  • FxCop静态代码分析

    SQL Inject: SQL注入

    FxCop :静态代码分析,用软件来检测你的代码

    代码检测扩展功能(自己可以写方法,来检测自己的代码有那些漏洞)


    #region Using directives

    using System;
    using System.Globalization;

    using Microsoft.Cci;
    using Microsoft.FxCop.Sdk;
    using Microsoft.FxCop.Sdk.Introspection;

    #endregion

    namespace FxCop.Rules
    {
        #region //comment
        /// <summary>
     ///  <para>
     ///   Checks for complicated methods and constructors.
     ///  </para>
     /// </summary>
     /// <remarks>
     ///  <para>
     ///   A method or constructor is classes as complicated if it contains more than 75 method calls.
     ///  </para>
        /// </remarks>
        #endregion

        public class AvoidComplicatedMethods : BaseRule
     {
      private const int MAXIMUM_METHOD_CALLS = 75;

            #region //comment
            /// <summary>
      ///  <para>
      ///   Initializes a new instance of the <see cref="AvoidComplicatedMethods"/> class.
      ///  </para>
            /// </summary>
            #endregion

            public AvoidComplicatedMethods() : base("AvoidComplicatedMethods")
      {
            }

            #region //comment
            /// <summary>
      ///  <para>
      ///   This member overrides <see cref="BaseIntrospectionRule.Check(Member)"/>.
      ///  </para>
      /// </summary>
      /// <param name="m">
      ///  The <see cref="Member"/> to check.
      /// </param>
      /// <returns>
      ///  A <see cref="ProblemCollection"/> containing the problems associated with <paramref name="m"/>.
            /// </returns>
            #endregion

            public override ProblemCollection Check(Member m)
      {
       Method method = m as Method;

       if (method == null)
        return null;

       if (method.Instructions == null)
        return null;

       string name = method.Name.Name;

       if (name == "InitializeComponent")
        return null;

       int methodCallCount = 0;
       
       for (int i = 0; i < method.Instructions.Length; i++)
       {
        if (RuleHelper.IsMethodCall(method.Instructions[i]))
        {
         methodCallCount++;
        }
       }

       if (methodCallCount > MAXIMUM_METHOD_CALLS)
       {
        AddProblem(method, methodCallCount);    
       }

       return Problems;
      }

      private void AddProblem(Method m, int callCount)
      {
       Problems.Add(new Problem(GetResolution(RuleUtilities.Format(m), callCount.ToString(CultureInfo.CurrentCulture), MAXIMUM_METHOD_CALLS.ToString(CultureInfo.CurrentCulture))));
      }
     }
    }

     MSDN中文版的FxCop静态代码分析


    <?xml version="1.0" encoding="utf-8" ?>
    <Rules FriendlyName="Custom Rules">
     <Rule TypeName="AvoidComplicatedMethods" Category="Custom" CheckId="CUS1000">
      <Name>Avoid complicated methods</Name>
      <Description>Methods that have many methods calls or property accessors are hard to maintain and therefore should be kept to a minimum.</Description>
      <Owner>Not Available</Owner>
      <Url>http://www.gotdotnet.com/team/fxcop/</Url>
      <Resolution>'{0}' has {1} method calls. Refactor '{0}' so that it calls fewer than {2} methods.</Resolution>
      <Email>Not Available</Email>
      <MessageLevel Certainty="95">Warning</MessageLevel>
      <FixCategories>NonBreaking</FixCategories>
     </Rule> 
    </Rules>

  • 相关阅读:
    Java在处理大数据的时候一些小技巧
    大并发处理解决方案
    数据库SQL优化大总结之 百万级数据库优化方案
    DotNet中的计时器线程计时器
    System.Threading.Timer的使用技巧
    Asp.net Mvc 请求是如何到达 MvcHandler的——UrlRoutingModule、MvcRouteHandler分析,并造个轮子
    C#-结构
    @Html.ActionLink(),@Html.Raw(),@Url.Action()等
    bootstarpt-table小结
    input[ type="file"]上传文件问题
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136365.html
Copyright © 2011-2022 走看看