惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
V2EX - 技术
V2EX - 技术
美团技术团队
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
C
Cybersecurity and Infrastructure Security Agency CISA
W
WeLiveSecurity
博客园 - 聂微东
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Help Net Security
GbyAI
GbyAI
G
GRAHAM CLULEY
The Last Watchdog
The Last Watchdog
U
Unit 42
罗磊的独立博客
B
Blog RSS Feed
K
Kaspersky official blog
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Know Your Adversary
Know Your Adversary
IT之家
IT之家
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
S
Schneier on Security
Last Week in AI
Last Week in AI
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 热门话题
雷峰网
雷峰网
Martin Fowler
Martin Fowler
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
Netflix TechBlog - Medium
T
Tor Project blog
The GitHub Blog
The GitHub Blog
The Hacker News
The Hacker News
F
Fortinet All Blogs
Webroot Blog
Webroot Blog
Spread Privacy
Spread Privacy
Cloudbric
Cloudbric
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
Microsoft Security Blog
Microsoft Security Blog

博客园 - 杞人

float、double为0判断 Excel操作知识(持续补充) 算法设计方案 解决由于DTD规范引发Table设置高度无效 - 杞人 - 博客园 VS.NET 代码折叠Region C#访问FTP VS.NET 自动生成版本号问题 - 杞人 - 博客园 UDP通讯 DataSet中表的关系及约束 Web Services 获取当前路径! Windows Services .NET超时解决方案 Facade 外观模式(结构型模式) Decorator 装饰模式(结构型模式) AJAX.NET请求时发生异常处理方案 setTimeout和setInterval的使用说明 write( ) 和 writeln( )使用说明 Composite 组合模式(结构型模式)
C# 动态编译及反射执行
杞人 · 2010-08-04 · via 博客园 - 杞人

        #region 程序集1 无参数、静态类下的静态方法

        static void CodeAssembly_1()
        {
            //提供对 C# 代码生成器和代码编译器的实例的访问。
            CSharpCodeProvider provider = new CSharpCodeProvider();

            //表示用于调用编译器的参数
            CompilerParameters parameter = new CompilerParameters();
            //获取当前项目所引用的程序集。
            parameter.ReferencedAssemblies.Add("System.dll");
            //获取或设置一个值,该值指示是否生成可执行文件。
            parameter.GenerateExecutable = false;
            //获取或设置一个值,该值指示是否在内存中生成输出。
            parameter.GenerateInMemory = true;

            //使用指定的编译器设置,从包含源代码的字符串的指定数组,编译程序集。返回编译结果。
            CompilerResults result = provider.CompileAssemblyFromSource(parameter,
                GetCode_1("120/12"));//将你的式子放在这里
            //获取编译器错误和警告的集合
            if (result.Errors.Count > 0)
            {
                Console.WriteLine("动态编译出错了!");
            }
            else
            {
                //获取或设置已编译的程序集。
                Assembly assembly = result.CompiledAssembly;
                //获取当前实例的 Type
                Type AType = assembly.GetType("NameSpace_1.Class_1");
                //获取类型的方法
                MethodInfo method = AType.GetMethod("Func");
                //调用方法
                Console.WriteLine(method.Invoke(null, null));
            }
            Console.Read();
        }
        //获取代码
        private static string GetCode_1(string para)
        {
            StringBuilder codes = new StringBuilder();
            //引用
            codes.Append("using System; ");
            //命名空间定义
            codes.Append("namespace NameSpace_1");
            codes.Append("{");
            //类定义
            codes.Append("  public class Class_1");
            codes.Append("  {");
            //方法定义
            codes.Append("      public static object Func()");
            codes.Append("      {");
            codes.Append("          return (" + para + ");");
            codes.Append("      }");
            codes.Append("  }");
            codes.Append("}");

            return (codes.ToString());
        }

        #endregion

        #region 程序集2 含构造函数类需要生成实例对象后调用还参数方法

        static void CodeAssembly_2()
        {
            //提供对 C# 代码生成器和代码编译器的实例的访问。
            CSharpCodeProvider provider = new CSharpCodeProvider();

            //表示用于调用编译器的参数
            CompilerParameters parameter = new CompilerParameters();
            //获取当前项目所引用的程序集。
            parameter.ReferencedAssemblies.Add("System.dll");
            //获取或设置一个值,该值指示是否生成可执行文件。
            parameter.GenerateExecutable = false;
            //获取或设置一个值,该值指示是否在内存中生成输出。
            parameter.GenerateInMemory = true;

            //使用指定的编译器设置,从包含源代码的字符串的指定数组,编译程序集。返回编译结果。
            CompilerResults result = provider.CompileAssemblyFromSource(parameter, GetCode_2());
            //获取编译器错误和警告的集合
            if (result.Errors.Count > 0)
            {
                Console.WriteLine("动态编译出错了!");
            }
            else
            {
                //获取或设置已编译的程序集。
                Assembly assembly = result.CompiledAssembly;
               
                //获取当前实例的 Type
                Type AType = assembly.GetType("NameSpace_2.Class_2");

                //动态生成类的实例  含构造函数调用
                object assemblyObject = System.Activator.CreateInstance(AType, 100);

                //获取类型的方法
                MethodInfo method = AType.GetMethod("Plus");

                //调用方法
                object[] methodParameters = new object[2];
                methodParameters[0] = 5.5;
                methodParameters[1] = 4.5;
                //由于是调用类方法,因此需要指定类实例和方法参数。
                Console.WriteLine(method.Invoke(assemblyObject, methodParameters));
            }
            Console.Read();
        }

        //获取代码
        private static string GetCode_2()
        {
            StringBuilder codes = new StringBuilder();
            //引用
            codes.Append("using System; ");
            //命名空间定义
            codes.Append("namespace NameSpace_2");
            codes.Append("{");
            //类定义
            codes.Append("  public class Class_2");
            codes.Append("  {");
            //私有成员定义
            codes.Append("      private double c = 0.0;");
            //构造函数定义
            codes.Append("      public Class_2(double c)");
            codes.Append("      {");
            codes.Append("          this.c = c;");
            codes.Append("      }");
            //方法定义
            codes.Append("      public double Plus(double a, double b)");
            codes.Append("      {");
            codes.Append("          return (a + b + c);");
            codes.Append("      }");

            codes.Append("  }");
            codes.Append("}");

            return (codes.ToString());
        }

        #endregion