通过反射调用泛型参数方法

最近在学习EDM,发现LinqPad是一个好东西,可以运行Linq To Sql, Entity SQL Language脚本,但不会用ESql查询功能,始终报错,半天找不到门道,一怒之下决定自己写一个小型的ESql查询器。

 

将比较重要的知识点归纳了一下,贴几段重要的代码,以免下次遗忘。

 

        private void btnRunQuery_Click(object sender, EventArgs e)
        {
            if (cmbEntitySet.SelectedItem == null)
                return;
            Type typeObjectQuery = (cmbEntitySet.SelectedItem as ImageComboBoxItem).Value as Type;
            Type typeEntity = typeObjectQuery.GetGenericArguments()[0];

            using (cdms3Entities ctx = new cdms3Entities())
            {
                //通过反射调用ObjectQuery CreateQuery(string queryString, params ObjectParameter[] parameters);
                MethodInfo miCreateQuery = ctx.GetType().GetMethod("CreateQuery", BindingFlags.Instance | BindingFlags.Public);
                //设置CreateQuery中的T泛型参数为typeEntity所指的实体对象类型
                miCreateQuery = miCreateQuery.MakeGenericMethod(typeEntity);
                //创建调用参数,传入Entity SQL Language语句
                object[] args = new object[] { mmeESql.Text, new ObjectParameter[] { } };
                //调用CreateQuery函数,返回一个ObjectQuery类型的对象,ObjectQuery泛型类由ObjectQuery类派生,所以可转换为ObjectQuery类型
                ObjectQuery oq = miCreateQuery.Invoke(ctx, args) as ObjectQuery;

                //如果下列检查框中有选择需要Include到查询中的实体对象
                if (cmbIncludes.Properties.GetCheckedItems() != "")
                {
                    //获取ObjectQuery Include(string path);函数,注意函数的返回值类型是泛型,但函数并不是泛型,所以不需要也不能对其调用MakeGenericMethod方法
                    MethodInfo miInclude = oq.GetType().GetMethod("Include", BindingFlags.Instance | BindingFlags.Public);
                    foreach (CheckedListBoxItem item in cmbIncludes.Properties.Items)
                    {
                        if (item.CheckState == CheckState.Checked)
                        {
                            EntityObjectInfo eo = item.Value as EntityObjectInfo;
                            args = new object[] { eo.Path };
                            //调用ObjectQuery.Include方法
                            oq = miInclude.Invoke(oq, args) as ObjectQuery;
                        }
                    }
                }

                //下面调用ObjectQuery.ToList()方法来获取查询结果。
                //特别要注意的是ToList()方法是一个扩展方法,方法原型是:public static List ToList(this IEnumerable source);位于System.Linq.Enumerable类
                //所以反射时应当对Enumerable类进行,且该方法为公共静态方法。
                MethodInfo miToList = typeof(Enumerable).GetMethod("ToList", BindingFlags.Static | BindingFlags.Public);
                miToList = miToList.MakeGenericMethod(typeEntity);
                args = new object[] { oq };
                object o = miToList.Invoke(null, args);
                //var q = ctx.CreateQuery(mmeESql.Text);
                mmeTraceSql.Text = oq.ToTraceString();
                grdResultData.DataSource = o;
            }

        }

 

转载于:https://www.cnblogs.com/wiseant/archive/2009/12/14/1624088.html

你可能感兴趣的:(通过反射调用泛型参数方法)