代码之家  ›  专栏  ›  技术社区  ›  Pikachu620

在Visual C中运行时编译#

  •  1
  • Pikachu620  · 技术社区  · 6 年前

    我正在学习在运行时使用 CSharpCodeProvider , CompilerParameters , CompilerResults 诸如此类。

    我能够得到字符串的内容 通过 一种方法。

    public static string Test()    //This is in the file to be compiled.
    {
        return "This is a test string!";
    }
    

    使用

    MethodInfo main = program.GetMethod("Test");    //This is in the main program.
    //program is a Assembly Type generated in another part of the program.
    str=main.Invoke(null, null).ToString();
    

    去拿绳子。

    我怎样才能直接拿到绳子?例如

    public string str="This is a test string!";    //This is in the file to be compiled.
    

    我试着把字符串变成一种属性并加以使用 GetProperty("str"); ,但它得到的只是 名称 财产的所有权。 str ,我不知道怎样才能拿到 所容纳之物 绳子的长度。 这是一个测试字符串! .

    以下是代码:

    using System.IO;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;
    
    namespace MudOS
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
                CompilerParameters parameters = new CompilerParameters();
    
                parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                parameters.GenerateInMemory = true;
                parameters.GenerateExecutable = true;
    
                string str = File.ReadAllText(Directory.GetCurrentDirectory() + @"\MudLib\Test.c");
                CompilerResults results = provider.CompileAssemblyFromSource(parameters, str);
    
                if (results.Errors.HasErrors)
                {
                    MessageBox.Show("Compiling error!");
                    return;
                }
                Assembly assembly = results.CompiledAssembly;
                Type program = assembly.GetType("MudOS.Test");
                MethodInfo main = program.GetMethod("Test");
                resultString=main.Invoke(null, null).ToString();
                MessageBox.Show(resultString);
    
                PropertyInfo pinfo = program.GetProperty("str");
                MessageBox.Show(pinfo.Name.ToString());
                //I want the CONTENT of the string, not the NAME.
            }
        }
    }
    

    下面是运行时要编译的文件:Test。C

    using System.Windows.Forms;
    
    namespace MudOS
    {
        class Test
        {
            public string testStr="This is a test string!";
            //I would like to know if it's possible to get this string directly.
    
            public string str
            {
                get{ return "This is a test string!"; }
                //I was unable to get this content.
            }
    
            public static void Main()
            {
            }
    
            public static string Test()
            {
                return "This is a test string!";
                //I was able to get this content just fine.
            }
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Héctor M.    6 年前

    试试这个:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Reflection;
    
    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                var type = typeof(MethodContainer);
                var method = type.GetMethod("MyMethod");
                var mc = new MethodContainer();
                string result = (string)method.Invoke(mc, null);
                Console.WriteLine(result); //Output: This is a test
            }
        }
    
        public class MethodContainer
        {
            public string MyMethod()
            {
                return "This is a test";
            }
        }
    }
    

    更新:

    您必须获取类型( typeof(Your_Clas) )然后使用 GetMethod()

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Reflection;
    
    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                MethodInfo method = typeof(Program).GetMethod("MyMethod");
                string result = (string)method.Invoke(null, null);
                Console.WriteLine(result);
    
            }
    
            public static string MyMethod()
            {
                return "LOL";
            }
        }        
    }
    
        2
  •  0
  •   Giorgi Chkhikvadze    6 年前
    program.getField("testStr").getValue(null). 
    

    从程序集中获取类型后,请尝试此方法。