مشاركة عبر


ca1024: استخدم خصائص مكان مناسب

TypeName

UsePropertiesWhereAppropriate

CheckId

ca1024

Category

Microsoft.تصميم

تعطيل تغيير

فصل

السبب

أسلوب عام أو محمية لها اسم يبدأ ب Getتأخذ بدون معلمات وإرجاع القيمة هو غير عنصر صفيفة.

وصف القاعدة

في معظم الحالات، تمثل خصائص بيانات، و وظائف تنفيذ الإجراءات. يتم الوصول إلى خصائص مثل حقول، مما يجعلها سهلة استخدم. أسلوب هو ترشيحه جيدة تصبح خاصية في حالة وجود أي من الشروط التالية:

  • يأخذ دون وسائط و إرجاع معلومات حالة الكائن.

  • يقبل وسيطة مفردة إلى تعيين جزء من الولاية الكائن.

خصائص يجب أن تتصرف كما لو كانوا حقول؛ وإذا تعذر الأسلوب، يجب تغييرها إلى خاصية. أفضل الطرق إلى الخصائص في الحالات التالية:

  • أسلوب يقوم بإجراء عملية يستغرق وقتاً طويلاً. الأسلوب هو perceivably أبطأ من الوقت المستغرق لتعيين أو يحصل القيمة حقل.

  • أسلوب إجراء تحويل. لم يعد الوصول إلى حقل محول نسخة بيانات التي تخزنها.

  • أسلوب يحصل تأثير جانبي observable. استرداد القيمة حقل لا ينتج أي تأثيرات جانبية.

  • ترتيب التنفيذ هو الهامة. أن يؤدي تعيين القيمة حقل لا يعتمد تشغيل العمليات غير ذلك تواجه حدث.

  • استدعاء أسلوب مرتين على التوالي ينشئ نتائج مختلفة.

  • الأسلوب هو ثابتة ولكن بإرجاع أحد الكائنات التي يمكن تغييرها بواسطة المستدعي. استرداد القيمة الحقل لا يسمح المتصل إلى تغيير sإلىred بيانات حسب الحقل.

  • أسلوب بإرجاع صفيفة.

كيف إلى الإصلاح انتهاكات

إلى إصلاح انتهاكا لهذه قاعدة، قم بتغيير الأسلوب إلى خاصية.

عند إلى منع التحذيرات

منع ظهور تحذير من هذه قاعدة إذا كان الأسلوب الذي يحقق على الأقل واحد المعايير المذكورة سابقا.

توسيع خصائص التحكم في مصحح الأخطاء

السبب الوحيد المبرمجين تجنب استخدام خاصية هو حيث أنها لا تريد المصحح تلقائي-توسيعه. ل مثال، قد تتضمن خاصية تخصيص كائن كبير أو استدعاء P/Invoke، ولكن قد لا فعلياً له أي تأثيرات-جانبية observable.

يمكنك منع المصحح تلقائياً توسيع خصائص بتطبيق System.Diagnostics.DebuggerBrowsableAttribute. يظهر المثال التالي هذه السمة التي يتم تطبيقها إلى خاصية مثيل.

Imports System 
Imports System.Diagnostics 

Namespace Microsoft.Samples 

    Public Class TestClass 

        ' [...] 

        <DebuggerBrowsable(DebuggerBrowsableState.Never)> _ 
        Public ReadOnly Property LargeObject() As LargeObject 
            Get 
                ' Allocate large object 
                ' [...] 
            End Get 
        End Property 

    End Class 

End Namespace
using System; 
using System.Diagnostics; 

namespace Microsoft.Samples 
{ 
    public class TestClass 
    { 
        // [...] 

        [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
        public LargeObject LargeObject 
        { 
            get 
            { 
                // Allocate large object 
                // [...] 

        }
    }
}

مثال

المثال التالي على العديد من الطرق التي يجب أن يتم تحويلها إلى الخصائص، و عدة يجب لأن لا تتصرف كحقول.

using System;
using System.Globalization;
using System.Collections;
namespace DesignLibrary
{
   // Illustrates the behavior of rule: 
   //  UsePropertiesWhereAppropriate.

   public class Appointment
   {
      static long nextAppointmentID;
      static double[] discountScale = {5.0, 10.0, 33.0};
      string customerName;
      long customerID;
      DateTime when;

      // Static constructor.
      static Appointment()
      {
         // Initializes the static variable for Next appointment ID.
      }

      // This method will violate the rule, but should not be a property.
      // This method has an observable side effect. 
      // Calling the method twice in succession creates different results.
      public static long GetNextAvailableID()
      {
         nextAppointmentID++;
         return nextAppointmentID - 1;
      }

      // This method will violate the rule, but should not be a property.
      // This method performs a time-consuming operation. 
      // This method returns an array.

      public Appointment[] GetCustomerHistory()
      {
         // Connect to a database to get the customer's appointment history.
         return LoadHistoryFromDB(customerID);
      }

      // This method will violate the rule, but should not be a property.
      // This method is static but returns a mutable object.
      public static double[] GetDiscountScaleForUpdate()
      {
         return discountScale;
      }

      // This method will violate the rule, but should not be a property.
      // This method performs a conversion.
      public string GetWeekDayString()
      {
         return DateTimeFormatInfo.CurrentInfo.GetDayName(when.DayOfWeek);
      }

      // These methods will violate the rule, and should be properties.
      // They each set or return a piece of the current object's state.

      public DayOfWeek GetWeekDay ()
      {
         return when.DayOfWeek;
      }

      public void  SetCustomerName (string customerName)
      {
         this.customerName = customerName;
      }
      public string GetCustomerName ()
      {
         return customerName;
      }

     public void SetCustomerID (long customerID)
      {
         this.customerID = customerID;
      }

      public long GetCustomerID ()
      {
         return customerID;
      }

      public void SetScheduleTime (DateTime when)
      {
         this.when = when;
      }

      public DateTime GetScheduleTime ()
      {
         return when;
      }

      // Time-consuming method that is called by GetCustomerHistory.
      Appointment[] LoadHistoryFromDB(long customerID)
      {
         ArrayList records = new ArrayList();
         // Load from database.
         return (Appointment[])records.ToArray();
      }
   }
}