我需要一些东西来在运行时构建动态属性检索方法并快速执行,因此我使用Reflection Emit
开发了一个解决方案:
public static void Func(string properyName, A obj)
{
var type = obj.GetType();
var dynamicMethod = new DynamicMethod("PropertyExtractor", typeof(string), new[] { typeof(A) }, type, true);
var ilGen = dynamicMethod.GetILGenerator();
var getMethod = type.GetMethod($"get_{properyName}");
var property = type.GetProperty(properyName);
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Castclass, type);
ilGen.Emit(OpCodes.Callvirt, getMethod);
var toStringMethod = property.PropertyType.GetMethod("ToString", Type.EmptyTypes);
ilGen.Emit(OpCodes.Call, toStringMethod);
ilGen.Emit(OpCodes.Ret);
var @delegate = (F)dynamicMethod.CreateDelegate(typeof(F));
var a = @delegate(obj);
}
public delegate string F(A obj);
public abstract class A
{
public int Id { get; set; }
public string Name { get; set; }
}
public class B : A
{
public DateTime Timestamp { get; set; }
}
下面是调用它的代码:
var obj = new B
{
Id = 1,
Name = "SomeName",
Timestamp = DateTime.Today
};
Func("Timestamp", obj);
这只是为了测试,这就是为什么它位于名为 Func
的方法中,参数类型为 A
等。
正如您所看到的,您为它提供了属性和实例的名称,它会创建一个委托(delegate)来检索该特定实例中属性的字符串值。一切正常,直到出现 DateTime
。我使用分配给属性的 DateTime.Now
进行测试,每次运行该函数时,我都会得到奇怪的值,例如:
我尝试放置一个 IFormatProvider 以防万一 - 结果相同。
.NET版本是4.7.1,
提前致谢。
最佳答案
我发现了问题。 DateTime 是值类型,而 ToString 需要将地址加载到计算堆栈上,而不是值。当我添加当时的局部变量并添加将值存储在局部变量中然后加载该变量的地址的指令时,它就被解决了。感谢所有回复的人。
关于c# - 动态生成的 MSIL 中奇怪的 DateTime ToString 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395976/