Пример #1
0
        /// <summary>
        /// gets a setter to set a field in a class to a value given a string representation of the value
        /// </summary>
        /// <param name="info">The <see cref="FieldInfo"> of the field being set</see></param>
        /// <param name="argumentClass">The class instance that the field will be set in</param>
        /// <returns>A setter (Action) which given a string will set the field to the parsed value of the string</returns>
        internal static Action <object> GetSetter(FieldInfo info, object argumentClass)
        {
            SetterFactory sf      = new SetterFactory(info);
            var           setter2 = sf.GetSetter();

            return(x => setter2(argumentClass, (string)x));
        }
Пример #2
0
        /// <summary>
        /// Initializes the descriptors by scanning the application settings class
        /// </summary>
        private void BuildDescriptors()
        {
            TypeInfo ti = this.applicationArgumentClass.GetType().GetTypeInfo();

            foreach (PropertyInfo property in ti.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                try
                {
                    Action <object> action = SetterFactory.GetSetter(property, this.applicationArgumentClass);
                    bool            found  = false;
                    foreach (CustomAttributeData attributeData in property.CustomAttributes.Where(
                                 x => x.AttributeType.FullName == typeof(ArgumentAttribute).FullName))
                    {
                        this.AddDescriptor(attributeData, action, property.Name, property.PropertyType);
                        found = true;
                    }

                    if (!found)
                    {
                        this.AddDescriptor(null, action, property.Name, property.PropertyType);
                    }
                }
                catch (Exception ex)
                {
                    this.context.Exceptions.Add(ex);
                }
            }

            foreach (FieldInfo field in ti.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // skip property backing fields
                if (field.Name.EndsWith("k__BackingField", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                try
                {
                    Action <object> action = SetterFactory.GetSetter(field, this.applicationArgumentClass);
                    bool            found  = false;
                    foreach (CustomAttributeData attributeData in
                             field.CustomAttributes.Where(
                                 x => x.AttributeType.FullName == typeof(ArgumentAttribute).FullName))
                    {
                        this.AddDescriptor(attributeData, action, field.Name, field.FieldType);
                        found = true;
                    }

                    if (!found)
                    {
                        this.AddDescriptor(null, action, field.Name, field.FieldType);
                    }
                }
                catch (Exception ex)
                {
                    this.context.Exceptions.Add(ex);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Builds an Action which will set the value of
        /// field or property. If the field or property is a List{}
        /// if will check if the field/property has been initialized, if not
        /// it will initialize it. Then it adds the value to the list.
        /// </summary>
        /// <returns>The action object</returns>
        internal Action <object, string> GetSetter()
        {
            Type targetType = SetterFactory.GetListType(this.methodType);

            if (targetType == null)
            {
                Action <object, string> setter = (obj, v) =>
                {
                    object value = ObjectCreator.Create(this.methodType, v);
                    this.setIntoScalar(obj, value);
                };

                return(setter);
            }
            else
            {
                // this is a list, and it may be null, so get the list
                Action <object, string> setter = (obj, v) =>
                {
                    object value = ObjectCreator.Create(targetType, v);
                    object list  = this.get(obj);
                    if (list == null)
                    {
                        ConstructorInfo constructor = this.methodType.GetConstructor(new Type[] { });
                        if (constructor != null)
                        {
                            list = constructor.Invoke(new object[] { });
                            this.setIntoList(obj, list);
                        }
                    }

                    if (list != null)
                    {
                        MethodInfo addMethod = list.GetType().GetMethod("Add");
                        addMethod.Invoke(list, new[] { value });
                    }
                };

                return(setter);
            }
        }