IsLong 類別

定義

表示修改過的整數是標準的 C++ long 值。 此類別無法獲得繼承。

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

範例

以下範例示範如何利用反射將物體發射 IsLong 到組裝體中。



#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 與整數值是不同的類型。 然而,它們在集合中都被表示 ELEMENT_TYPE_I4 。 為了區分 long 與 C++ 中的整數,Microsoft C++ 編譯器會在實例發出時,為任何 IsLong 實例加上 long 修飾符。 此過程對於維持語言層級型別安全至關重要。

編譯器會在元資料中輸出自訂修飾符,以改變即時(JIT)編譯器處理預設行為不適當的值方式。 當 JIT 編譯器遇到自訂修飾符時,會依照修飾符指定的方式處理該值。 編譯器可以對方法、參數和回傳值套用自訂修飾符。 JIT 編譯器必須回應所需的修飾符,但可忽略可選的修飾符。

你可以使用以下其中一種技術,將自訂修飾符輸出到元資料中:

適用於