Export (0) Print
Expand All

FieldBuilder.SetOffset Method

Specifies the field layout.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public void SetOffset(
	int iOffset
)

Parameters

iOffset
Type: System.Int32
The offset of the field within the type containing this field.

ExceptionCondition
InvalidOperationException

The containing type has been created using CreateType.

ArgumentException

iOffset is less than zero.

The following code sample illustrates the use of SetOffset.


using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
using System.Runtime.CompilerServices;

public class FieldBuilder_Sample
{
   public static Type CreateType(AppDomain currentDomain)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder=myAssembly.DefineDynamicModule("MyModule","MyModule.mod");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);
      TypeBuilder myTypeBuilder2 = myModuleBuilder.DefineType("MyClass2",
         TypeAttributes.Public|TypeAttributes.BeforeFieldInit|TypeAttributes.SequentialLayout|TypeAttributes.AnsiClass|TypeAttributes.Sealed);
      FieldBuilder myFieldBuilder1= myTypeBuilder2.DefineField("myBytes1",
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder2= myTypeBuilder2.DefineField("myBytes2",
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder3= myTypeBuilder2.DefineField("myErrorCode",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder4= myTypeBuilder2.DefineField("myReserved1",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder5= myTypeBuilder2.DefineField("myReserved2",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder6= myTypeBuilder2.DefineField("myPathName",
                                    typeof(char[]),FieldAttributes.Public);
      myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128)); 
      myFieldBuilder6.SetOffset(4);
      Type myType1 = myTypeBuilder2.CreateType();  
      // Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
      Type[] myParameters={ typeof(string), myType1 ,typeof(uint)}; 
      MethodBuilder myMethodBuilder= myTypeBuilder.DefinePInvokeMethod("OpenFile",
                                     "kernel32.dll",MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.HideBySig,
                                       CallingConventions.Standard,typeof(IntPtr),
                                       myParameters,CallingConvention.Winapi,CharSet.None);
      Type myAttributeType = typeof(MethodImplAttribute);
      ConstructorInfo myConstructorInfo = 
         myAttributeType.GetConstructor(new Type[1]{typeof(MethodImplOptions)});
      CustomAttributeBuilder myAttributeBuilder = new CustomAttributeBuilder(myConstructorInfo,
                                                   new object[1]{MethodImplOptions.PreserveSig});
      myMethodBuilder.SetCustomAttribute(myAttributeBuilder);
      ParameterBuilder myParameterBuilder2=myMethodBuilder.DefineParameter(2,
                                            ParameterAttributes.Out,"myClass2");
      Type myType=myTypeBuilder.CreateType();
      myAssembly.Save("EmittedAssembly.dll");
      return myType;
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      try
      {
         Type myType = CreateType(Thread.GetDomain());
         Type myClass2 = myType.Module.GetType("MyClass2"); 
         object myParam2 = Activator.CreateInstance(myClass2);
         uint myUint=0x00000800;
         object[] myArgs= {"MyFile.Txt",myParam2,myUint};
         Object myObject  = myType.InvokeMember("OpenFile",BindingFlags.Public | 
            BindingFlags.InvokeMethod | BindingFlags.Static,null,null,myArgs);
         Console.WriteLine("MyClass.OpenFile method returned: \"{0}\"", myObject);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception Caught "+e.Message);
      }
   }

}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft