Exemple #1
0
        internal static IChoProfile GetContext(StackFrame frame)
        {
            string     name   = null;
            MemberInfo method = frame.GetMethod();

            if (method != null)
            {
                name = method.Name;
                ChoProfileAttribute profileAttribute = ChoType.GetMemberAttributeByBaseType <ChoProfileAttribute>(method);
                if (profileAttribute == null)
                {
                    var type = method.DeclaringType;
                    name             = type.Name;
                    profileAttribute = ChoType.GetAttribute(type, typeof(ChoProfileAttribute)) as ChoProfileAttribute;
                }
                if (profileAttribute != null)
                {
                    if (_profileAttribute == profileAttribute)
                    {
                        return(_contextProfile);
                    }
                    else
                    {
                        _profileAttribute = profileAttribute;
                    }

                    if (profileAttribute.Name.IsNullOrWhiteSpace())
                    {
                        profileAttribute.Name = name;
                    }

                    if (profileAttribute.OuterProfileName.IsNullOrWhiteSpace())
                    {
                        _contextProfile = profileAttribute.ConstructProfile(null, null);
                    }
                    else
                    {
                        _contextProfile = profileAttribute.ConstructProfile(null, GetProfile(profileAttribute.OuterProfileName));
                    }
                }
            }

            return(_contextProfile);
        }
Exemple #2
0
        public static bool IsValid(this object @this, out Exception aggEx)
        {
            aggEx = null;

            if (@this == null)
            {
                return(true);
            }

            var results = new List <ValidationResult>();

            if (@this is IChoValidatable)
            {
                ((IChoValidatable)@this).TryValidate(results);
            }
            else
            {
                foreach (MemberInfo mi in ChoType.GetMembers(@this.GetType()).Where(m => ChoType.GetMemberAttributeByBaseType <ChoMemberAttribute>(m) != null))
                {
                    //if (ChoObjectMemberMetaDataCache.Default.IsRequired(mi) && ChoType.GetMemberValue(@this, mi) == null)
                    //    results.Add(new ValidationResult("Null value found for {0} member.".FormatString(mi.Name)));
                }

                var context = new ValidationContext(@this, null, null);
                Validator.TryValidateObject(@this, context, results, true);
            }

            if (results.Count > 0)
            {
                aggEx = new ApplicationException("Failed to validate '{0}' object. {2}{1}".FormatString(@this.GetType().FullName, ToString(results), Environment.NewLine));
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private static ChoPropertyInfos ConstructPropertyInfos(ChoCommandLineArgObject cmdLineArgObject)
        {
            if (cmdLineArgObject != null)
            {
                Dictionary <string, ChoPropertyInfos> propDict = _propDict;
                string elementPath = cmdLineArgObject.GetType().Name;

                if (elementPath.IsNullOrWhiteSpace())
                {
                    return(ChoPropertyInfos.Default);
                }

                if (!propDict.ContainsKey(elementPath))
                {
                    lock (_padLock)
                    {
                        if (!propDict.ContainsKey(elementPath))
                        {
                            MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(cmdLineArgObject.GetType());
                            if (memberInfos != null && memberInfos.Length > 0)
                            {
                                //Set member values
                                List <ChoPropertyInfo>            propertyInfoList    = new List <ChoPropertyInfo>();
                                ChoDefaultCommandLineArgAttribute memberInfoAttribute = null;

                                foreach (MemberInfo memberInfo in memberInfos)
                                {
                                    memberInfoAttribute = (ChoDefaultCommandLineArgAttribute)ChoType.GetMemberAttributeByBaseType(memberInfo, typeof(ChoDefaultCommandLineArgAttribute));
                                    if (memberInfoAttribute == null)
                                    {
                                        continue;
                                    }

                                    ChoPropertyInfo propInfo = new ChoPropertyInfo();

                                    propInfo.Name = ChoType.GetMemberName(memberInfo);

                                    if (ChoType.GetMemberType(memberInfo) != typeof(bool))
                                    {
                                        propInfo.DefaultValue             = new ChoCDATA(memberInfoAttribute.DefaultValue);
                                        propInfo.FallbackValue            = new ChoCDATA(memberInfoAttribute.FallbackValue);
                                        propInfo.IsDefaultValueSpecified  = memberInfoAttribute.IsDefaultValueSpecified;
                                        propInfo.IsFallbackValueSpecified = memberInfoAttribute.IsFallbackValueSpecified;
                                        propertyInfoList.Add(propInfo);
                                    }
                                }

                                if (propertyInfoList != null)
                                {
                                    ChoPropertyInfos propertyInfos = new ChoPropertyInfos();
                                    propertyInfos.PropertyInfoArr = propertyInfoList.ToArray();

                                    SetMetaDataSection(cmdLineArgObject, propertyInfos);
                                    return(propertyInfos);
                                }
                            }
                        }
                    }
                }
            }

            return(ChoPropertyInfos.Default);
        }
Exemple #4
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;
                        }
                    }
                }
            }
        }