Exemplo n.º 1
0
        internal ChoMemberInfo(object target, Type type, IMethodMessage methodCallMsg)
        {
            _methodMsg    = methodCallMsg;
            _target       = target;
            MethodCallMsg = _methodMsg as MethodCallMessageWrapper;

            if (_methodMsg.MethodName == "FieldSetter" || (_methodMsg.MethodName.StartsWith("set_") &&
                                                           _methodMsg.MethodName != "set_Dirty"))
            {
                _dirtyOperation = true;
            }

            if (_methodMsg.MethodName == "FieldSetter" || _methodMsg.MethodName == "FieldGetter")
            {
                _name       = _methodMsg.Args[1] as string;
                _memberType = MemberTypes.Field;
            }
            else if (_methodMsg.MethodName.StartsWith("get_"))
            {
                _name       = _methodMsg.MethodName.Replace("get_", String.Empty);
                _memberType = MemberTypes.Property;
            }
            else if (_methodMsg.MethodName.StartsWith("set_"))
            {
                _name       = _methodMsg.MethodName.Replace("set_", String.Empty);
                _memberType = MemberTypes.Property;
            }
            else
            {
                _name = _methodMsg.MethodName;
            }

            //_memberInfo = ChoType.GetMember(_target.GetType(), Name);
            _memberInfo = ChoType.GetMemberInfo(type, Name);

            if (_dirtyOperation)
            {
                //Set Converted value
                //Value = ChoConvert.ConvertFrom(_target, Value, ChoType.GetMemberType(_memberInfo),
                //    ChoTypeDescriptor.GetTypeConverters(_memberInfo), ChoTypeDescriptor.GetTypeConverterParams(_memberInfo));
                Value = ChoConvert.ConvertFrom(Value, _memberInfo, _target);
            }
        }
