private SwitchRecord CreateSwitchRecord(object obj, MemberInfo member, CommandLineSwitchAttribute switchAttribute) { SwitchRecord rec; if (member is PropertyInfo) { PropertyInfo pi = (PropertyInfo)member; rec = new SwitchRecord(switchAttribute.Name, switchAttribute.Description, pi.PropertyType); // Map in the Get/Set methods. rec.SetMethod = pi.GetSetMethod(); rec.GetMethod = pi.GetGetMethod(); rec.PropertyOwner = obj; } else { throw new NotImplementedException(member.GetType().FullName); } foreach (CommandLineAliasAttribute aliasAttrib in member.GetCustomAttributes(typeof(CommandLineAliasAttribute), false)) { rec.AddAlias(aliasAttrib.Alias); } return(rec); }
/// <summary> /// Initializes a new instance of the <see cref="CommandLineParser"/> class. /// </summary> /// <param name="commandLine">The command line.</param> /// <param name="obj">The class for auto attributes.</param> public CommandLineParser(string commandLine, object obj) { m_commandLine = commandLine; Type type = obj.GetType(); foreach (MemberInfo member in type.GetMembers()) { object[] attributes = member.GetCustomAttributes(typeof(CommandLineSwitchAttribute), false); if (attributes.Length == 0) { continue; } CommandLineSwitchAttribute switchAttribute = (CommandLineSwitchAttribute)attributes[0]; switches.Add(switchAttribute.Name, CreateSwitchRecord(obj, member, switchAttribute)); } }