Example #1
0
        private static void DoPostTypeMemberConversion(Type type)
        {
            bool hasError = false;

            foreach (MemberInfo memberInfo in ChoType.GetMemberInfos(type, typeof(ChoTypeConverterAttribute)))
            {
                object memberValue = null;
                try
                {
                    memberValue = ChoType.GetMemberValue(null, memberInfo.Name);
                    ChoType.SetMemberValue(null, memberInfo, ChoObject.ConvertValueToObjectMemberType(type, memberInfo, memberValue) /* ChoConvert.ChangeType(configObject, memberInfo) */);
                }
                catch (TargetInvocationException)
                {
                    throw;
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    hasError = true;

                    ChoConfigurationObjectErrorManagerService.SetObjectMemberError(type, memberInfo.Name, String.Format("[Value: {0}] - {1}", ChoString.ToString(memberValue), ex.Message));
                    ChoType.SetMemberDefaultValue(null, memberInfo.Name);
                }
            }

            if (hasError)
            {
                ChoConfigurationObjectErrorManagerService.SetObjectError(type, "Object has some validation errors.");
            }
        }
Example #2
0
        public void Validate()
        {
            if (RegistryKey.IsNullOrWhiteSpace())
            {
                throw new ChoConfigurationException("Missing registry key.");
            }

            if (ConfigObjectType == null)
            {
                throw new ChoConfigurationException("Missing configuration object type.");
            }

            //Load RegistryValueKinds
            foreach (MemberInfo memberInfo in ChoType.GetMemberInfos(ConfigObjectType, typeof(ChoMemberRegistryInfoAttribute)))
            {
                ChoMemberRegistryInfoAttribute memberRegistryInfoAttribute = memberInfo.GetCustomAttribute <ChoMemberRegistryInfoAttribute>();
                string memberName = ChoType.GetMemberName(memberInfo);

                RegistryValueKindDict.Add(memberName, memberRegistryInfoAttribute.RegistryValueKind);
                RegistryValueKindDictEx.Add(memberInfo.Name, memberRegistryInfoAttribute.RegistryValueKind);
                RegistryKeyMap.Add(memberInfo.Name, memberName);
            }
        }
        public static void Load(object target, string[] commandLineArgs)
        {
            ChoGuard.ArgumentNotNull(target, "Target");

            ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject;

            Exception exception = null;

            if (commandLineArgObject != null)
            {
                commandLineArgObject.CommandLineArgs = commandLineArgs;
            }

            using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser(commandLineArgs))
            {
                commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) =>
                {
                    if (commandLineArgObject != null)
                    {
                        commandLineArgObject.OnUnrecognizedCommandLineArgFound(eventArgs);
                    }
                });

                commandLineArgParser.Parse();

                if (commandLineArgParser.IsUsageArgSpecified)
                {
                    throw new ChoCommandLineArgUsageException(GetUsage(target));
                }

                string cmdLineSwitch = null;
                if (commandLineArgObject == null || !commandLineArgObject.OnBeforeCommandLineArgObjectLoaded(commandLineArgs))
                {
                    MemberInfo[] memberInfos  = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute));
                    MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoDefaultCommandLineArgAttribute));

                    ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me;
                    memberInfos = memberInfos.Concat(memberInfos1).ToArray();
                    if (memberInfos != null && memberInfos.Length > 0)
                    {
                        foreach (MemberInfo memberInfo in memberInfos)
                        {
                            ChoCommandLineArgAttribute commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                            if (commandLineArgumentAttribute == null)
                            {
                                continue;
                            }

                            cmdLineSwitch = commandLineArgumentAttribute.CommandLineSwitch;
                            bool isSwitchSpecified = IsSwitchSpecified(commandLineArgParser.Switches, cmdLineSwitch, commandLineParserSettings.IgnoreCase);

                            exception = ExtractNPopulateValue(target, memberInfo, cmdLineSwitch, commandLineArgParser, commandLineArgObject, isSwitchSpecified);

                            if (exception != null)
                            {
                                break;
                            }
                        }

                        if (exception == null)
                        {
                            cmdLineSwitch = DefaultCmdLineSwitch;
                            MemberInfo defaultMemberInfo = GetMemberForDefaultSwitch(memberInfos1);
                            exception = ExtractNPopulateValue(target, defaultMemberInfo, DefaultCmdLineSwitch, commandLineArgParser, commandLineArgObject, false);
                        }
                    }
                }

                if (commandLineArgObject != null)
                {
                    if (exception != null)
                    {
                        if (!commandLineArgObject.OnCommandLineArgObjectLoadError(commandLineArgs, exception))
                        {
                            throw new ChoCommandLineArgException("Found exception while loading `{3}` command line argument. {0}{0}{2}{0}{0}{1}".FormatString(
                                                                     Environment.NewLine, GetUsage(target), exception.Message, cmdLineSwitch), exception);
                        }
                    }
                    else
                    {
                        commandLineArgObject.OnAfterCommandLineArgObjectLoaded(commandLineArgs);
                    }
                }
            }
        }
        public static void Load(object target, string[] commandLineArgs)
        {
            ChoGuard.ArgumentNotNull(target, "Target");

            ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject;

            if (commandLineArgObject == null)
            {
                throw new ChoApplicationException("Target is not ChoCommandLineArgObject.");
            }

            ChoCommandLineArgObjectAttribute commandLineArgumentsObjectAttribute = commandLineArgObject.GetType().GetCustomAttribute(typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute;

            if (commandLineArgumentsObjectAttribute == null)
            {
                throw new ChoApplicationException("Missing ChoCommandLineArgObjectAttribute. Must be specified.");
            }

            ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me;

            bool isUsageAvail = !commandLineArgumentsObjectAttribute.Silent;

            bool showUsageIfEmpty = commandLineArgumentsObjectAttribute.GetShowUsageIfEmpty();

            if (commandLineArgs.IsNullOrEmpty() && showUsageIfEmpty)
            {
                if (isUsageAvail)
                {
                    throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage());
                }
            }

            Exception exception = null;

            using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser())
            {
                commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) =>
                {
                    if (commandLineArgObject != null)
                    {
                        commandLineArgObject.RaiseUnrecognizedCommandLineArgFound(eventArgs);
                    }
                });

                commandLineArgParser.Parse(commandLineArgs);

                if (commandLineArgObject != null)
                {
                    commandLineArgObject.CommandLineArgs = commandLineArgs;
                }

                if ((commandLineArgParser.IsUsageArgSpecified && commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace()) ||
                    (!commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace() && HasUsageSwitchSpecified(commandLineArgParser, commandLineArgumentsObjectAttribute))
                    )
                {
                    if (isUsageAvail)
                    {
                        throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage());
                    }
                }

                string cmdLineSwitch = null;
                if (commandLineArgObject == null || !commandLineArgObject.RaiseBeforeCommandLineArgObjectLoaded(commandLineArgs))
                {
                    MemberInfo[] memberInfos  = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute));
                    MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoPositionalCommandLineArgAttribute));

                    memberInfos = memberInfos1.Concat(memberInfos).ToArray();
                    if (memberInfos != null && memberInfos.Length > 0)
                    {
                        foreach (MemberInfo memberInfo in memberInfos)
                        {
                            cmdLineSwitch = GetCmdLineSwitch(memberInfo);
                            exception     = ExtractNPopulateValue(commandLineArgObject, memberInfo, commandLineArgParser);

                            if (isUsageAvail)
                            {
                                if (exception != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                if (commandLineArgObject != null)
                {
                    if (exception != null)
                    {
                        if (!commandLineArgObject.RaiseCommandLineArgObjectLoadError(commandLineArgs, exception))
                        {
                            if (isUsageAvail)
                            {
                                if (exception is ChoCommandLineArgException)
                                {
                                    throw exception;
                                }
                                else
                                {
                                    throw new ChoCommandLineArgException("Found exception while loading `{2}` command line argument. {0}{0}{1}".FormatString(
                                                                             Environment.NewLine, exception.Message, cmdLineSwitch), commandLineArgObject.GetUsage(), exception);
                                }
                            }
                        }
                    }
                    else
                    {
                        commandLineArgObject.RaiseAfterCommandLineArgObjectLoaded(commandLineArgs);
                    }
                }
            }
        }