反射到表达式树:传递参数并访问静态类上的字段,但带有表达式树

问题描述

出于性能方面的考虑,我们试图将我们的例程从反射树重构到表达式树。 我们已经进行了必要的计算,并得出结论认为此更改将使我们的用户群受益。

可悲的是,尽管我们正在努力处理琐碎的事情……起初:使用动态字段名,并在表达式树中返回值。 这些有很多通用的“硬连线”示例,但是我们还没有找到合适的实际使用案例的合适线索。

基本上我们有以下两个对象(为方便发布而略作调整)

        public struct clsAlterRowTableObject
        {
           public string propActor { get; set; }
           public string propIdColumnName { get; set; }
        }

        public static class clsAlterRowTableManager
        {
            public static clsAlterRowTableObject table1 = new clsAlterRowTableManager.clsAlterRowTableObject
            {
                 propActor = "table1 ",propIdColumnName = "table1Id"
            };

            public static clsAlterRowTableObject table2 = new clsAlterRowTableManager.clsAlterRowTableObject
            {
                 propActor = "table2",propIdColumnName = "table2Id"
            };
        }

,我们具有以下功能

            private static clsAlterRowTableObject metGetField(string varpFieldName)
            {
                FieldInfo varManagerTableProperty = typeof(clsAlterRowTableManager).GetField(varpFieldName,BindingFlags.Public | BindingFlags.Static );
                return (clsAlterRowTableObject)varManagerTableProperty.GetValue(null);
            }

函数成功返回管理器字段的实例(表1和表2),当前可满足我们的目的。该功能每小时可能被调用数千次。

现在我们尝试在表达式树中转换所述函数。 我们的目的是声明对已编译Func的静态引用,并使用该引用执行调用...,但首先我们需要成功地转换方法:)

            private static Func<string,clsAlterRowTableObject> metGetTableInstance()
            {
                //parameter deFinition
                ParameterExpression varTableName = Expression.Parameter(typeof(string),"varpFieldName");
                BindingFlags varFlags = BindingFlags.Public | BindingFlags.Static;
                Expression varBindingFlags = Expression.Constant(varFlags);

                //return value deFinition
                ParameterExpression varReturnValue = Expression.Variable(typeof(clsAlterRowTableManager.clsAlterRowTableObject));
                LabelTarget varReturnTarget = Expression.Label(typeof(clsAlterRowTableObject));
                GotoExpression varReturnExpression = Expression.Return(varReturnTarget,varReturnValue,typeof(clsAlterRowTableManager.clsAlterRowTableObject));
                LabelExpression varReturnLabel = Expression.Label(varReturnTarget,Expression.Constant(new clsAlterRowTableManager.clsAlterRowTableObject { }));

                //original call
                //FieldInfo varManagerTableProperty = typeof(clsAlterRowTableManager).GetField(varpTableName,BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                //return (clsAlterRowTableObject)varManagerTableProperty.GetValue(null);

                //this test works
                var varProperty = Expression.Field(null,typeof(clsAlterRowTableManager),"table1");

                //...but how can we use the actual parameter for the field name
                var varProperty = Expression.Field(null,???somethingsomethingvarpFieldName???);
                //...more statements with expressions list,block and compile,to be focused on later

这就是问题所在:通过一个Google搜索,您可以立即找到有关如何通过常量名称检索字段的示例,但是缺少有关如何将实际参数用作值的示例。 此外,尝试-返回-值时也存在类似的问题...,但我们很乐意立即解决参数传递问题。

谢谢。

PS 这是一个封闭的问题的转发和重写。我们感谢对此发表评论的人。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)