Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

IsLong Class

Indicates that a modified integer is a standard C++ long value. This class cannot be inherited.

System.Object
  System.Runtime.CompilerServices.IsLong

Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)
public static class IsLong

The C++ standard indicates that a long value and an integer value are distinct types. However, they are both represented using ELEMENT_TYPE_I4 in an assembly. 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.

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. 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. 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:

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.");
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.