Beispiel #1
0
        public Dictionary <string, string> GetAttributes()
        {
            var result = new Dictionary <string, string>();

            var props = this.GetType().GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(ConsoleParamAttribute)));

            foreach (var prop in props)
            {
                ConsoleParamAttribute attributeValue = (ConsoleParamAttribute)Attribute.GetCustomAttribute(prop, typeof(ConsoleParamAttribute));
                result.Add(attributeValue.Name, $"{prop.Name}{(attributeValue.Required ? " (Required)" : "")} - {prop.GetValue(this)}");
            }
            return(result);
        }
Beispiel #2
0
        public static T Read <T>(List <string> args, ILogger logger) where T : ConsoleParamBase
        {
            Type typeOfObject = typeof(T);
            T    targetObject = (T)Activator.CreateInstance(typeOfObject, logger);

            var props = targetObject.GetType().GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(ConsoleParamAttribute)));

            foreach (var prop in props)
            {
                ConsoleParamAttribute attributeValue = (ConsoleParamAttribute)Attribute.GetCustomAttribute(prop, typeof(ConsoleParamAttribute));
                string argumentValue = args.FirstOrDefault(i => i.Contains(attributeValue.Name));
                if (!String.IsNullOrEmpty(argumentValue))
                {
                    prop.SetValue(targetObject, argumentValue.Replace($"-{attributeValue.Name}", ""));
                }
                else if (attributeValue.Required)
                {
                    throw new ConsoleParamException();
                }
            }
            return(targetObject);
        }