Exemplo n.º 1
0
        // TODO: Put strings into the resources.
        //
        public static MemberInfo ExtractMember(LambdaExpression expr, string altExprArgName = default)
        {
            var argName = string.IsNullOrEmpty(altExprArgName) ? nameof(expr) : altExprArgName.FmtStr().G();

            expr.EnsureNotNull(argName);
            //
            var exprBody = expr.Body;

            if (exprBody is MemberExpression bodyAsMemberExpr)
            {
                return(bodyAsMemberExpr.Member);
            }
            else if (exprBody is MethodCallExpression bodyAsMethodCallExpr)
            {
                return(bodyAsMethodCallExpr.Method);
            }
            else
            {
                throw new ArgumentException(message: $"Specified expression does not access a member and doesn't access a method.{Environment.NewLine}\tExpression:{expr.FmtStr().GNLI2()}", paramName: argName);
            }
        }
Exemplo n.º 2
0
        // TODO: Put exception messages into the resources.
        //
        public static PropertyInfo ExtractNonStaticProperty(LambdaExpression propExpr, string altPropExprArgName = default)
        {
            var prop = ExtractMember(propExpr, altExprArgName: string.IsNullOrEmpty(altPropExprArgName) ? nameof(propExpr) : altPropExprArgName) as PropertyInfo;

            if (prop is null)
            {
                throw
                    new ArgumentException(
                        message: $"Specified member access expression does not access a property.{Environment.NewLine}\tExpression:{propExpr.FmtStr().GNLI2()}",
                        paramName: string.IsNullOrEmpty(altPropExprArgName) ? nameof(propExpr) : altPropExprArgName);
            }
            else
            {
                prop.Arg(string.IsNullOrEmpty(altPropExprArgName) ? nameof(propExpr) : altPropExprArgName).EnsureNonStatic();
                return(prop);
            }
        }
Exemplo n.º 3
0
 // TODO: Put strings into the resources.
 //
 public static void EnsurePropertyReadExpression(LambdaExpression instance, LambdaExpression prop, string altInstanceArgName = default, string altPropArgName = default)
 {
     if (!IsPropertyReadExpression(instance: instance, prop: prop, altInstanceArgName: altInstanceArgName, altPropArgName: altPropArgName))
     {
         throw
             new ArgumentException(
                 message: $"Specified expression is not an expression reading a property of the specified type.{Environment.NewLine}\tExpression:{prop.FmtStr().GNLI2()}{Environment.NewLine}\tType:{instance.ReturnType.FmtStr().GNLI2()}",
                 paramName: string.IsNullOrEmpty(altPropArgName) ? nameof(prop) : altPropArgName);
     }
 }
Exemplo n.º 4
0
 static void P_EnsureDefinedProperty(Type type, LambdaExpression propExpr, out PropertyInfo extractedPropInfo, string altTypeArgName = null, string altPropExprArgName = null)
 {
     if (!P_IsDefinedProperty(type, propExpr, out extractedPropInfo, altTypeArgName: altTypeArgName, altPropExprArgName: altPropExprArgName))
     {
         throw new ArgumentException(message: $"Specified type doesn't have a property specified by expression.{Environment.NewLine}\tType:{type.FmtStr().GNLI2()}{Environment.NewLine}\tExpression:{propExpr.FmtStr().GNLI2()}", paramName: string.IsNullOrEmpty(altPropExprArgName) ? "propertyExpression" : altPropExprArgName);
     }
 }