다음을 통해 공유


CA1044: 속성은 쓰기 전용이면 안 됩니다.

TypeName

PropertiesShouldNotBeWriteOnly

CheckId

CA1044

범주

Microsoft.Design

변경 수준

주요 변경

원인

public 또는 protected 속성에 set 접근자는 있지만 get 접근자가 없습니다.

규칙 설명

get 접근자는 속성에 대한 읽기 권한을 제공하고 set 접근자는 쓰기 권한을 제공합니다. 읽기 전용 속성을 사용하는 것은 가능하고 종종 필요하기도 하지만 쓰기 전용 속성의 사용은 금지되어 있습니다. 이는 사용자가 값을 설정하도록 한 다음 값을 볼 수 있게 하면 값의 보안을 제공할 수 없기 때문입니다. 또한 읽기 권한이 없으면 공유 개체의 상태를 볼 수 없으므로 사용하는 데 제한을 받습니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 속성에 get 접근자를 추가합니다. 또는, 쓰기 전용 속성의 동작이 필요한 경우 이 속성을 메서드로 변환할 것을 고려해 보십시오.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시하는 것이 좋습니다.

예제

다음 예제에서는 BadClassWithWriteOnlyProperty이 쓰기 전용 속성을 가진 형식입니다. GoodClassWithReadWriteProperty에는 올바른 코드가 들어 있습니다.

Imports System

Namespace DesignLibrary

   Public Class BadClassWithWriteOnlyProperty

      Dim someName As String

      ' Violates rule PropertiesShouldNotBeWriteOnly.
      WriteOnly Property Name As String
         Set 
            someName = Value
         End Set 
      End Property

   End Class

   Public Class GoodClassWithReadWriteProperty

      Dim someName As String

      Property Name As String
         Get 
            Return someName
         End Get 

         Set 
            someName = Value
         End Set 
      End Property

   End Class

End Namespace
using System;

namespace DesignLibrary
{
   public class BadClassWithWriteOnlyProperty
   {
      string someName;

      // Violates rule PropertiesShouldNotBeWriteOnly.
      public string Name 
      { 
         set 
         { 
            someName = value; 
         } 
      }
   }

   public class GoodClassWithReadWriteProperty
   {
      string someName;

      public string Name 
      { 
         get 
         { 
            return someName; 
         } 
         set 
         { 
            someName = value; 
         } 
      }
   }
}