首页 新闻 会员 周边 捐助

求教个反编译的方法

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-11-21 21:53

用ILSpy反编译一个dll,其中有个函数无法编译通过.如下(别的反编译工具生成代码可能不甚相同):

 
 1 public static IEnumerable<T> Query<T>(this DbConnection cn, string commandText, params object[] parameters)
 2 {
 3     var func = null;
 4     List<T> list = new List<T>();
 5     DbCommand dbCommand = DB.CreateCommand(commandText, cn, parameters);
 6     IEnumerable<DB.PropInfo<T>> props = null;
 7     Type typeFromHandle = typeof(T);
 8     bool flag = typeFromHandle.IsPrimitive || typeFromHandle.IsValueType || typeFromHandle == typeof(string);
 9     if (!flag)
10     {
11         props = DB.GetSettableProps<T>();
12     }
13     DbDataReader dbDataReader = dbCommand.ExecuteReader();
14     if (!dbDataReader.HasRows)
15     {
16         dbDataReader.Close();
17         return list;
18     }
19     if (flag)
20     {
21         while (dbDataReader.Read())
22         {
23             list.Add((T)((object)Convert.ChangeType(dbDataReader[0], typeof(T))));
24         }
25     }
26     else
27     {
28         List<string> list2 = new List<string>();
29         for (int i = 0; i < dbDataReader.FieldCount; i++)
30         {
31             list2.Add(dbDataReader.GetName(i));
32         }
33         var arg_10E_0 = 
34             from n in list2
35             select new
36             {
37                 n = n,
38                 prop = props.FirstOrDefault((DB.PropInfo<T> p) => string.Equals(p.Name, n, StringComparison.Ordinal)) ?? props.FirstOrDefault((DB.PropInfo<T> p) => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase))
39             };
40         if (func == null)
41         {
42             func = (<>h__TransparentIdentifier0 => new
43             {
44                 Name = <>h__TransparentIdentifier0.n,
45                 Property = <>h__TransparentIdentifier0.prop
46             });
47         }
48         var list3 = arg_10E_0.Select(func).ToList();
49         while (dbDataReader.Read())
50         {
51             T t = Activator.CreateInstance<T>();
52             foreach (var current in list3)
53             {
54                 current.Property.Setter(t, Convert.ChangeType(dbDataReader[current.Name], current.Property.Type));
55             }
56             list.Add(t);
57         }
58     }
59     dbDataReader.Close();
60     return list;
61 }

这里有几句无法编译:

if (func == null)
{
func = (<>h__TransparentIdentifier0 => new
{
    Name = <>h__TransparentIdentifier0.n,
    Property = <>h__TransparentIdentifier0.prop
});
}    

求精通C#的大神能帮忙还原成可编译通过的代码?谢谢!

CodeYu的主页 CodeYu | 初学一级 | 园豆:184
提问于:2012-11-21 21:21
< >
分享
所有回答(1)
0
public static IEnumerable<T> Query<T>(this DbConnection cn, string commandText, params object[] parameters)
        {
            //var func = null;
            List<T> list = new List<T>();
            DbCommand dbCommand = DB.CreateCommand(commandText, cn, parameters);
            IEnumerable<DB.PropInfo<T>> props = null;
            Type typeFromHandle = typeof(T);
            bool flag = typeFromHandle.IsPrimitive || typeFromHandle.IsValueType || typeFromHandle == typeof(string);
            if (!flag)
            {
                props = DB.GetSettableProps<T>();
            }
            DbDataReader dbDataReader = dbCommand.ExecuteReader();
            if (!dbDataReader.HasRows)
            {
                dbDataReader.Close();
                return list;
            }
            if (flag)
            {
                while (dbDataReader.Read())
                {
                    list.Add((T)((object)Convert.ChangeType(dbDataReader[0], typeof(T))));
                }
            }
            else
            {
                List<string> list2 = new List<string>();
                for (int i = 0; i < dbDataReader.FieldCount; i++)
                {
                    list2.Add(dbDataReader.GetName(i));
                }
                var arg_10E_0 = 
                    from n in list2
                    select new
                    {
                        n = n,
                        prop = props.FirstOrDefault((DB.PropInfo<T> p) => string.Equals(p.Name, n, StringComparison.Ordinal)) ?? props.FirstOrDefault((DB.PropInfo<T> p) => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase))
                    };
                //if (func == null)
                //{
                    //func = (<>h__TransparentIdentifier0 => new
                    //{
                        //Name = //<>h__TransparentIdentifier0.n,
                        //Property = //<>h__TransparentIdentifier0.prop
                    //});
                //}
                var list3 = arg_10E_0.Select(item => new { Name = item.n, Property = item.prop });
                while (dbDataReader.Read())
                {
                    T t = Activator.CreateInstance<T>();
                    foreach (var current in list3)
                    {
                        current.Property.Setter(t, Convert.ChangeType(dbDataReader[current.Name], current.Property.Type));
                    }
                    list.Add(t);
                }
            }
            dbDataReader.Close();
            return list;
        }
CodeYu | 园豆:184 (初学一级) | 2012-11-21 21:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册