Пример #1
0
            public override bool Equals(object obj)
            {
                ConverterKey other = obj as ConverterKey;

                if (other == null)
                {
                    return(false);
                }
                return(other.ConverterType == ConverterType && other.ForType == ForType);
            }
        /// <summary>
        /// Gets an instance of specified converter type initialized for given type.
        /// </summary>
        /// <param name="converterType">Type of converter.</param>
        /// <param name="argumentType">Type of objects that will be converted.</param>
        /// <returns>Returns the converter of given type, that is ready to work
        /// with object of <paramref name="argumentType"/> type.</returns>
        /// <remarks>
        /// Converter type must implement <see cref="IConverter"/> interface and has
        /// public constructor with argument of <see cref="System.Type"/> type.
        /// The factory caches creates convertes so several trheads could get the same
        /// converter object simultaneously.
        /// </remarks>
        public IConverter GetConverter(Type converterType, Type argumentType)
        {
            var converterKey = new ConverterKey(converterType, argumentType);

            var converter = (IConverter)_cache[converterKey];

            if (converter == null)
            {
                converter = CreateConverter(converterType, argumentType);
                converter = StoreConverter(converterKey, converter);
            }
            return(converter);
        }
Пример #3
0
        /// <summary>
        /// Gets an instance of specified converter type initialized for given type.
        /// </summary>
        /// <param name="converterType">Type of converter.</param>
        /// <param name="argumentType">Type of objects that will be converted.</param>
        /// <returns>Returns the converter of given type, that is ready to work
        /// with object of <paramref name="argumentType"/> type.</returns>
        /// <remarks>
        /// Converter type must implement <see cref="IConverter"/> interface and has
        /// public constructor with argument of <see cref="System.Type"/> type.
        /// The factory caches creates convertes so several trheads could get the same
        /// converter object simultaneously.
        /// </remarks>
        public IConverter GetConverter(Type converterType, Type argumentType)
        {
            ConverterKey converterKey = new ConverterKey(converterType, argumentType);

            IConverter c = (IConverter)_cache[converterKey];

            if (c == null)
            {
                c = CreateConverter(converterType, argumentType);
                c = StoreConverter(converterKey, c);
            }
            return(c);
        }
Пример #4
0
        public static Func <TypeConverter> GetConverterCreatorFor(ICustomAttributeProvider info, Type target_type)
        {
            Func <TypeConverter> creator;
            var converterKey = new ConverterKey {
                Info = info, Type = target_type
            };

            if (cachedConverters.TryGetValue(converterKey, out creator))
            {
                return(creator);
            }

            TypeConverter converter = null;

            Attribute[]            attrs;
            TypeConverterAttribute at = null;
            Type t = null;

            // first check for a TypeConverter attribute on the property
            if (info != null)
            {
                try {
                    attrs = (Attribute[])info.GetCustomAttributes(true);
                    foreach (Attribute attr in attrs)
                    {
                        if (attr is TypeConverterAttribute)
                        {
                            at = (TypeConverterAttribute)attr;
                            break;
                        }
                    }
                } catch (Exception) {
                }
            }

            if (at == null)
            {
                // we didn't find one on the property.
                // check for one on the Type.
                try {
                    attrs = (Attribute[])target_type.GetCustomAttributes(true);
                    foreach (Attribute attr in attrs)
                    {
                        if (attr is TypeConverterAttribute)
                        {
                            at = (TypeConverterAttribute)attr;
                            break;
                        }
                    }
                } catch (Exception) {
                }
            }

            if (at == null)
            {
                if (target_type == typeof(bool?))
                {
                    t = typeof(NullableBoolConverter);
                }
                else
                {
                    cachedConverters.Add(converterKey, null);
                    return(null);
                }
            }
            else
            {
                t = Type.GetType(at.ConverterTypeName);
            }

            if (t == null || !typeof(TypeConverter).IsAssignableFrom(t))
            {
                cachedConverters.Add(converterKey, null);
                return(null);
            }

            ConstructorInfo ci = t.GetConstructor(new Type[] { typeof(Type) });

            if (ci != null)
            {
                creator = System.Linq.Expressions.Expression.Lambda <Func <TypeConverter> > (
                    System.Linq.Expressions.Expression.New(ci,
                                                           System.Linq.Expressions.Expression.Constant(target_type))).Compile();
            }
            else
            {
                creator = System.Linq.Expressions.Expression.Lambda <Func <TypeConverter> > (
                    System.Linq.Expressions.Expression.New(t)).Compile();
            }

            cachedConverters.Add(converterKey, creator);
            return(creator);
        }
