本文整理匯總了C#中System.Reflection.Emit.UnmanagedMarshal類的典型用法代碼示例。如果您正苦於以下問題:C# UnmanagedMarshal類的具體用法?C# UnmanagedMarshal怎麽用?C# UnmanagedMarshal使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
UnmanagedMarshal類屬於System.Reflection.Emit命名空間,在下文中一共展示了UnmanagedMarshal類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
public class Example
{
public static void Main()
{
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName myAsmName = new AssemblyName("EmitMarshalAs");
AssemblyBuilder myAssembly =
myDomain.DefineDynamicAssembly(myAsmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(myAsmName.Name,
myAsmName.Name + ".dll");
TypeBuilder myType =
myModule.DefineType("Sample", TypeAttributes.Public);
MethodBuilder myMethod =
myType.DefineMethod("Test", MethodAttributes.Public,
null, new Type[] { typeof(string) });
// Get a parameter builder for the parameter that needs the
// attribute, using the HasFieldMarshal attribute. In this
// example, the parameter is at position 0 and has the name
// "arg".
ParameterBuilder pb =
myMethod.DefineParameter(0,
ParameterAttributes.HasFieldMarshal, "arg");
// Get the MarshalAsAttribute constructor that takes an
// argument of type UnmanagedType.
//
ConstructorInfo ci =
typeof(MarshalAsAttribute).GetConstructor(
new Type[] { typeof(UnmanagedType) });
// Create a CustomAttributeBuilder representing the attribute,
// specifying the necessary unmanaged type. In this case,
// UnmanagedType.BStr is specified.
//
CustomAttributeBuilder cabuilder =
new CustomAttributeBuilder(
ci, new object[] { UnmanagedType.BStr });
// Apply the attribute to the parameter.
//
pb.SetCustomAttribute(cabuilder);
// Emit a dummy method body.
ILGenerator il = myMethod.GetILGenerator();
il.Emit(OpCodes.Ret);
Type finished = myType.CreateType();
myAssembly.Save(myAsmName.Name + ".dll");
}
}