我想避免使用 GetType 和 GetField。我可以在 Ldsfld 中使用字符串吗?我在下面包含了我要完成的工作的模型。如您所见,我是 IL Generation 的新手——我正在尝试消除我的应用程序中反射成本的一些成本。
using System;
using System.Reflection.Emit;
namespace ConsoleApplication10
{
static class Program
{
public static string TextBox1 = "Hello World!";
static void Main(string[] args)
{
var dm = new DynamicMethod("My_method",
typeof(string), null, true);
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldsfld, "string ConsoleApplication10.Program::TextBox1");
il.Emit(OpCodes.Ret);
var func = (Func<string>)dm.CreateDelegate(typeof(Func<string>));
var s = func();
Console.WriteLine(s);
}
}
}