Example #1
0
        public void Main()
        {
            object eval;

            if (PropertyName == "$item")
            {
                eval = FilterTarget;
            }
            else
            {
                var evalProp = FilterTarget.GetType().GetProperty(PropertyName);
                if (evalProp == null)
                {
                    return;
                }
                if (evalProp.GetGetMethod() == null)
                {
                    return;
                }

                eval = evalProp.GetValue(FilterTarget, null);
                if (eval == null)
                {
                    return;
                }
            }

            if (eval is string == false && (Operator == FilterOperators.Contains || Operator == FilterOperators.NotContains))
            {
                throw new ArgException("The Contains and NotContains operators can only be applied to properties of type string");
            }

            object comparison = Value;

            try
            {
                if (eval.GetType() != typeof(string))
                {
                    if (ArgRevivers.CanRevive(eval.GetType()))
                    {
                        comparison = ArgRevivers.Revive(eval.GetType(), "", comparison + "");
                    }
                }
            }
            catch (Exception ex)
            {
                PowerLogger.LogLine("Unable to convert a string to:" + eval.GetType().FullName);
                return;
            }

            if (FilterLogic.FilterAcceptsObject(eval, Operator, comparison))
            {
                ArgPipeline.Push(FilterTarget);
            }
        }
Example #2
0
        public object MapIncompatibleDirectTargets(Type desiredType, object incompatibleObject)
        {
            if (incompatibleObject is JObject == false)
            {
                throw new ArgumentException("Type not supported, must be JObject: " + incompatibleObject.GetType().FullName);
            }

            var revivedValue = Activator.CreateInstance(desiredType);

            int mappedProps = 0;

            foreach (JProperty prop in ((JObject)(incompatibleObject)).Properties())
            {
                if (prop.Type == JTokenType.Object || prop.Type == JTokenType.Array || prop.Type == JTokenType.Bytes || prop.Type == JTokenType.Constructor || prop.Type == JTokenType.Comment)
                {
                    continue;
                }

                var targetProp = desiredType.GetProperty(prop.Name);

                if (targetProp == null || targetProp.GetSetMethod() == null)
                {
                    continue;
                }

                if (prop.Type == JTokenType.Null)
                {
                    targetProp.SetValue(revivedValue, null, null);
                    mappedProps++;
                }
                else if (ArgRevivers.CanRevive(targetProp.PropertyType))
                {
                    PowerLogger.LogLine("Mapped JObject property to " + desiredType.Name + "." + targetProp.Name);
                    var revivedPropValue = ArgRevivers.Revive(targetProp.PropertyType, prop.Name, prop.Value.ToString());
                    targetProp.SetValue(revivedValue, revivedPropValue, null);
                    mappedProps++;
                }
            }

            if (mappedProps == 0)
            {
                throw new ArgException("Could not map the given object to type: " + desiredType);
            }

            return(revivedValue);
        }
Example #3
0
 private bool TrySimpleConvert(object o, Type target, out object result)
 {
     if (o is string && ArgRevivers.CanRevive(target))
     {
         result = ArgRevivers.Revive(target, null, (string)o);
         return(true);
     }
     else
     {
         try
         {
             result = Convert.ChangeType(o, target);
             return(true);
         }
         catch (Exception ex)
         {
             result = null;
             return(false);
         }
     }
 }