Beispiel #1
0
        public static object[] GetTypeConverters(PropertyDescriptor pd)
        {
            if (pd == null)
            {
                return(null);
            }

            int index = 0;
            SortedList <int, object> queue       = new SortedList <int, object>();
            SortedList <int, object> paramsQueue = new SortedList <int, object>();

            foreach (Attribute attribute in GetPropetyAttributes <ChoTypeConverterAttribute>(pd))
            {
                ChoTypeConverterAttribute converterAttribute = (ChoTypeConverterAttribute)attribute;
                if (converterAttribute != null)
                {
                    if (converterAttribute.PriorityInternal == null)
                    {
                        queue.Add(index, converterAttribute.CreateInstance());
                        paramsQueue.Add(index, converterAttribute.ParametersArray);
                        index++;
                    }
                    else
                    {
                        queue.Add(converterAttribute.PriorityInternal.Value, converterAttribute.CreateInstance());
                        paramsQueue.Add(converterAttribute.PriorityInternal.Value, converterAttribute.ParametersArray);
                    }
                }
            }

            if (queue.Count == 0)
            {
                return(GetTypeConvertersForType(pd.PropertyType));
            }

            if (queue.Count > 0)
            {
                return(queue.Values.ToArray());
            }
            else
            {
                return(null);
            }
        }
        public static object[] GetTypeConverters(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                return(null);
            }

            Type memberType;

            if (!ChoType.TryGetMemberType(memberInfo, out memberType) || (memberType == null /*|| memberType.IsSimple() */))
            {
                return(null);
            }

            if (_typeMemberTypeConverterCache.ContainsKey(memberInfo))
            {
                if (_typeMemberTypeConverterCache[memberInfo] == EmptyTypeConverters)
                {
                    if (_typeTypeConverterCache.ContainsKey(memberType))
                    {
                        return(_typeTypeConverterCache[memberType]);
                    }
                }

                return(_typeMemberTypeConverterCache[memberInfo]);
            }
            else
            {
                lock (_typeMemberTypeConverterCacheLockObject)
                {
                    if (!_typeMemberTypeConverterCache.ContainsKey(memberInfo))
                    {
                        Type typeConverterAttribute = typeof(ChoTypeConverterAttribute);

                        _typeMemberTypeConverterCache[memberInfo]       = EmptyTypeConverters;
                        _typeMemberTypeConverterParamsCache[memberInfo] = EmptyParams;

                        SortedList <int, object>   queue       = new SortedList <int, object>();
                        SortedList <int, object[]> paramsQueue = new SortedList <int, object[]>();
                        foreach (Attribute attribute in ChoType.GetMemberAttributesByBaseType(memberInfo, typeof(ChoTypeConverterAttribute)))
                        {
                            ChoTypeConverterAttribute converterAttribute = (ChoTypeConverterAttribute)attribute;
                            if (converterAttribute != null)
                            {
                                queue.Add(converterAttribute.Priority, converterAttribute.CreateInstance());
                                paramsQueue.Add(converterAttribute.Priority, converterAttribute.Parameters);
                            }

                            if (queue.Count > 0)
                            {
                                _typeMemberTypeConverterCache[memberInfo]       = queue.Values.ToArray();
                                _typeMemberTypeConverterParamsCache[memberInfo] = paramsQueue.Values.ToArray();

                                return(_typeMemberTypeConverterCache[memberInfo]);
                            }
                        }

                        if (queue.Count == 0 && !memberType.IsSimple())
                        {
                            if (!_typeTypeConverterCache.ContainsKey(memberType))
                            {
                                ChoTypeConverterAttribute converterAttribute = memberType.GetCustomAttribute <ChoTypeConverterAttribute>();
                                if (converterAttribute != null /*&& converterAttribute.ConverterType == memberType*/)
                                {
                                    _typeTypeConverterCache.Add(memberType, new object[] { converterAttribute.CreateInstance() });
                                    _typeTypeConverterParamsCache.Add(memberType, new object[] { converterAttribute.Parameters });

                                    return(_typeTypeConverterCache[memberType]);
                                }
                                //}

                                TypeConverter converter = TypeDescriptor.GetConverter(memberType);
                                if (converter != null)
                                {
                                    _typeTypeConverterCache.Add(memberType, new object[] { converter });
                                }
                                else
                                {
                                    _typeTypeConverterCache.Add(memberType, EmptyTypeConverters);
                                }

                                _typeTypeConverterParamsCache.Add(memberType, EmptyParams);
                            }

                            return(_typeTypeConverterCache[memberType]);
                        }
                    }

                    return(_typeMemberTypeConverterCache.ContainsKey(memberInfo) ? _typeMemberTypeConverterCache[memberInfo] : EmptyTypeConverters);
                }
            }
        }
