Beispiel #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));
        }
Beispiel #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);
                }
            }
        }