IsLong 类

定义

指示修饰的整数是标准 C++ long 值。Indicates that a modified integer is a standard C++ long value. 此类不能被继承。This class cannot be inherited.

public ref class IsLong abstract sealed
public static class IsLong
type IsLong = class
Public Class IsLong
继承
IsLong

示例

下面的示例演示如何 IsLong 使用反射将对象发送到程序集。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.");
}

注解

C + + 标准指示 long 值和整数值是不同的类型。The C++ standard indicates that a long value and an integer value are distinct types. 但是,它们 ELEMENT_TYPE_I4 在程序集中均使用表示。However, they are both represented using ELEMENT_TYPE_I4 in an assembly. 若要 long 从 c + + 中的整数区分,Microsoft c + + 编译器将 IsLong 修饰符添加到 long 实例为 emited 的任何实例。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. 此过程对于维护语言级类型安全至关重要。This process is critically important for maintaining language-level type safety.

编译器在元数据中发出自定义修饰符,以更改当默认行为不合适时实时 (JIT) 编译器处理值的方式。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. 当 JIT 编译器遇到自定义修饰符时,它将按修饰符指定的方式处理值。When the JIT compiler encounters a custom modifier, it handles the value in the way that the modifier specifies. 编译器可以对方法、参数和返回值应用自定义修饰符。Compilers can apply custom modifiers to methods, parameters, and return values. JIT 编译器必须响应所需的修饰符,但可以忽略可选修饰符。The JIT compiler must respond to required modifiers but can ignore optional modifiers.

可以使用以下方法之一将自定义修饰符发送到元数据中:You can emit custom modifiers into metadata using one of the following techniques:

适用于