CodeMemberField Class

Represents a declaration for a field of a type.

Namespace:  System.CodeDom
Assembly:  System (in System.dll)

[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[ComVisibleAttribute(true)]
public class CodeMemberField : CodeTypeMember

The CodeMemberField type exposes the following members.

  NameDescription
Public methodCodeMemberField()Initializes a new instance of the CodeMemberField class.
Public methodCodeMemberField(CodeTypeReference, String)Initializes a new instance of the CodeMemberField class using the specified field type and field name.
Public methodCodeMemberField(String, String)Initializes a new instance of the CodeMemberField class using the specified field type and field name.
Public methodCodeMemberField(Type, String)Initializes a new instance of the CodeMemberField class using the specified field type and field name.
Top

  NameDescription
Public propertyAttributesGets or sets the attributes of the member. (Inherited from CodeTypeMember.)
Public propertyCommentsGets the collection of comments for the type member. (Inherited from CodeTypeMember.)
Public propertyCustomAttributesGets or sets the custom attributes of the member. (Inherited from CodeTypeMember.)
Public propertyEndDirectivesGets the end directives for the member. (Inherited from CodeTypeMember.)
Public propertyInitExpressionGets or sets the initialization expression for the field.
Public propertyLinePragmaGets or sets the line on which the type member statement occurs. (Inherited from CodeTypeMember.)
Public propertyNameGets or sets the name of the member. (Inherited from CodeTypeMember.)
Public propertyStartDirectivesGets the start directives for the member. (Inherited from CodeTypeMember.)
Public propertyTypeGets or sets the type of the field.
Public propertyUserDataGets the user-definable data for the current object. (Inherited from CodeObject.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

CodeMemberField can be used to represent the declaration for a field of a type.

The following example demonstrates use of a CodeMemberField to declare a field of type string named testStringField.

// Declares a type to contain a field and a constructor method.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("FieldTest");

// Declares a field of type String named testStringField.
CodeMemberField field1 = new CodeMemberField("System.String", "TestStringField");
type1.Members.Add( field1 );

// Declares an empty type constructor.
CodeConstructor constructor1 = new CodeConstructor();
constructor1.Attributes = MemberAttributes.Public;            
type1.Members.Add( constructor1 );

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

//    public class FieldTest  
//    { 
//      private string testStringField; 
//         
//        public FieldTest()  
//        { 
//        }                             
//    }            
// This example demonstrates declaring a public constant type member field. 

// When declaring a public constant type member field, you must set a particular 
// access and scope mask to the member attributes of the field in order for the 
// code generator to properly generate the field as a constant field. 

// Declares an integer field using a CodeMemberField
CodeMemberField constPublicField = new CodeMemberField(typeof(int), "testConstPublicField");

// Resets the access and scope mask bit flags of the member attributes of the field 
// before setting the member attributes of the field to public and constant.
constPublicField.Attributes = (constPublicField.Attributes & ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask) | MemberAttributes.Public | MemberAttributes.Const;

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft