【问题标题】:Compiling at run time in Visual C#在 Visual C# 中运行时编译
【发布时间】:2018-06-04 02:52:48
【问题描述】:

我正在学习使用CSharpCodeProviderCompilerParametersCompilerResults 等在运行时编译文件。

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

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,我不知道如何获取字符串的content,例如。 这是一个测试字符串!

以下是代码:

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.
        }
    }
}

【问题讨论】:

标签: c# string properties compilation


【解决方案1】:

试试这个:

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";
        }
    }        
}

【讨论】:

  • 感谢您的回答,但我已经声明我已经能够通过该方法获取字符串。我想了解如何直接获取字符串。非常感谢!
  • 这就像我刚刚提供的代码的一部分! >"property或者直接获取字符串!?
  • @Pikachu620 你的意思是:string test = MyMethod()?
  • 我的意思是,string test = "This is a test string!"; 这可能吗!?或者通过方法调用是唯一的方法!?
  • @Pikachu620 是的,可以使用string test = "This is a test string!"; 并通过方法调用,两种方式都可以
【解决方案2】:
program.getField("testStr").getValue(null). 

从程序集获取Type后试试这个方法

【讨论】:

  • 谢谢!我已将接受的答案更改为您的答案!但请务必删除其他答案!谢谢!
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多