Example #1
0
        /// <summary>
        /// Sets the "source" object to the "value" specified in "codeToExecute"
        /// </summary>
        /// <param name="source">Object the code should be executed against</param>
        /// <param name="codeToExecute">Code that should be executed ex. 'Person.Age'</param>
        /// <param name="value">Value to set the source+codeToExecute to.</param>
        /// <param name="createIfNotExists">Creates items it cannot find</param>
        public static Boolean SetValue(object source, String codeToExecute, object value, Boolean createIfNotExists)
        {
            Boolean executed = true;

            ReflectorResult reflectorResult = GetReflectorResult(source, codeToExecute, false, createIfNotExists);

            if (reflectorResult != null)
            {
                TypeConverter typeConverter = null;
                PropertyInfo  propertyInfo  = reflectorResult.MemberInfo as PropertyInfo;
                if (propertyInfo != null)
                {
                    if (propertyInfo.CanWrite)
                    {
                        typeConverter = GetTypeConverter(propertyInfo);

                        ConversionResult conversionResult = ConvertValue(value, propertyInfo.PropertyType, typeConverter);
                        if (conversionResult.Success)
                        {
                            propertyInfo.SetValue(reflectorResult.PreviousValue, conversionResult.ConvertedValue, reflectorResult.MemberInfoParameters);
                        }
                        else
                        {
                            executed = false;
                            PentaLogger.LogVerbose("Invalid value: " + value);
                        }
                    }
                }
                else
                {
                    FieldInfo fieldInfo = reflectorResult.MemberInfo as FieldInfo;
                    if (fieldInfo != null)
                    {
                        typeConverter = GetTypeConverter(fieldInfo);
                        ConversionResult conversionResult = ConvertValue(value, fieldInfo.FieldType, typeConverter);
                        if (conversionResult.Success)
                        {
                            fieldInfo.SetValue(reflectorResult.PreviousValue, conversionResult.ConvertedValue);
                        }
                        else
                        {
                            executed = false;
                            PentaLogger.LogVerbose("Invalid value: " + value);
                        }
                    }
                    else
                    {
                        // both property and field are invalid
                        executed = false;
                    }
                }
            }
            else
            {
                executed = false;
            }

            return(executed);
        }
Example #2
0
        private static ReflectorResult GetReflectorResult(object source, String codeToExecute, bool getLastValue, bool createIfNotExists)
        {
            ReflectorResult result = new ReflectorResult(source);

            try
            {
                // Split the code into usable fragments
                String[] codeFragments = SplitCodeArray(codeToExecute);
                for (Int32 i = 0; i < codeFragments.Length; i++)
                {
                    // if the value is null we cannot go any deeper so don't waste your time
                    if (result.Value == null)
                    {
                        return(result);
                    }

                    String codeFragment = codeFragments[i];
                    result.PreviousValue = result.Value;

                    if (codeFragment.Contains("]"))
                    {
                        ProcessArray(result, codeFragment, createIfNotExists);
                    }
                    else if (codeFragment.Contains(")"))
                    {
                        ProcessMethod(result, codeFragment);
                    }
                    else
                    {
                        // For set properties we do not need the last value
                        bool retrieveValue = getLastValue;
                        if (!retrieveValue)
                        {
                            // If this is not the last one in the array, get it anyway
                            retrieveValue = i + 1 != codeFragments.Length;
                        }
                        ProcessProperty(result, codeFragment, retrieveValue);
                    }
                }
            }
            catch (InvalidCodeFragmentException ex)
            {
                PentaLogger.LogVerbose("Invalid Property: '" + codeToExecute + "' Invalid Fragment: '" + ex.Message + "'");
            }

            return(result);
        }