【发布时间】:2017-11-16 15:55:05
【问题描述】:
我使用 System.CodeDom.Compiler 生成动态代码,我需要将一些对象传递给代码中的函数,但是当我传递对象时,它们引用我当前的名称空间...
string code = @"
using System;
using " + type + @";
namespace First
{
public class Program
{
static " + type + ".Class1 " + type.ToLower() + " = (" + type + ".Class1)"+o + @";
public static bool check () {
if( " + exp
+
@")
return true;
else
return false;
}
public static void Main()
{
" +
" Console.WriteLine(\"Hello, world!\");"
+ @"
}
}
}
";
我收到此错误:名称“MineRuleEngine”(我当前的命名空间)在当前上下文中不存在
【问题讨论】:
-
什么是
type在运行时以及为什么在末尾使用@字符? -
考虑使用插值字符串来提高可读性:Console.WriteLine($"Name = {name}, hours = {hours:hh}");
-
我的问题是对象“o”是指例如 MineRuleEngine.person 。而我的动态代码不知道“MineRuleEngine”命名空间
-
为什么是动态编程标签?