コード例 #1
0
        /// <summary>
        ///
        /// <param name="param">ParameterExpression that points to "x=>" parameter</param>
        /// <param name="part">part of the lambda rule like "x.Id = 1"</param>
        /// <returns></returns>
        private BinaryExpression GetRuleExpression(ParameterExpression param, string part)
        {
            var partMatch = rulePartRegex.Match(part);

            if (partMatch.Success)
            {
                Type   tType     = typeof(T);
                string fieldName = partMatch.Groups[1].Value.Replace("(", string.Empty).Replace(param.Name + ".", string.Empty).Trim();
                //var fieldNameParts = fieldName.Split('.');
                //fieldName = fieldNameParts[fieldNameParts.Length - 1];

                string compareOperator = partMatch.Groups[2].Value.Trim();
                string compareTo       = partMatch.Groups[3].Value.Replace(")", string.Empty).Trim();

                if (compareTo.StartsWith("\"") && compareTo.EndsWith("\""))
                {
                    compareTo = compareTo.Substring(1, compareTo.Length - 2);
                }

                System.Reflection.MemberInfo memberInfo = null;
                var    fieldNameParts = fieldName.Split('.');
                string memberStrName  = fieldNameParts[fieldNameParts.Length - 1];
                foreach (var interfType in tType.GetInterfaces())
                {
                    var memberInfos = interfType.GetMember(memberStrName, System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    if (memberInfos.Length > 0)
                    {
                        memberInfo = memberInfos[0]; //CONSIDER: multiple fields can be found
                    }
                }
                if (memberInfo == null)
                {
                    var memberInfos = tType.GetMember(memberStrName, System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    if (memberInfos.Length > 0)
                    {
                        memberInfo = memberInfos[0]; //CONSIDER: multiple fields can be found
                    }
                }

                if (memberInfo == null)
                {
                    throw new Xxception(string.Format("Type '{0}' do not expose member named '{1}'", tType.FullName, memberStrName));
                }
                Type compareToType = memberInfo.GetReturnType();

                var lhs = Expression.MakeMemberAccess(param, memberInfo);
                ConstantExpression rhs;
                if (string.IsNullOrEmpty(compareTo) && compareToType.IsValueType)
                {
                    var val = Activator.CreateInstance(compareToType);
                    rhs = Expression.Constant(val);
                }
                else
                {
                    //if (compareToType.GenericTypeArguments != null && compareToType.GenericTypeArguments.Length == 1) //Nullable type
                    //{
                    //    var underlyingType = compareToType.GenericTypeArguments[0];
                    //    var val = Convert.ChangeType(compareTo, underlyingType);
                    //    rhs = Expression.Constant(val);
                    //}
                    //else
                    //{
                    //    var val = Convert.ChangeType(compareTo, compareToType);
                    //    rhs = Expression.Constant(val);
                    //}
                    var val = ChangeType(compareTo, compareToType);
                    rhs = Expression.Constant(val);
                }
                return(GetCompareExpression(compareOperator, lhs, rhs));
            }
            throw new Xxception(string.Format("Unable to parse lambda expression {0}", part));
        }