小编Ton*_*ony的帖子

当属性类型为Int64时,DynamicMethod返回不正确的值

我正在编写一个例程来使用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)

.net reflection reflection.emit

5
推荐指数
0
解决办法
848
查看次数

标签 统计

.net ×1

reflection ×1

reflection.emit ×1