IsLong Classe
Definição
Indica que um inteiro modificado é um valor long
C++ padrão.Indicates that a modified integer is a standard C++ long
value. Essa classe não pode ser herdada.This class cannot be inherited.
public ref class IsLong abstract sealed
public static class IsLong
type IsLong = class
Public Class IsLong
- Herança
-
IsLong
Exemplos
O exemplo a seguir demonstra como emitir um IsLong objeto em um assembly usando reflexão.The following example demonstrates how to emit an IsLong object into an assembly using reflection.
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::CompilerServices;
using namespace System::Threading;
ref class CodeEmitter
{
private:
AssemblyBuilder^ asmBuilder;
String^ asmName;
ModuleBuilder^ modBuilder;
void prepareAssembly(String^ name){
// Check the input.
if(!name){
throw gcnew ArgumentNullException("AssemblyName");
}
asmName = name;
// Create an AssemblyName object and set the name.
AssemblyName^ asmName = gcnew AssemblyName();
asmName->Name = name;
// Use the AppDomain class to create an AssemblyBuilder instance.
AppDomain^ currentDomain = Thread::GetDomain();
asmBuilder = currentDomain->DefineDynamicAssembly(asmName,AssemblyBuilderAccess::RunAndSave);
// Create a dynamic module.
modBuilder = asmBuilder->DefineDynamicModule(name);
}
public:
// Constructor.
CodeEmitter(String ^ AssemblyName){
prepareAssembly(AssemblyName);
}
// Create a new type.
TypeBuilder^ CreateType(String^ name){
// Check the input.
if(!name){
throw gcnew ArgumentNullException("AssemblyName");
}
return modBuilder->DefineType( name );
}
// Write the assembly.
void WriteAssembly(MethodBuilder^ entryPoint){
// Check the input.
if(!entryPoint){
throw gcnew ArgumentNullException("entryPoint");
}
asmBuilder->SetEntryPoint( entryPoint );
asmBuilder->Save( asmName );
}
};
void main()
{
// Create a CodeEmitter to handle assembly creation.
CodeEmitter ^ e = gcnew CodeEmitter("program.exe");
// Create a new type.
TypeBuilder^ mainClass = e->CreateType("MainClass");
// Create a new method.
MethodBuilder^ mBuilder = mainClass->DefineMethod("mainMethod", MethodAttributes::Static);
// Create an ILGenerator and emit IL for
// a simple "Hello World." program.
ILGenerator^ ilGen = mBuilder->GetILGenerator();
ilGen->Emit(OpCodes::Ldstr, "Hello World");
array<Type^>^mType = {String::typeid};
MethodInfo^ writeMI = Console::typeid->GetMethod( "WriteLine", mType );
ilGen->EmitCall(OpCodes::Call, writeMI, nullptr );
ilGen->Emit( OpCodes::Ret );
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// Apply a required custom modifier
// to a field.
/////////////////////////////////////////////////
/////////////////////////////////////////////////
array<Type^>^fType = {IsLong::typeid};
mainClass->DefineField("modifiedInteger", Type::GetType("System.Int64"), fType, nullptr, FieldAttributes::Private);
// Create the type.
mainClass->CreateType();
// Write the assembly using a reference to
// the entry point.
e->WriteAssembly(mBuilder);
Console::WriteLine(L"Assembly created.");
}
Comentários
O padrão C++ indica que um long
valor e um valor inteiro são tipos distintos.The C++ standard indicates that a long
value and an integer value are distinct types. No entanto, elas são representadas usando ELEMENT_TYPE_I4
em um assembly.However, they are both represented using ELEMENT_TYPE_I4
in an assembly. Para distinguir um long
de um inteiro em C++, o compilador do Microsoft C++ adiciona o IsLong modificador a qualquer instância de um long
quando a instância é emitida.To distinguish a long
from an integer in C++, the Microsoft C++ compiler adds the IsLong modifier to any instance of a long
when the instance is emited. Esse processo é extremamente importante para manter a segurança de tipo no nível de linguagem.This process is critically important for maintaining language-level type safety.
Os compiladores emitem modificadores personalizados dentro de metadados para alterar a forma como o compilador JIT (just-in-time) manipula valores quando o comportamento padrão não é apropriado.Compilers emit custom modifiers within metadata to change the way that the just-in-time (JIT) compiler handles values when the default behavior is not appropriate. Quando o compilador JIT encontra um modificador personalizado, ele manipula o valor da maneira que o modificador especifica.When the JIT compiler encounters a custom modifier, it handles the value in the way that the modifier specifies. Os compiladores podem aplicar modificadores personalizados a métodos, parâmetros e valores de retorno.Compilers can apply custom modifiers to methods, parameters, and return values. O compilador JIT deve responder aos modificadores necessários, mas pode ignorar modificadores opcionais.The JIT compiler must respond to required modifiers but can ignore optional modifiers.
Você pode emitir modificadores personalizados em metadados usando uma das seguintes técnicas:You can emit custom modifiers into metadata using one of the following techniques:
Usando métodos na TypeBuilder classe, como, DefineMethod , DefineField DefineConstructor e DefineProperty .Using methods in the TypeBuilder class such as DefineMethod, DefineField, DefineConstructor, and DefineProperty.
Gerando um arquivo de instruções MSIL (Microsoft Intermediate Language) que contém chamadas para
modopt
emodreq
, e montando o arquivo com o Ilasm.exe (Il assembler).Generating a Microsoft intermediate language (MSIL) instruction file that contains calls tomodopt
andmodreq
, and assembling the file with the Ilasm.exe (IL Assembler).Usando a API de reflexão não gerenciada.Using the unmanaged reflection API.