Пример #1
0
        public IDictionary <string, object> GetDefaults()
        {
            IDictionary <string, object> dict = new Dictionary <string, object>();

            ChoPropertyAttribute attr = null;
            object memberValue        = null;
            string propName           = null;

            //scan through members and load them
            foreach (var prop in ChoType.GetMembers(GetType()).Where(m => m.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null && !ChoType.IsReadOnlyMember(m)))
            {
                attr = ChoType.GetMemberAttribute <ChoPropertyAttribute>(prop);
                try
                {
                    propName    = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name;
                    memberValue = ChoType.GetDefaultValue(ChoType.GetMemberInfo(GetType(), prop.Name));

                    if (memberValue != null && memberValue is string)
                    {
                        string mv = memberValue as string;
                        if (attr != null)
                        {
                            switch (attr.TrimOption)
                            {
                            case ChoPropertyValueTrimOption.Trim:
                                mv = mv.Trim();
                                break;

                            case ChoPropertyValueTrimOption.TrimEnd:
                                mv = mv.TrimEnd();
                                break;

                            case ChoPropertyValueTrimOption.TrimStart:
                                mv = mv.TrimStart();
                                break;
                            }
                        }

                        memberValue = mv;
                    }

                    if (!dict.ContainsKey(propName))
                    {
                        dict.Add(propName, memberValue);
                    }
                }
                catch (Exception ex)
                {
                    //ChoLog.Error("{0}: Error getting default value for '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message));
                    SetDefaultValue(prop, false);
                }
            }

            return(dict);
        }
Пример #2
0
        private void Initialize()
        {
            try
            {
                if (!Monitor.TryEnter(_padLock, 1 * 1000))
                {
                    return;
                }

                IDictionary <string, object> kvpDict = null;

                if (_func != null)
                {
                    kvpDict = _func();
                }
                else
                {
                    kvpDict = Seed();
                }

                if (kvpDict == null)
                {
                    return;
                }

                IDictionary <string, object> mkvpDict = _kvpDict;
                bool hasDiff = mkvpDict == null || kvpDict.Except(mkvpDict).Concat(mkvpDict.Except(kvpDict)).Any();
                if (!hasDiff)
                {
                    return;
                }

                _kvpDict = kvpDict;

                ChoPropertyAttribute attr = null;
                object memberValue        = null;
                string propName           = null;
                //scan through members and load them
                foreach (var prop in ChoType.GetMembers(GetType()).Where(m => m.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null && !ChoType.IsReadOnlyMember(m)))
                {
                    attr = ChoType.GetMemberAttribute <ChoPropertyAttribute>(prop);
                    try
                    {
                        SetDefaultValue(prop, true);

                        propName = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name;

                        if (kvpDict.ContainsKey(propName))
                        {
                            memberValue = AfterKVPLoaded(prop.Name, kvpDict[propName]);
                        }
                        else
                        {
                            memberValue = AfterKVPLoaded(prop.Name, null);
                        }

                        if (memberValue != null && memberValue is string)
                        {
                            string mv = memberValue as string;
                            if (attr != null)
                            {
                                switch (attr.TrimOption)
                                {
                                case ChoPropertyValueTrimOption.Trim:
                                    mv = mv.Trim();
                                    break;

                                case ChoPropertyValueTrimOption.TrimEnd:
                                    mv = mv.TrimEnd();
                                    break;

                                case ChoPropertyValueTrimOption.TrimStart:
                                    mv = mv.TrimStart();
                                    break;
                                }
                            }

                            memberValue = mv;
                        }

                        if (ChoType.GetMemberType(prop) == typeof(string))
                        {
                            if (!((string)memberValue).IsNullOrEmpty())
                            {
                                ChoType.ConvertNSetMemberValue(this, prop, memberValue);
                            }
                        }
                        else
                        {
                            if (memberValue != null)
                            {
                                ChoType.ConvertNSetMemberValue(this, prop, memberValue);
                            }
                        }
                        ChoValidator.ValidateFor(this, prop);
                    }
                    catch (Exception ex)
                    {
                        //ChoLog.Error("{0}: Error loading '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message));
                        SetDefaultValue(prop, false);
                    }
                }
            }
            catch (Exception outerEx)
            {
                //ChoLog.Error("{0}: Error loading options. {1}".FormatString(NName, outerEx.Message));
            }
            finally
            {
                Monitor.Exit(_padLock);
            }
        }