Exemplo n.º 2
0
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            MemberInfo memberInfo = ChoTypeMembersCache.GetMemberInfo(_type, binder.Name);

            if (memberInfo == null)
            {
                return(false);
            }

            string propertyName = ChoType.GetMemberName(memberInfo);

            object newConvertedValue = null;

            try
            {
                newConvertedValue = ChoConvert.ConvertFrom(value, memberInfo, _instance);
                //           newConvertedValue = ChoConvert.ConvertFrom(_instance, value, ChoType.GetMemberType(memberInfo),
                //ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                ChoType.SetMemberValue(_instance, memberInfo, newConvertedValue);
            }
            catch (ChoConfigurationObjectPostInvokeException)
            {
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                if (_instance.RaiseConfigurationObjectMemberLoadError(binder.Name, propertyName, value, ex))
                {
                    _instance.OnPropertyChanged(binder.Name);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            _instance.OnPropertyChanged(binder.Name);
            return(true);
        }
        private void Reset()
        {
            if (ConfigObject == null)
            {
                return;
            }

            ChoConfigurationObjectErrorManagerService.ResetObjectErrors(ConfigObject);

            //MemberInfo[] memberInfos = ChoType.GetMembers(ConfigObject.GetType(), typeof(ChoConfigurationPropertyAttribute));
            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(ConfigObject.GetType());
            if (memberInfos != null && memberInfos.Length > 0)
            {
                ChoPropertyInfoAttribute memberInfoAttribute = null;
                foreach (MemberInfo memberInfo in memberInfos)
                {
                    memberInfoAttribute = (ChoPropertyInfoAttribute)ChoType.GetMemberAttribute(memberInfo, typeof(ChoPropertyInfoAttribute));
                    string name                    = ChoType.GetMemberName(memberInfo);
                    object defaultValue            = null;
                    bool   isDefaultValueSpecified = ChoConfigurationMetaDataManager.TryConfigDefaultValue(this, name, memberInfoAttribute, out defaultValue);
                    if (memberInfoAttribute == null || !isDefaultValueSpecified)
                    {
                        continue;
                    }

                    try
                    {
                        //object newConvertedValue = memberInfoAttribute.DefaultValue;
                        object newConvertedValue = ChoConvert.ConvertFrom(ConfigObject, defaultValue, ChoType.GetMemberType(memberInfo),
                                                                          ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        SetConfigPropertyValue(newConvertedValue, memberInfo);
                        //ChoType.SetMemberValue(ConfigObject, memberInfo, newConvertedValue);
                    }
                    catch (Exception innerEx)
                    {
                        ChoConfigurationObjectErrorManagerService.SetObjectMemberError(ConfigObject, memberInfo.Name, String.Format("Failed to assign `{0}` default value. {1}", ChoString.ToString(defaultValue), innerEx.Message));
                    }
                }
            }
        }
        private bool ExtractNPopulateValues(ref bool hasErrors, ref bool isDirty)
        {
            bool   isModfied     = false;
            object oldValue      = null;
            object newValue      = null;
            object origValue     = null;
            object defaultValue  = null;
            object fallbackValue = null;

            bool isConfigmemberDefined = false;

            //MemberInfo[] memberInfos = ChoType.GetMembers(ConfigObject.GetType(), typeof(ChoConfigurationPropertyAttribute));
            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(ConfigObject.GetType());
            if (memberInfos != null && memberInfos.Length > 0)
            {
                //Set member values
                string name;
                ChoPropertyInfoAttribute memberInfoAttribute = null;
                foreach (MemberInfo memberInfo in memberInfos)
                {
                    if (memberInfo.GetCustomAttribute <ChoIgnorePropertyAttribute>() != null)
                    {
                        continue;
                    }

                    memberInfoAttribute = (ChoPropertyInfoAttribute)ChoType.GetMemberAttribute(memberInfo, typeof(ChoPropertyInfoAttribute));

                    //if (memberInfoAttribute == null) continue;

                    oldValue      = null;
                    newValue      = null;
                    origValue     = null;
                    defaultValue  = null;
                    fallbackValue = null;

                    name = ChoType.GetMemberName(memberInfo);
                    isConfigmemberDefined = ConfigSection.HasConfigMemberDefined(name);

                    oldValue = ChoType.GetMemberValue(ConfigObject, memberInfo.Name);

                    object configFallbackValue     = null;
                    object configDefaultValue      = null;
                    bool   isDefaultValueSpecified = ChoConfigurationMetaDataManager.TryConfigDefaultValue(this, name, memberInfoAttribute, out configDefaultValue);
                    ChoConfigurationMetaDataManager.TryConfigFallbackValue(this, name, memberInfoAttribute, out configFallbackValue);

                    if (configFallbackValue == null)
                    {
                        if (isDefaultValueSpecified)
                        {
                            defaultValue = origValue = newValue = configDefaultValue;
                        }
                    }
                    else
                    {
                        fallbackValue = origValue = newValue = configFallbackValue;
                    }

                    if (!isConfigmemberDefined)
                    {
                        if (!isDirty)
                        {
                            isDirty = memberInfoAttribute != null && memberInfoAttribute.Persistable ? true : false;
                        }
                    }
                    else
                    {
                        origValue = newValue = ConfigSection[name];
                    }

                    try
                    {
                        object newConvertedValue = ChoConvert.ConvertFrom(ConfigObject, newValue, ChoType.GetMemberType(memberInfo),
                                                                          ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        newValue = newConvertedValue;
                    }
                    catch { }

                    ChoConfigurableObject configObject = ConfigObject as ChoConfigurableObject;
                    if (configObject != null)
                    {
                        if (!configObject.IsMemeberValueEqualInternal(memberInfo, oldValue, newValue))
                        {
                            if (!configObject.RaiseBeforeConfigurationObjectMemberLoaded(memberInfo.Name, name, origValue, ref newValue))
                            {
                                try
                                {
                                    //ChoType.SetMemberValue(ConfigObject, memberInfo.Name, newValue != null ? ChoConvert.ConvertFrom(ConfigObject, newValue,
                                    //    ChoType.GetMemberType(memberInfo), ChoTypeConvertersCache.GetTypeConverters(memberInfo)) : null);
                                    SetConfigPropertyValue(newValue, memberInfo);
                                    if (!_firstTime)
                                    {
                                        isModfied = true;
                                    }

                                    configObject.RaiseAfterConfigurationObjectMemberLoaded(memberInfo.Name, name, newValue);
                                    ChoConfigurationObjectErrorManagerService.ResetObjectMemberError(ConfigObject, memberInfo.Name);
                                }
                                catch (Exception innerEx)
                                {
                                    if (!configObject.RaiseConfigurationObjectMemberLoadError(memberInfo.Name, name, origValue, innerEx))
                                    {
                                        if (Silent)
                                        {
                                            if (_firstTime)
                                            {
                                                AssignToFallbackOrDefaultValue(defaultValue, fallbackValue, memberInfo);
                                            }

                                            ChoConfigurationObjectErrorManagerService.SetObjectMemberError(ConfigObject, memberInfo.Name, String.Format(Resources.ConfigConstructMsg, ChoString.ToString(origValue), innerEx.Message));
                                        }
                                        else
                                        {
                                            throw new ChoConfigurationConstructionException(String.Format(Resources.ConfigConstructExceptionMsg, ChoString.ToString(origValue), ConfigObject.GetType().FullName,
                                                                                                          memberInfo.Name), innerEx);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            //ChoType.SetMemberValue(ConfigObject, memberInfo.Name, newValue != null ? ChoConvert.ConvertFrom(ConfigObject, newValue,
                            //    ChoType.GetMemberType(memberInfo), ChoTypeConvertersCache.GetTypeConverters(memberInfo)) : null);
                            SetConfigPropertyValue(newValue, memberInfo);
                            if (!_firstTime)
                            {
                                isModfied = true;
                            }
                            ChoConfigurationObjectErrorManagerService.ResetObjectMemberError(ConfigObject, memberInfo.Name);
                        }
                        catch (Exception innerEx)
                        {
                            if (Silent)
                            {
                                if (_firstTime)
                                {
                                    AssignToFallbackOrDefaultValue(defaultValue, fallbackValue, memberInfo);
                                }
                                ChoConfigurationObjectErrorManagerService.SetObjectMemberError(ConfigObject, memberInfo.Name, String.Format(Resources.ConfigConstructMsg, ChoString.ToString(origValue), innerEx.Message));
                            }
                            else
                            {
                                throw new ChoConfigurationConstructionException(String.Format(Resources.ConfigConstructExceptionMsg, ChoString.ToString(origValue), ConfigObject.GetType().FullName,
                                                                                              memberInfo.Name), innerEx);
                            }
                        }
                    }
                }
            }

            return(isModfied);
        }
Exemplo n.º 5
0
 public static object GetConvertedValue(this MemberInfo memberInfo, object value)
 {
     return(ChoConvert.ConvertFrom(null, value, ChoType.GetMemberType(memberInfo),
                                   ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo)));
 }
        private static Exception ExtractNPopulateValue(object target, MemberInfo memberInfo, string switchString, ChoCommandLineArgParser commandLineArgParser,
                                                       ChoCommandLineArgObject commandLineArgObject, bool isSwitchSpecified)
        {
            ChoCommandLineArgAttribute        commandLineArgumentAttribute   = null;
            ChoDefaultCommandLineArgAttribute defaultCommandLineArgAttribute = null;

            if (memberInfo == null)
            {
                if (commandLineArgObject != null)
                {
                    commandLineArgObject.OnCommandLineArgMemberNotFound(switchString, commandLineArgParser[switchString]);
                }
            }
            else
            {
                //if (ChoType.IsReadOnlyMember(memberInfo))
                //    return null;

                commandLineArgumentAttribute   = null;
                defaultCommandLineArgAttribute = null;

                commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
                if (commandLineArgumentAttribute == null)
                {
                    defaultCommandLineArgAttribute = (ChoDefaultCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoDefaultCommandLineArgAttribute>(true);
                    if (defaultCommandLineArgAttribute == null)
                    {
                        return(null);
                    }
                }

                string cmdLineArgValue    = null;
                object newCmdLineArgValue = null;

                try
                {
                    if (defaultCommandLineArgAttribute != null)
                    {
                        cmdLineArgValue = commandLineArgParser.DefaultArgs.Length > 0 ? commandLineArgParser.DefaultArgs[0] : null;
                    }
                    else if (commandLineArgumentAttribute != null)
                    {
                        cmdLineArgValue = commandLineArgParser[commandLineArgumentAttribute.CommandLineSwitch];
                        defaultCommandLineArgAttribute = commandLineArgumentAttribute;
                    }
                    else
                    {
                        return(null);
                    }

                    object defaultCmdLineArgValue = defaultCommandLineArgAttribute.DefaultValue;

                    if (isSwitchSpecified && cmdLineArgValue == null && defaultCommandLineArgAttribute.FallbackValue != null)
                    {
                        defaultCmdLineArgValue = defaultCommandLineArgAttribute.FallbackValue;
                    }

                    if (commandLineArgObject == null || !commandLineArgObject.OnBeforeCommandLineArgLoaded(memberInfo.Name, cmdLineArgValue, defaultCommandLineArgAttribute.DefaultValue, defaultCommandLineArgAttribute.FallbackValue))
                    {
                        if (!cmdLineArgValue.IsNullOrWhiteSpace())
                        {
                            newCmdLineArgValue = ChoConvert.ConvertFrom(target, ChoString.ExpandPropertiesEx(cmdLineArgValue),
                                                                        ChoType.GetMemberType(memberInfo),
                                                                        ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        }
                        else if (defaultCmdLineArgValue != null)
                        {
                            if (defaultCmdLineArgValue is string)
                            {
                                newCmdLineArgValue = ChoConvert.ConvertFrom(target, ChoString.ExpandPropertiesEx(defaultCmdLineArgValue as string),
                                                                            ChoType.GetMemberType(memberInfo),
                                                                            ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                            }
                            else
                            {
                                newCmdLineArgValue = ChoConvert.ConvertFrom(target, defaultCmdLineArgValue, ChoType.GetMemberType(memberInfo),
                                                                            ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                            }
                        }

                        if (newCmdLineArgValue != null)
                        {
                            ChoType.SetMemberValue(target, memberInfo, newCmdLineArgValue);
                            if (commandLineArgObject != null)
                            {
                                commandLineArgObject.OnAfterCommandLineArgLoaded(memberInfo.Name, newCmdLineArgValue);
                            }
                        }
                        else if (defaultCommandLineArgAttribute.IsRequired)
                        {
                            throw new ChoCommandLineArgException("Missing arg value for '{0}' required command line switch.".FormatString(
                                                                     commandLineArgumentAttribute == null ? DefaultCmdLineSwitch : commandLineArgumentAttribute.CommandLineSwitch));
                        }
                    }
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (commandLineArgObject != null && commandLineArgObject.OnCommandLineArgLoadError(memberInfo.Name, cmdLineArgValue, ex))
                    {
                    }
                    else
                    {
                        return(ex);
                    }
                }
            }

            return(null);
        }
        //private static void AssignToDefaultValues(ChoCommandLineArgObject commandLineArgObject)
        //{
        //    object newCmdLineArgValue = null;

        //    string name = null;
        //    string defaultValue = null;
        //    bool isDefaultValueSpecified;
        //    MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(commandLineArgObject.GetType());
        //    if (memberInfos != null && memberInfos.Length > 0)
        //    {
        //        ChoCommandLineArgAttribute defaultCommandLineArgAttribute = null;
        //        foreach (MemberInfo memberInfo in memberInfos)
        //        {
        //            defaultCommandLineArgAttribute = (ChoCommandLineArgAttribute)ChoType.GetMemberAttribute(memberInfo, typeof(ChoCommandLineArgAttribute));
        //            if (defaultCommandLineArgAttribute == null) continue;

        //            name = ChoType.GetMemberName(memberInfo);
        //            defaultValue = null;
        //            if (ChoType.GetMemberType(memberInfo) == typeof(bool))
        //                continue;

        //            isDefaultValueSpecified = ChoCmdLineArgMetaDataManager.TryGetDefaultValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out defaultValue);
        //            if (!isDefaultValueSpecified)
        //                continue;
        //            try
        //            {
        //                defaultValue = ChoString.ExpandPropertiesEx(defaultValue);
        //                object newConvertedValue = ChoConvert.ConvertFrom(defaultValue, memberInfo, commandLineArgObject);

        //                //object newConvertedValue = ChoConvert.ConvertFrom(commandLineArgObject, defaultValue, ChoType.GetMemberType(memberInfo),
        //                //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
        //                ChoType.SetMemberValue(commandLineArgObject, memberInfo, newCmdLineArgValue);
        //            }
        //            catch //(Exception ex)
        //            {
        //            }
        //        }
        //    }
        //}

        private static Exception ExtractNPopulateValue(ChoCommandLineArgObject commandLineArgObject, MemberInfo memberInfo, ChoCommandLineArgParser commandLineArgParser)
        {
            ChoDefaultCommandLineArgAttribute    defaultCommandLineArgAttribute = null;
            ChoCommandLineArgAttribute           commandLineArgumentAttribute   = null;
            ChoPositionalCommandLineArgAttribute posCommandLineArgAttribute     = null;

            if (ChoType.IsReadOnlyMember(memberInfo))
            {
                return(null);
            }

            commandLineArgumentAttribute = null;
            posCommandLineArgAttribute   = null;

            defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true);
            if (commandLineArgumentAttribute == null)
            {
                defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true);
                if (posCommandLineArgAttribute == null)
                {
                    return(null);
                }
            }

            bool   containsCmdLineArg       = false;
            string cmdLineArgValue          = null;
            object newCmdLineArgValue       = null;
            string defaultValue             = null;
            bool   isDefaultValueSpecified  = false;
            bool   isFallbackValueSpecified = false;
            string name             = null;
            string fallbackValue    = null;
            object fallbackValueObj = null;
            object defaultValueObj  = null;

            name = ChoType.GetMemberName(memberInfo);

            try
            {
                if (posCommandLineArgAttribute != null)
                {
                    if (!commandLineArgParser.IsSwitchSpecified(posCommandLineArgAttribute.Position))
                    {
                        commandLineArgObject.RaiseCommandLineArgNotFound(posCommandLineArgAttribute.Position.ToString(), ref cmdLineArgValue);
                    }
                    cmdLineArgValue = commandLineArgParser[posCommandLineArgAttribute.Position];
                }
                else if (commandLineArgumentAttribute != null)
                {
                    if (!commandLineArgParser.IsSwitchSpecified(commandLineArgumentAttribute.CommandLineSwitch))
                    {
                        commandLineArgObject.RaiseCommandLineArgNotFound(commandLineArgumentAttribute.CommandLineSwitch, ref cmdLineArgValue);
                    }

                    if (ChoType.GetMemberType(memberInfo) == typeof(bool))
                    {
                        containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                        if (containsCmdLineArg)
                        {
                            cmdLineArgValue = "True";
                            //cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                            //if (cmdLineArgValue.IsNullOrWhiteSpace())
                            //    cmdLineArgValue = "True";
                        }
                        else
                        {
                            containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, "{0}-".FormatString(commandLineArgumentAttribute.CommandLineSwitch), commandLineArgumentAttribute.Aliases);
                            if (containsCmdLineArg)
                            {
                                cmdLineArgValue = "False";
                            }
                        }
                    }
                    //else if (ChoType.GetMemberType(memberInfo).IsEnum)
                    //{
                    //    containsCmdLineArg = IsSwitchSpecified(commandLineArgParser, Enum.GetNames(ChoType.GetMemberType(memberInfo)));
                    //    if (containsCmdLineArg)
                    //        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, Enum.GetNames(ChoType.GetMemberType(memberInfo)));
                    //    else
                    //        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                    //}
                    else
                    {
                        cmdLineArgValue = GetCmdLineArgValue(commandLineArgParser, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Aliases);
                    }
                }
                else
                {
                    return(null);
                }

                //if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                //{
                isDefaultValueSpecified  = ChoCmdLineArgMetaDataManager.TryGetDefaultValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out defaultValue);
                isFallbackValueSpecified = ChoCmdLineArgMetaDataManager.TryGetFallbackValue(commandLineArgObject, name, defaultCommandLineArgAttribute, out fallbackValue);
                //}

                try
                {
                    if (isFallbackValueSpecified)
                    {
                        //                    fallbackValueObj = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(fallbackValue),
                        //ChoType.GetMemberType(memberInfo),
                        //ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));

                        fallbackValueObj = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(fallbackValue), memberInfo, commandLineArgObject);
                    }
                }
                catch
                {
                }

                try
                {
                    if (isDefaultValueSpecified)
                    {
                        //defaultValueObj = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(defaultValue), ChoType.GetMemberType(memberInfo),
                        //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                        defaultValueObj = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(defaultValue), memberInfo, commandLineArgObject);
                    }
                }
                catch
                {
                }

                if (commandLineArgObject != null && !commandLineArgObject.RaiseBeforeCommandLineArgLoaded(memberInfo.Name, ref cmdLineArgValue, defaultValueObj, fallbackValueObj))
                {
                    if (!cmdLineArgValue.IsNull())
                    {
                        newCmdLineArgValue = ChoConvert.ConvertFrom(ChoString.ExpandPropertiesEx(cmdLineArgValue), memberInfo, commandLineArgObject);

                        //newCmdLineArgValue = ChoConvert.ConvertFrom(commandLineArgObject, ChoString.ExpandPropertiesEx(cmdLineArgValue),
                        //    ChoType.GetMemberType(memberInfo),
                        //    ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));
                    }

                    if (newCmdLineArgValue == null && defaultCommandLineArgAttribute.IsRequired)
                    {
                        if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                        {
                            if (commandLineArgumentAttribute != null)
                            {
                                throw new ChoCommandLineArgException("Missing arg value for '{0}' required command line switch.".FormatString(
                                                                         commandLineArgumentAttribute == null ? ChoCommandLineArgObject.DefaultCmdLineSwitch : commandLineArgumentAttribute.CommandLineSwitch),
                                                                     commandLineArgObject.GetUsage());
                            }
                            else if (posCommandLineArgAttribute != null)
                            {
                                if (posCommandLineArgAttribute.ShortName.IsNull())
                                {
                                    throw new ChoCommandLineArgException("Missing positional arg value at '{0}' position.".FormatString(
                                                                             posCommandLineArgAttribute == null ? ChoCommandLineArgObject.DefaultCmdLineSwitch : posCommandLineArgAttribute.Position.ToString()), commandLineArgObject.GetUsage());
                                }
                                else
                                {
                                    throw new ChoCommandLineArgException("Missing '{0}' argument.".FormatString(posCommandLineArgAttribute.ShortName), commandLineArgObject.GetUsage());
                                }
                            }
                            else
                            {
                                throw new ChoCommandLineArgException("Missing arg value at '{0}' position.".FormatString(ChoCommandLineArgObject.DefaultCmdLineSwitch), commandLineArgObject.GetUsage());
                            }
                        }
                    }
                    else
                    {
                        if (newCmdLineArgValue == null)
                        {
                            if (isDefaultValueSpecified)
                            {
                                //if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                                newCmdLineArgValue = defaultValueObj;
                                //else
                                //    newCmdLineArgValue = false;
                            }
                        }

                        ChoType.SetMemberValue(commandLineArgObject, memberInfo, newCmdLineArgValue);
                        if (commandLineArgObject != null)
                        {
                            commandLineArgObject.RaiseAfterCommandLineArgLoaded(memberInfo.Name, newCmdLineArgValue);
                        }
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (commandLineArgObject != null && commandLineArgObject.RaiseCommandLineArgLoadError(memberInfo.Name, cmdLineArgValue, ex))
                {
                }
                else
                {
                    if (defaultCommandLineArgAttribute.IsRequired)
                    {
                        return(ex);
                    }

                    if (fallbackValueObj != null)
                    {
                        ChoType.SetMemberValue(commandLineArgObject, memberInfo, fallbackValueObj);
                    }
                    else
                    {
                        return(ex);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 8
0
        private static void ExtractAndPopulateValues <T>(T target, IDataReader dr, ChoDataObjectAttribute objAttr)
        {
            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(target.GetType());
            if (memberInfos != null && memberInfos.Length > 0)
            {
                string name;
                string fallbackValue = null;
                object memberValue   = null;
                object newValue      = null;
                object defaultValue  = null;
                object value         = null;

                ChoPropertyInfoAttribute memberInfoAttribute = null;

                foreach (MemberInfo memberInfo in memberInfos)
                {
                    name        = ChoType.GetMemberName(memberInfo);
                    memberValue = null;
                    newValue    = null;
                    value       = null;

                    memberInfoAttribute = (ChoPropertyInfoAttribute)ChoType.GetMemberAttributeByBaseType(memberInfo, typeof(ChoPropertyInfoAttribute));

                    try
                    {
                        if (dr.FieldExists(name))
                        {
                            value = dr[name];
                        }
                        try
                        {
                            defaultValue = memberInfo.GetDefaultValue();
                            //if (memberInfoAttribute != null)
                            //{
                            //    if (!memberInfoAttribute.DefaultValue.IsNullOrWhiteSpace())
                            //        defaultValue = ChoString.ExpandPropertiesEx(memberInfoAttribute.DefaultValue);
                            //}
                        }
                        catch
                        {
                            defaultValue = null;
                        }

                        try
                        {
                            if (memberInfoAttribute != null)
                            {
                                if (!memberInfoAttribute.FallbackValue.IsNullOrWhiteSpace())
                                {
                                    fallbackValue = ChoString.ExpandPropertiesEx(memberInfoAttribute.FallbackValue);
                                }
                            }
                        }
                        catch
                        {
                            fallbackValue = null;
                        }

                        newValue = memberValue = value;

                        if (newValue.IsNullOrDbNull())
                        {
                            if (fallbackValue == null)
                            {
                                newValue = defaultValue;
                            }
                            else
                            {
                                newValue = fallbackValue;
                            }
                        }

                        newValue = ChoConvert.ConvertFrom(newValue, memberInfo, target);
                        //                    newValue = ChoConvert.ConvertFrom(target, newValue, ChoType.GetMemberType(memberInfo),
                        //ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo));

                        if (newValue == null)
                        {
                        }
                        else
                        {
                            ChoType.SetMemberValue(target, memberInfo, newValue);
                        }
                    }
                    catch (Exception)
                    {
                        if (objAttr == null || (objAttr != null && !objAttr.Silent))
                        {
                            throw;
                        }
                    }
                }
            }
        }