IsSignUnspecifiedByte Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Indicates that a modifier is neither signed nor unsigned. This class cannot be inherited.
public ref class IsSignUnspecifiedByte abstract sealed
public static class IsSignUnspecifiedByte
type IsSignUnspecifiedByte = class
Public Class IsSignUnspecifiedByte
- Inheritance
-
IsSignUnspecifiedByte
Examples
The following code example creates an assembly using classes in the System.Reflection.Emit namespace and emits the IsSignUnspecifiedByte modifier into that assembly.
#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 = {IsSignUnspecifiedByte::typeid};
mainClass->DefineField("modifiedInteger", Type::GetType("System.Byte"), 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.");
}
Remarks
Some programming languages, such as C++, recognize three distinct char
values: signed char
, unsigned char
, and char
. To distinguish the unmodified char
type from the others, the Microsoft C++ compiler adds the IsSignUnspecifiedByte modifier to each char
type emitted to an assembly.
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:
Using methods in the TypeBuilder class such as DefineMethod, DefineField, DefineConstructor, and DefineProperty.
Generating a Microsoft intermediate language (MSIL) instruction file that contains calls to
modopt
andmodreq
, and assembling the file with the Ilasm.exe (IL Assembler).Using the unmanaged reflection API.