Beispiel #3
0
        public static object[] GetTypeConverters(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                return(null);
            }

            string mn = memberInfo.Name;

            object[] tcs = null;
            lock (_typeMemberTypeConverterCacheLockObject)
            {
                if (_typeMemberTypeConverterCache.TryGetValue(memberInfo, out tcs))
                {
                    if (tcs == EmptyTypeConverters)
                    {
                        Type memberType;
                        if (!ChoType.TryGetMemberType(memberInfo, out memberType) || (memberType == null /*|| memberType.IsSimple() */))
                        {
                            return(null);
                        }
                        _typeTypeConverterCache.TryGetValue(memberType, out tcs);
                        return(tcs);
                    }

                    return(_typeMemberTypeConverterCache[memberInfo]);
                }
                else
                {
                    if (!_typeMemberTypeConverterCache.ContainsKey(memberInfo))
                    {
                        Type typeConverterAttribute = typeof(ChoTypeConverterAttribute);

                        _typeMemberTypeConverterCache[memberInfo]       = EmptyTypeConverters;
                        _typeMemberTypeConverterParamsCache[memberInfo] = EmptyParams;

                        int index = 0;
                        SortedList <int, object> queue       = new SortedList <int, object>();
                        SortedList <int, object> paramsQueue = new SortedList <int, object>();
                        foreach (Attribute attribute in GetPropetyAttributes <ChoTypeConverterAttribute>(memberInfo.ReflectedType, memberInfo.Name))  //ChoType.GetMemberAttributesByBaseType(memberInfo, typeof(ChoTypeConverterAttribute)))
                        {
                            ChoTypeConverterAttribute converterAttribute = (ChoTypeConverterAttribute)attribute;
                            if (converterAttribute != null)
                            {
                                if (converterAttribute.PriorityInternal == null)
                                {
                                    queue.Add(index, converterAttribute.CreateInstance());
                                    paramsQueue.Add(index, converterAttribute.ParametersArray);
                                    index++;
                                }
                                else
                                {
                                    queue.Add(converterAttribute.PriorityInternal.Value, converterAttribute.CreateInstance());
                                    paramsQueue.Add(converterAttribute.PriorityInternal.Value, converterAttribute.ParametersArray);
                                }
                            }
                        }

                        if (queue.Count == 0)
                        {
                            Type type = ChoType.GetMemberType(memberInfo);
                            if (ChoTypeConverter.Global.Contains(type))
                            {
                                _typeMemberTypeConverterCache[memberInfo] = (from a1 in ChoTypeConverter.Global.GetAll()
                                                                             where a1.Key == type
                                                                             select a1.Value).ToArray();
                            }
                        }

                        if (queue.Count > 0)
                        {
                            _typeMemberTypeConverterCache[memberInfo]       = queue.Values.ToArray();
                            _typeMemberTypeConverterParamsCache[memberInfo] = paramsQueue.Values.ToArray();

                            return(_typeMemberTypeConverterCache[memberInfo]);
                        }

                        //if (queue.Count == 0 && !memberType.IsSimple())
                        //{
                        //    if (!_typeTypeConverterCache.ContainsKey(memberType))
                        //    {
                        //        Type[] types = ChoType.GetTypes(typeof(ChoTypeConverterAttribute)).Where(t => t.GetCustomAttribute<ChoTypeConverterAttribute>().ConverterType == memberType).ToArray();

                        //        if (types != null)
                        //        {
                        //            int index1 = 0;
                        //            SortedList<int, object> queue1 = new SortedList<int, object>();
                        //            SortedList<int, object[]> paramsQueue1 = new SortedList<int, object[]>();

                        //            foreach (Type t in types)
                        //            {
                        //                queue.Add(index1, Activator.CreateInstance(t));
                        //                index1++;
                        //            }
                        //            _typeTypeConverterCache.Add(memberType, queue.Values.ToArray());
                        //            return _typeTypeConverterCache[memberType];
                        //        }

                        //        TypeConverter converter = TypeDescriptor.GetConverter(memberType);
                        //        if (converter != null)
                        //            _typeTypeConverterCache.Add(memberType, new object[] { converter });
                        //        else
                        //            _typeTypeConverterCache.Add(memberType, EmptyTypeConverters);

                        //        _typeTypeConverterParamsCache.Add(memberType, EmptyParams);
                        //    }

                        //    return _typeTypeConverterCache[memberType];
                        //}
                    }

                    return(_typeMemberTypeConverterCache.ContainsKey(memberInfo) ? _typeMemberTypeConverterCache[memberInfo] : EmptyTypeConverters);
                }
            }
        }