注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

C#中动态创建DLL文件  

2011-04-24 18:10:01|  分类: C# & .NET |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

这个是网上看到的一段代码,利用反射创建了一个程序集,很强大。

using System;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
namespace DynamicDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            // 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);
        }
    }
}
  评论这张
 
阅读(773)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016