我正在编写一个例程来使用DynamicMethod从对象中检索值.它适用于大多数数据类型,但DateTime.Ticks除外,它是int64
在以下测试应用程序中.我使用MethodInfo和DynamicMethod,methodInfo返回正确的值,但DynamicMethod不返回.有任何想法吗?
using System;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication2
{
public delegate object MemberGetDelegate(object obj);
class Program
{
static void Main(string[] args)
{
DateTime dat = DateTime.Today;
PropertyInfo pi = typeof(DateTime).GetProperty("Ticks");
MethodInfo mi = pi.GetGetMethod();
Type type = pi.PropertyType;
object ticks = mi.Invoke(dat, new object[] { });
Console.WriteLine("Get by MethodInfo " + ticks.ToString());
MemberGetDelegate mget=TypeUtils.GetMemberFunc(pi);
object ret = mget(dat);
Console.WriteLine("Get by DynamicMethod " + ret.ToString());
Console.Read();
}
}
static class TypeUtils
{
public static readonly Type objectType = typeof(object); …Run Code Online (Sandbox Code Playgroud)