Пример #5
0
		/// <summary>
		/// Gets an instance of specified converter type initialized for given type.
		/// </summary>
		/// <param name="converterType">Type of converter.</param>
		/// <param name="argumentType">Type of objects that will be converted.</param>
		/// <returns>Returns the converter of given type, that is ready to work
		/// with object of <paramref name="argumentType"/> type.</returns>
		/// <remarks>
		/// Converter type must implement <see cref="IConverter"/> interface and has
		/// public constructor with argument of <see cref="System.Type"/> type.
		/// The factory caches creates convertes so several trheads could get the same
		/// converter object simultaneously.
		/// </remarks>
		public IConverter GetConverter( Type converterType, Type argumentType )
		{
			ConverterKey converterKey = new ConverterKey( converterType,  argumentType );

			IConverter c = (IConverter)_cache[converterKey];
			if( c == null ) 
			{
				c = CreateConverter( converterType, argumentType );
				c = StoreConverter( converterKey, c );
			}
			return c;
		}
Пример #6
0
		public static Func<TypeConverter> GetConverterCreatorFor (ICustomAttributeProvider info, Type target_type)
		{
			Func<TypeConverter> creator;
			var converterKey = new ConverterKey { Info = info, Type = target_type };
			if (cachedConverters.TryGetValue (converterKey, out creator))
				return creator;

			TypeConverter converter = null;
			Attribute[] attrs;
			TypeConverterAttribute at = null;
			Type t = null;

			// first check for a TypeConverter attribute on the property
			if (info != null) {
				try {
					attrs = (Attribute[])info.GetCustomAttributes (true);
					foreach (Attribute attr in attrs) {
						if (attr is TypeConverterAttribute) {
							at = (TypeConverterAttribute)attr;
							break;
						}
					}
				} catch (Exception) {
				}
			}

			if (at == null) {
				// we didn't find one on the property.
				// check for one on the Type.
				try {
					attrs = (Attribute[])target_type.GetCustomAttributes (true);
					foreach (Attribute attr in attrs) {
						if (attr is TypeConverterAttribute) {
							at = (TypeConverterAttribute)attr;
							break;
						}
					}
				} catch (Exception) {
				}
			}

			if (at == null) {
				if (target_type == typeof (bool?)) {
					t = typeof (NullableBoolConverter);
				} else {
					cachedConverters.Add (converterKey, null);
					return null;
				}
			} else {
				t = Type.GetType (at.ConverterTypeName);
			}

			if (t == null || !typeof (TypeConverter).IsAssignableFrom (t)) {
				cachedConverters.Add (converterKey, null);
				return null;
			}
			
			ConstructorInfo ci = t.GetConstructor (new Type[] { typeof(Type) });
			
			if (ci != null) {
				creator = System.Linq.Expressions.Expression.Lambda<Func<TypeConverter>> (
					System.Linq.Expressions.Expression.New (ci,
					System.Linq.Expressions.Expression.Constant (target_type))).Compile ();
			} else
				creator = System.Linq.Expressions.Expression.Lambda<Func<TypeConverter>> (
					System.Linq.Expressions.Expression.New (t)).Compile ();
			
			cachedConverters.Add (converterKey, creator);
			return creator;
		}