コード例 #1
0
        GetTargetProperties(IArgumentTarget target)
        {
            var argumentProperties = new Dictionary <PropertyInfo, CommandLineArgumentPropertyAttribute>();

            var propertyInfos = target.GetType()
                                .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var propertyInfo in propertyInfos)
            {
                var attributes = propertyInfo.GetCustomAttributes(typeof(CommandLineArgumentPropertyAttribute), false);
                if (attributes.Length != 1)
                {
                    continue;
                }

                argumentProperties.Add(propertyInfo, (CommandLineArgumentPropertyAttribute)attributes[0]);
            }

            return(argumentProperties);
        }
コード例 #2
0
        private static void InjectArgumentValues(IReadOnlyDictionary <string, string> keyValues, IArgumentTarget target)
        {
            var argumentProperties = GetTargetProperties(target);

            foreach (var propertyAttr in argumentProperties)
            {
                var propertyInfo = propertyAttr.Key;
                var attribute    = propertyAttr.Value;

                if (!keyValues.ContainsKey(attribute.Key))
                {
                    if (attribute.DefaultValue != null)
                    {
                        propertyInfo.SetValue(target, attribute.DefaultValue);
                    }

                    continue;
                }

                var argument = keyValues[attribute.Key];

                var value = GetValue(argument, propertyInfo.PropertyType, attribute.DefaultValue);
                propertyInfo.SetValue(target, value);
            }
        }