本文整理汇总了C#中System.Reflection.Emit.LocalBuilder类的典型用法代码示例。如果您正苦于以下问题:C# LocalBuilder类的具体用法?C# LocalBuilder怎么用?C# LocalBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LocalBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了LocalBuilder类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
class LocalBuilder_Sample
{
public static void Main()
{
// Create an assembly.
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "SampleAssembly";
AssemblyBuilder myAssembly =
Thread.GetDomain().DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.RunAndSave);
// Create a module. For a single-file assembly the module
// name is usually the same as the assembly name.
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(myAssemblyName.Name,
myAssemblyName.Name + ".dll", true);
// Define a public class 'Example'.
TypeBuilder myTypeBuilder =
myModule.DefineType("Example", TypeAttributes.Public);
// Create the 'Function1' public method, which takes an integer
// and returns a string.
MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1",
MethodAttributes.Public | MethodAttributes.Static,
typeof(String), new Type[] { typeof(int) });
// Generate IL for 'Function1'. The function body demonstrates
// assigning an argument to a local variable, assigning a
// constant string to a local variable, and putting the contents
// of local variables on the stack.
ILGenerator myMethodIL = myMethod.GetILGenerator();
// Create local variables named myString and myInt.
LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
myLB1.SetLocalSymInfo("myString");
Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType);
LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
myLB2.SetLocalSymInfo("myInt", 1, 2);
Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType);
// Store the function argument in myInt.
myMethodIL.Emit(OpCodes.Ldarg_0 );
myMethodIL.Emit(OpCodes.Stloc_1 );
// Store a literal value in myString, and return the value.
myMethodIL.Emit(OpCodes.Ldstr, "string value" );
myMethodIL.Emit(OpCodes.Stloc_0 );
myMethodIL.Emit(OpCodes.Ldloc_0 );
myMethodIL.Emit(OpCodes.Ret );
// Create "Example" class.
Type myType1 = myTypeBuilder.CreateType();
Console.WriteLine("'Example' is created.");
myAssembly.Save(myAssemblyName.Name + ".dll");
Console.WriteLine( "'{0}' is created.", myAssemblyName.Name + ".dll" );
// Invoke 'Function1' method of 'Example', passing the value 42.
Object myObject2 = myType1.InvokeMember("Function1",
BindingFlags.InvokeMethod, null, null, new Object[] { 42 });
Console.WriteLine("Example.Function1 returned: {0}", myObject2);
}
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:73,代码来源:LocalBuilder 输出:
local 'myString' type is: System.String
local 'myInt' type is: System.Int32
'Example' is created.
'SampleAssembly.dll' is created.
Example.Function1 returned: string value
注:本文中的System.Reflection.Emit.LocalBuilder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论