다음을 통해 공유


FieldBuilder.Name 속성

해당 필드의 이름을 나타냅니다. 이 속성은 읽기 전용입니다.

네임스페이스: System.Reflection.Emit
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overrides ReadOnly Property Name As String
‘사용 방법
Dim instance As FieldBuilder
Dim value As String

value = instance.Name
public override string Name { get; }
public:
virtual property String^ Name {
    String^ get () override;
}
/** @property */
public String get_Name ()
public override function get Name () : String

속성 값

해당 필드의 이름이 들어 있는 String입니다.

설명

다음 코드 예제에서는 Name를 사용합니다.

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Class FieldBuilder_Sample
   Private Shared Function CreateType(currentDomain As AppDomain) As Type

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
                                                AssemblyBuilderAccess.Run)
      ' Create a dynamic module in Dynamic Assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule")
      ' Define a public class named "MyClass" in the assembly.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _
                                          TypeAttributes.Public)
      ' Define a private String field named "MyField" in the type.
      Dim myFieldBuilder As FieldBuilder = myTypeBuilder.DefineField("MyField", _
                  GetType(String), FieldAttributes.Private Or FieldAttributes.Static)
      ' Create the constructor.
      Dim constructorArgs As Type() = {GetType(String)}
      Dim constructor As ConstructorBuilder = _
                  myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
                           CallingConventions.Standard, constructorArgs)
      Dim constructorIL As ILGenerator = constructor.GetILGenerator()
      constructorIL.Emit(OpCodes.Ldarg_0)
      Dim superConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
      constructorIL.Emit(OpCodes.Call, superConstructor)
      constructorIL.Emit(OpCodes.Ldarg_0)
      constructorIL.Emit(OpCodes.Ldarg_1)
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder)
      constructorIL.Emit(OpCodes.Ret)

      ' Create the MyMethod method.
      Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod",  _
                        MethodAttributes.Public, GetType(String), Nothing)
      Dim methodIL As ILGenerator = myMethodBuilder.GetILGenerator()
      methodIL.Emit(OpCodes.Ldarg_0)
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder)
      methodIL.Emit(OpCodes.Ret)
      Console.WriteLine("Name               :" + myFieldBuilder.Name)
      Console.WriteLine("DeclaringType      :" + myFieldBuilder.DeclaringType.ToString())
      Console.WriteLine("Type               :" + myFieldBuilder.FieldType.ToString())
      Console.WriteLine("Token              :" + myFieldBuilder.GetToken().Token.ToString())
      Return myTypeBuilder.CreateType()
   End Function 'CreateType

   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Try
         Dim myType As Type = CreateType(Thread.GetDomain())
         ' Create an instance of the "HelloWorld" class.
         Dim helloWorld As Object = Activator.CreateInstance(myType, New Object() {"HelloWorld"})
         ' Invoke the "MyMethod" method of the "MyClass" class.
         Dim myObject As Object = myType.InvokeMember("MyMethod", _
                     BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
         Console.WriteLine("MyClass.MyMethod returned: " & Microsoft.VisualBasic.Chr(34) & myObject  & Microsoft.VisualBasic.Chr(34) )
     Catch e as Exception
            Console.WriteLine("Exception Caught "+e.Message)
     End Try
   End Sub 'Main
End Class 'FieldBuilder_Sample
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public class FieldBuilder_Sample
{
   private static Type CreateType(AppDomain currentDomain)
   {

      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
                     currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder=myAssembly.DefineDynamicModule("MyModule");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);

      // Define a private String field named "MyField" in the type.
      FieldBuilder myFieldBuilder= myTypeBuilder.DefineField("MyField",
          typeof(string),FieldAttributes.Private|FieldAttributes.Static);
      // Create the constructor.
      Type[] constructorArgs = { typeof(String) };
      ConstructorBuilder constructor = myTypeBuilder.DefineConstructor(
         MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder);
      constructorIL.Emit(OpCodes.Ret);

      // Create the MyMethod method.
      MethodBuilder myMethodBuilder= myTypeBuilder.DefineMethod("MyMethod",
                           MethodAttributes.Public,typeof(String),null);
      ILGenerator methodIL = myMethodBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);
      methodIL.Emit(OpCodes.Ret);
      Console.WriteLine("Name               :"+myFieldBuilder.Name);
      Console.WriteLine("DeclaringType      :"+myFieldBuilder.DeclaringType);
      Console.WriteLine("Type               :"+myFieldBuilder.FieldType);
      Console.WriteLine("Token              :"+myFieldBuilder.GetToken().Token);
      return myTypeBuilder.CreateType();
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      try
      {
         Type myType = CreateType(Thread.GetDomain());
         // Create an instance of the "HelloWorld" class.
         Object helloWorld = Activator.CreateInstance(myType, new Object[] { "HelloWorld" });
         // Invoke the "MyMethod" method of the "MyClass" class.
         Object myObject  = myType.InvokeMember("MyMethod",
                        BindingFlags.InvokeMethod, null, helloWorld, null);
         Console.WriteLine("MyClass.MyMethod returned: \"" + myObject + "\"");
      }
      catch( Exception e)
      {
         Console.WriteLine("Exception Caught "+e.Message);
      }
  }
}
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ CreateType( AppDomain^ currentDomain )
{
   // Create an assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "DynamicAssembly";
   AssemblyBuilder^ myAssembly = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );

   // Create a dynamic module in Dynamic Assembly.
   ModuleBuilder^ myModuleBuilder = myAssembly->DefineDynamicModule( "MyModule" );

   // Define a public class named S"MyClass" in the assembly.
   TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "MyClass", TypeAttributes::Public );

   // Define a private String field named S"MyField" in the type.
   FieldBuilder^ myFieldBuilder = myTypeBuilder->DefineField( "MyField", String::typeid, static_cast<FieldAttributes>(FieldAttributes::Private | FieldAttributes::Static) );

   // Create the constructor.
   array<Type^>^constructorArgs = {String::typeid};
   ConstructorBuilder^ constructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, constructorArgs );
   ILGenerator^ constructorIL = constructor->GetILGenerator();
   constructorIL->Emit( OpCodes::Ldarg_0 );
   ConstructorInfo^ superConstructor = Object::typeid->GetConstructor( gcnew array<Type^>(0) );
   constructorIL->Emit( OpCodes::Call, superConstructor );
   constructorIL->Emit( OpCodes::Ldarg_0 );
   constructorIL->Emit( OpCodes::Ldarg_1 );
   constructorIL->Emit( OpCodes::Stfld, myFieldBuilder );
   constructorIL->Emit( OpCodes::Ret );

   // Create the MyMethod method.
   MethodBuilder^ myMethodBuilder = myTypeBuilder->DefineMethod( "MyMethod", MethodAttributes::Public, String::typeid, nullptr );
   ILGenerator^ methodIL = myMethodBuilder->GetILGenerator();
   methodIL->Emit( OpCodes::Ldarg_0 );
   methodIL->Emit( OpCodes::Ldfld, myFieldBuilder );
   methodIL->Emit( OpCodes::Ret );
   Console::WriteLine( "Name               : {0}", myFieldBuilder->Name );
   Console::WriteLine( "DeclaringType      : {0}", myFieldBuilder->DeclaringType );
   Console::WriteLine( "Type               : {0}", myFieldBuilder->FieldType );
   Console::WriteLine( "Token              : {0}", myFieldBuilder->GetToken().Token );
   return myTypeBuilder->CreateType();
}

int main()
{
   try
   {
      Type^ myType = CreateType( Thread::GetDomain() );

      // Create an instance of the S"HelloWorld" class.
      array<Object^>^type = {"HelloWorld"};
      Object^ helloWorld = Activator::CreateInstance( myType, type );

      // Invoke the S"MyMethod" method of the S"MyClass" class.
      Object^ myObject = myType->InvokeMember( "MyMethod", BindingFlags::InvokeMethod, nullptr, helloWorld, nullptr );
      Console::WriteLine( "MyClass::MyMethod returned: \"{0}\"", myObject );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Caught {0}", e->Message );
   }
}

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

FieldBuilder 클래스
FieldBuilder 멤버
System.Reflection.Emit 네임스페이스