MemberAttributes MemberAttributes MemberAttributes MemberAttributes Enum


定义类成员的成员特性标识符。Defines member attribute identifiers for class members.

public enum class MemberAttributes
public enum MemberAttributes
type MemberAttributes = 
Public Enum MemberAttributes


Abstract Abstract Abstract Abstract 1

抽象成员。An abstract member.

AccessMask AccessMask AccessMask AccessMask 61440

访问屏蔽。An access mask.

Assembly Assembly Assembly Assembly 4096

在同一程序集中任何类都可以访问的成员。A member that is accessible to any class within the same assembly.

Const Const Const Const 5

常数成员。A constant member.

Family Family Family Family 12288

可以在其类以及其派生类家族内访问的成员。A member that is accessible within the family of its class and derived classes.

FamilyAndAssembly FamilyAndAssembly FamilyAndAssembly FamilyAndAssembly 8192

可以在其类以及其在同一程序集中的派生类中访问的成员。A member that is accessible within its class, and derived classes in the same assembly.

FamilyOrAssembly FamilyOrAssembly FamilyOrAssembly FamilyOrAssembly 16384

可以在其类、其在任何程序集中的派生类以及在同一程序集的任何类中均访问的成员。A member that is accessible within its class, its derived classes in any assembly, and any class in the same assembly.

Final Final Final Final 2

在派生类中不能重写的成员。A member that cannot be overridden in a derived class.

New New New New 16

新成员。A new member.

Overloaded Overloaded Overloaded Overloaded 256

重载成员。An overloaded member. 某些语言(如 Visual Basic)要求显式指示重载成员。Some languages, such as Visual Basic, require overloaded members to be explicitly indicated.

Override Override Override Override 4

重写基类成员的成员。A member that overrides a base class member.

Private Private Private Private 20480

私有成员。A private member.

Public Public Public Public 24576

公共成员。A public member.

ScopeMask ScopeMask ScopeMask ScopeMask 15

范围屏蔽。A scope mask.

Static Static Static Static 3

静态成员。A static member. 在 Visual Basic 中,这等效于 Shared 关键字。In Visual Basic, this is equivalent to the Shared keyword.

VTableMask VTableMask VTableMask VTableMask 240

VTable 屏蔽。A VTable mask.


下面CodeMemberProperty的代码示例演示如何使用来string定义具有getset访问器的属性。The following example code demonstrates use of a CodeMemberProperty to define a string property with get and set accessors.

// Declares a property of type String named StringProperty.
CodeMemberProperty^ property1 = gcnew CodeMemberProperty;
property1->Name = "StringProperty";
property1->Type = gcnew CodeTypeReference( "System.String" );
property1->Attributes = MemberAttributes::Public;
property1->GetStatements->Add( gcnew CodeMethodReturnStatement( gcnew CodeFieldReferenceExpression( gcnew CodeThisReferenceExpression,"testStringField" ) ) );
property1->SetStatements->Add( gcnew CodeAssignStatement( gcnew CodeFieldReferenceExpression( gcnew CodeThisReferenceExpression,"testStringField" ),gcnew CodePropertySetValueReferenceExpression ) );

// A C# code generator produces the following source code for the preceeding example code:
//       public virtual string StringProperty
//       {
//              get
//            {
//                return this.testStringField;
//            }
//            set
//            {
//                this.testStringField = value;
//            }
//       }
// Declares a property of type String named StringProperty.
CodeMemberProperty property1 = new CodeMemberProperty();
property1.Name = "StringProperty";
property1.Type = new CodeTypeReference("System.String");
property1.Attributes = MemberAttributes.Public;
property1.GetStatements.Add( new CodeMethodReturnStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "testStringField") ) );
property1.SetStatements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "testStringField"), new CodePropertySetValueReferenceExpression()));

// A C# code generator produces the following source code for the preceeding example code:

//       public virtual string StringProperty 
//       {
//              get 
//            {
//                return this.testStringField;
//            }
//            set 
//            {
//                this.testStringField = value;
//            }
//       }            
' Declares a property of type String named StringProperty.
Dim property1 As New CodeMemberProperty()
property1.Name = "StringProperty"
property1.Type = New CodeTypeReference("System.String")
property1.Attributes = MemberAttributes.Public
property1.GetStatements.Add(New CodeMethodReturnStatement(New CodeFieldReferenceExpression(New CodeThisReferenceExpression(), "testStringField")))
property1.SetStatements.Add(New CodeAssignStatement(New CodeFieldReferenceExpression(New CodeThisReferenceExpression(), "testStringField"), New CodePropertySetValueReferenceExpression()))

' A Visual Basic code generator produces the following source code for the preceeding example code:

'     Public Overridable Property StringProperty() As String
'         Get
'             Return Me.testStringField
'         End Get
'         Set(ByVal Value As String)
'             Me.testStringField = value
'         End Set
'     End Property


MemberAttributes枚举中定义的标识符可用于指示类成员的作用域和访问属性。The identifiers defined in the MemberAttributes enumeration can be used to indicate the scope and access attributes of a class member.


Virtual没有成员属性。There is no Virtual member attribute. 成员被声明为虚拟的方式是: 将成员访问 Publicproperty1.Attributes = MemberAttributes.Public(), 而不将其指定为最终成员。A member is declared virtual by setting its member access to Public (property1.Attributes = MemberAttributes.Public) without specifying it as Final. 如果没有最终virtual标志, C# public virtual overridable则会在 Visual Basic (Public Overridable) 中将成员置于 () 中。The absence of the Final flag makes a member virtual in C# (public virtual), overridable in Visual Basic (Public Overridable). 若要避免将成员声明virtualoverridable或, 请在Attributes属性中同时设置公共和最终标志。To avoid declaring the member as virtual or overridable, set both the Public and Final flags in the Attributes property. 有关设置成员特性的详细信息, 请参见属性。AttributesSee the Attributes property for more information on setting member attributes.


用于设置访问标志 ( Public包含术语AssemblyPrivateFamily的标志) 的模式是使用 AccessMask 掩码掩盖所有访问标志, 然后设置所需的访问标志。The pattern for setting the access flags (flags containing the terms Public, Private, Assembly, or Family) is to mask out all access flags using the AccessMask mask and then set the desired access flag. 例如, 将构造函数 (名为constructor1) 标识为constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;public 的代码语句为。For example, the code statement to identify a constructor (named constructor1) as public is constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;. 将属性直接设置为访问标志 ( constructor1.Attributes = MemberAttributes.Public;例如) 会清除可能设置的所有其他标志。 AttributesSetting the Attributes property directly to an access flag (for example, constructor1.Attributes = MemberAttributes.Public;) erases all other flags that might be set. 此模式还应该用于使用 ScopeMask 掩码设置作用域标志 (Abstract、Final、Static、Override 或 Const)。This pattern should also be used for setting the scope flags (Abstract, Final, Static, Override or Const) using the ScopeMask mask.