예제 #1
0
 /// <summary>
 /// Try to construct an Enum expression for the given enum type
 /// </summary>
 /// <param name="enumType">Enum type to create the expression for</param>
 /// <param name="formatter">Optional delegate function that formats the enum to a string</param>
 /// <returns>The constructed expression or null</returns>
 public static CompiledExpression SafeGetEnumCompiledExpression(Type enumType, Delegate formatter = null)
 {
     try
     {
         if (!enumExpressions.ContainsKey(enumType))
         {
             Type genericClass          = typeof(CompiledExpression <,>);
             Type constructedClass      = genericClass.MakeGenericType(enumType, typeof(String));
             Func <Object, String> func = null;
             if (formatter != null)
             {
                 func = (Object o) => (String)formatter.DynamicInvoke(o);
             }
             Expression enumExpr = EnumTranslation.BuildExpression(enumType, func);
             var        cp       = Activator.CreateInstance(constructedClass, enumExpr) as CompiledExpression;
             if (cp != null)
             {
                 enumExpressions[enumType] = cp;
             }
             return(cp);
         }
         return(enumExpressions[enumType]);
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Cannot construct the Enum expression for {0}, error occured: {1}", enumType.FullName, ex.Message);
     }
     return(null);
 }
예제 #2
0
            /// <summary>
            ///  Search for a compiled expression mapped to the given parameterless method
            /// </summary>
            /// <param name="methodName">Name of the method to search for</param>
            /// <param name="type">Type to search against</param>
            /// <returns>Compiled expression if found or null if not</returns>
            private CompiledExpression FindCompiledExpressionMethod(String methodName, Type type, Expression expression)
            {
                Type enumType = null;

                while (type != typeof(Object) && type != null)
                {
                    MethodInfo mi = type.SafeGetPublicMethod(methodName);
                    if (mi != null)
                    {
                        switch (methodName)
                        {
                        case "ToString":
                            var memberExp = expression as MemberExpression;
                            if (memberExp != null)
                            {
                                var pi = memberExp.Member as PropertyInfo;
                                if (pi != null && pi.PropertyType.IsEnum)
                                {
                                    if (enumType == null)
                                    {
                                        type     = pi.PropertyType;
                                        enumType = type;
                                        var expressionType = pi.PropertyType.Assembly.GetType(type.FullName + "Expressions");
                                        if (expressionType != null)
                                        {
                                            EnsureTypeInitialized(expressionType);
                                        }
                                    }
                                }
                            }
                            break;

                        default:
                            break;
                        }

                        EnsureTypeInitialized(type);
                        if (this.map.TryGetValue(mi, out CompiledExpression cp))
                        {
                            if (enumType != null)
                            {
                                cp = EnumTranslation.SafeGetEnumCompiledExpression(enumType, cp.UnderlyingLambda.Compile());
                            }
                            return(cp);
                        }
                    }
                    type = type.BaseType;
                }

                return(null);
            }