FieldBuilder.Attributes Propriedade
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Indica os atributos desse campo. Esta propriedade é somente para leitura.
public:
virtual property System::Reflection::FieldAttributes Attributes { System::Reflection::FieldAttributes get(); };
public override System.Reflection.FieldAttributes Attributes { get; }
member this.Attributes : System.Reflection.FieldAttributes
Public Overrides ReadOnly Property Attributes As FieldAttributes
Os atributos desse campo.
O exemplo de código a seguir ilustra o uso de Attributes
.
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^ myConstructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, constructorArgs );
ILGenerator^ constructorIL = myConstructor->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 );
if ( myFieldBuilder->Attributes.Equals( FieldAttributes::Static ) )
{
Console::WriteLine( "Field attribute defined as Static" );
}
else
if ( myFieldBuilder->Attributes.Equals( FieldAttributes::Static | FieldAttributes::Private ) )
{
Console::WriteLine( "Field attributes are Static and Private" );
}
Console::WriteLine( "ReflectedType of Field is : {0}", myFieldBuilder->ReflectedType );
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" of the S"MyClass".
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 );
}
}
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 myConstructor = myTypeBuilder.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
ILGenerator constructorIL = myConstructor.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);
if (myFieldBuilder.Attributes.Equals(FieldAttributes.Static))
{
Console.WriteLine("Field attribute defined as Static");
}
else if(myFieldBuilder.Attributes.Equals(FieldAttributes.Static|FieldAttributes.Private))
{
Console.WriteLine("Field attributes are Static and Private");
}
Console.WriteLine("ReflectedType of Field is: " + myFieldBuilder.ReflectedType);
return myTypeBuilder.CreateType();
}
public static void Main()
{
Type myType = CreateType(Thread.GetDomain());
// Create an instance of the "HelloWorld" class.
Object helloWorld = Activator.CreateInstance(myType, new Object[] { "HelloWorld" });
// Invoke the "MyMethod" of the "MyClass".
Object myObject = myType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod, null, helloWorld, null);
Console.WriteLine("MyClass.MyMethod returned: \"" + myObject + "\"");
}
}
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 myConstructor As ConstructorBuilder = _
myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
CallingConventions.Standard, constructorArgs)
Dim constructorIL As ILGenerator = myConstructor.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)
If myFieldBuilder.Attributes.Equals(FieldAttributes.Static) Then
Console.WriteLine("Field attribute defined as Static")
Else
If myFieldBuilder.Attributes.Equals(FieldAttributes.Static Or FieldAttributes.Private) Then
Console.WriteLine("Field attributes are Static and Private")
End If
End If
Console.WriteLine("ReflectedType of Field is: " & myFieldBuilder.ReflectedType.ToString())
Return myTypeBuilder.CreateType()
End Function 'CreateType
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Shared Sub Main()
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" of the "MyClass".
Dim myObject As Object = myType.InvokeMember("MyMethod", _
BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
Console.WriteLine("MyClass.MyMethod returned: """ & myObject & """")
End Sub
End Class
Produto | Versões |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided), 2.1 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: