예제 #1
0
        /// <summary>
        /// Uses heuristics to deduce a NHibernate type given a string naming the type.
        /// </summary>
        /// <param name="typeName">the type name</param>
        /// <param name="parameters">parameters for the type</param>
        /// <param name="length">optionally, the size of the type</param>
        /// <returns></returns>
        public static IType HeuristicType(string typeName, IDictionary <string, string> parameters, int?length)
        {
            IType type = Basic(typeName);

            if (type == null)
            {
                string[]           parsedTypeName;
                TypeClassification typeClassification = GetTypeClassification(typeName);
                if (typeClassification == TypeClassification.Length)
                {
                    parsedTypeName = typeName.Split(LengthSplit);
                }
                else
                {
                    parsedTypeName = typeClassification == TypeClassification.PrecisionScale ? typeName.Split(PrecisionScaleSplit) : new[] { typeName }
                };


                System.Type typeClass;
                try
                {
                    typeClass = ReflectHelper.ClassForName(parsedTypeName[0]);                     //typeName);
                }
                catch (Exception)
                {
                    typeClass = null;
                }

                if (typeClass != null)
                {
                    if (typeof(IType).IsAssignableFrom(typeClass))
                    {
                        try
                        {
                            type = (IType)Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(typeClass);
                        }
                        catch (Exception e)
                        {
                            throw new MappingException("Could not instantiate IType " + typeClass.Name + ": " + e, e);
                        }
                        InjectParameters(type, parameters);
                    }
                    else if (typeof(ICompositeUserType).IsAssignableFrom(typeClass))
                    {
                        type = new CompositeCustomType(typeClass, parameters);
                    }
                    else if (typeof(IUserType).IsAssignableFrom(typeClass))
                    {
                        type = new CustomType(typeClass, parameters);
                    }
                    else if (typeof(ILifecycle).IsAssignableFrom(typeClass))
                    {
                        type = NHibernateUtil.Entity(typeClass);
                    }
                    else if (typeClass.IsEnum)
                    {
                        try
                        {
                            type = (IType)Activator.CreateInstance(typeof(EnumType <>).MakeGenericType(typeClass));
                        }
                        catch (Exception e)
                        {
                            throw new MappingException("Can't instantiate enum " + typeClass.FullName + "; The enum can't be empty", e);
                        }
                    }
                    else if (IsNullableEnum(typeClass))
                    {
                        try
                        {
                            type = (IType)Activator.CreateInstance(typeof(EnumType <>).MakeGenericType(typeClass.GetGenericArguments()[0]));
                        }
                        catch (Exception e)
                        {
                            throw new MappingException("Can't instantiate enum " + typeClass.FullName + "; The enum can't be empty", e);
                        }
                    }
                    else if (typeClass.IsSerializable)
                    {
                        if (typeClassification == TypeClassification.Length)
                        {
                            type = GetSerializableType(typeClass, Int32.Parse(parsedTypeName[1]));
                        }
                        else if (length != null)
                        {
                            type = GetSerializableType(typeClass, length.Value);
                        }
                        else
                        {
                            type = GetSerializableType(typeClass);
                        }
                    }
                }
            }
            return(type);
        }
		/// <summary>
		/// Uses heuristics to deduce a NHibernate type given a string naming the
		/// type.
		/// </summary>
		/// <param name="typeName">the type name</param>
		/// <param name="parameters">parameters for the type</param>
		/// <returns>An instance of <c>NHibernate.Type.IType</c></returns>
		public static IType HeuristicType(string typeName, IDictionary<string, string> parameters)
		{
			IType type = Basic(typeName);

			if (type == null)
			{
				string[] parsedTypeName;
				TypeClassification typeClassification = GetTypeClassification(typeName);
				if (typeClassification == TypeClassification.Length)
				{
					parsedTypeName = typeName.Split(LengthSplit);
				}
				else
					parsedTypeName = typeClassification == TypeClassification.PrecisionScale ? typeName.Split(PrecisionScaleSplit) : new[] { typeName };


				System.Type typeClass;
				try
				{
					typeClass = ReflectHelper.ClassForName(parsedTypeName[0]); //typeName);
				}
				catch (Exception)
				{
					typeClass = null;
				}

				if (typeClass != null)
				{
					if (typeof(IType).IsAssignableFrom(typeClass))
					{
						try
						{
							type = (IType) Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(typeClass);
						}
						catch (Exception e)
						{
							throw new MappingException("Could not instantiate IType " + typeClass.Name + ": " + e, e);
						}
						InjectParameters(type, parameters);
					}
					else if (typeof(ICompositeUserType).IsAssignableFrom(typeClass))
					{
						type = new CompositeCustomType(typeClass, parameters);
					}
					else if (typeof(IUserType).IsAssignableFrom(typeClass))
					{
						type = new CustomType(typeClass, parameters);
					}
					else if (typeof(ILifecycle).IsAssignableFrom(typeClass))
					{
						type = NHibernateUtil.Entity(typeClass);
					}
					else if (typeClass.IsEnum)
					{
						type = (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(typeClass));
					}
					else if (IsNullableEnum(typeClass))
					{
						type = (IType)Activator.CreateInstance(typeof(EnumType<>).MakeGenericType(typeClass.GetGenericArguments()[0]));
					}
					else if (typeClass.IsSerializable)
					{
						if (typeClassification == TypeClassification.Length)
						{
							type = GetSerializableType(typeClass, Int32.Parse(parsedTypeName[1]));
						}
						else
						{
							type = GetSerializableType(typeClass);
						}
					}
				}
			}
			return type;
		}
예제 #3
0
		public static IType GetMetaType(System.Type typeClass, IDictionary parameters)
		{
			IType type;
			if (typeof(IType).IsAssignableFrom(typeClass))
			{
				try
				{
					type = (IType)Activator.CreateInstance(typeClass);
				}
				catch (Exception e)
				{
					throw new MappingException("Could not instantiate IType " + typeClass.Name + ": " + e, e);
				}
				InjectParameters(type, parameters);
			}
			else if (typeof(ICompositeUserType).IsAssignableFrom(typeClass))
			{
				type = new CompositeCustomType(typeClass, parameters);
			}
			else if (typeof(IUserType).IsAssignableFrom(typeClass))
			{
				type = new CustomType(typeClass, parameters);
			}
			else if (typeof(ILifecycle).IsAssignableFrom(typeClass))
			{
				type = NHibernateUtil.Entity(typeClass);
			}
			else if (typeClass.IsEnum)
			{
				type = NHibernateUtil.Enum(typeClass);
			}
			else if (IsNullableEnum(typeClass))
			{
				type = NHibernateUtil.Enum(typeClass.GetGenericArguments()[0]);
			}
			else if (typeClass.IsSerializable)
			{
				var typeName = typeClass.FullName;
				var typeClassification = GetTypeClassification(typeName);
				if (typeClassification == TypeClassification.Length)
				{
					var parsedTypeName = typeName.Split(lengthSplit);
					type = GetSerializableType(typeClass, Int32.Parse(parsedTypeName[1]));
				}
				else
				{
					type = GetSerializableType(typeClass);
				}
			}
			else
			{
				type = null;
			}
			return type;
		}
		/// <summary>
		/// Uses heuristics to deduce a NHibernate type given a string naming the 
		/// type. 
		/// </summary>
		/// <param name="typeName"></param>
		/// <returns>An instance of <c>NHibernate.Type.IType</c></returns>
		/// <remarks>
		/// When looking for the NHibernate type it will look in the cache of the Basic types first.
		/// If it doesn't find it in the cache then it uses the typeName to get a reference to the
		/// Class (Type in .NET).  Once we get the reference to the .NET class we check to see if it
		/// implements IType, ICompositeUserType, IUserType, ILifecycle (Association), or 
		/// IPersistentEnum.  If none of those are implemented then we will serialize the Type to the
		/// database using NHibernate.Type.SerializableType(typeName)  
		/// </remarks>
		public static IType HeuristicType( string typeName )
		{
			IType type = TypeFactory.Basic( typeName );

			if( type == null )
			{
				string[ ] parsedTypeName;
				TypeClassification typeClassification = GetTypeClassification( typeName );
				if( typeClassification == TypeClassification.Length )
				{
					parsedTypeName = typeName.Split( lengthSplit );
				}
				else if( typeClassification == TypeClassification.PrecisionScale )
				{
					parsedTypeName = typeName.Split( precisionScaleSplit );
				}
				else
				{
					parsedTypeName = new string[ ] {typeName};
				}


				System.Type typeClass;
				try
				{
					typeClass = ReflectHelper.ClassForName( parsedTypeName[ 0 ] ); //typeName);
				}
				catch( Exception )
				{
					typeClass = null;
				}

				if( typeClass != null )
				{
					if( typeof( IType ).IsAssignableFrom( typeClass ) )
					{
						try
						{
							type = ( IType ) Activator.CreateInstance( typeClass );
						}
						catch( Exception e )
						{
							throw new MappingException( "Could not instantiate IType " + typeClass.Name + ": " + e, e );
						}
					}
					else if( typeof( ICompositeUserType ).IsAssignableFrom( typeClass ) )
					{
						type = new CompositeCustomType( typeClass );
					}
					else if( typeof( IUserType ).IsAssignableFrom( typeClass ) )
					{
						type = new CustomType( typeClass );
					}
					else if( typeof( ILifecycle ).IsAssignableFrom( typeClass ) )
					{
						type = NHibernateUtil.Entity( typeClass );
					}
					else if( typeClass.IsEnum )
					{
						type = NHibernateUtil.Enum( typeClass );
					}
					else if( typeClass.IsSerializable )
					{
						if( typeClassification == TypeClassification.Length )
						{
							type = GetSerializableType( typeClass, Int32.Parse( parsedTypeName[ 1 ] ) );
						}
						else
						{
							type = GetSerializableType( typeClass );
						}
					}
				}
			}
			return type;
		}
예제 #5
0
        /// <summary>
        /// Uses heuristics to deduce a NHibernate type given a string naming the
        /// type.
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns>An instance of <c>NHibernate.Type.IType</c></returns>
        /// <remarks>
        /// When looking for the NHibernate type it will look in the cache of the Basic types first.
        /// If it doesn't find it in the cache then it uses the typeName to get a reference to the
        /// Class (Type in .NET).  Once we get the reference to the .NET class we check to see if it
        /// implements IType, ICompositeUserType, IUserType, ILifecycle (Association), or
        /// IPersistentEnum.  If none of those are implemented then we will serialize the Type to the
        /// database using NHibernate.Type.SerializableType(typeName)
        /// </remarks>
        public static IType HeuristicType(string typeName)
        {
            IType type = TypeFactory.Basic(typeName);

            if (type == null)
            {
                string[]           parsedTypeName;
                TypeClassification typeClassification = GetTypeClassification(typeName);
                if (typeClassification == TypeClassification.Length)
                {
                    parsedTypeName = typeName.Split(lengthSplit);
                }
                else if (typeClassification == TypeClassification.PrecisionScale)
                {
                    parsedTypeName = typeName.Split(precisionScaleSplit);
                }
                else
                {
                    parsedTypeName = new string[] { typeName };
                }


                System.Type typeClass;
                try
                {
                    typeClass = ReflectHelper.ClassForName(parsedTypeName[0]);                         //typeName);
                }
                catch (Exception)
                {
                    typeClass = null;
                }

                if (typeClass != null)
                {
                    if (typeof(IType).IsAssignableFrom(typeClass))
                    {
                        try
                        {
                            type = ( IType )Activator.CreateInstance(typeClass);
                        }
                        catch (Exception e)
                        {
                            throw new MappingException("Could not instantiate IType " + typeClass.Name + ": " + e, e);
                        }
                    }
                    else if (typeof(ICompositeUserType).IsAssignableFrom(typeClass))
                    {
                        type = new CompositeCustomType(typeClass);
                    }
                    else if (typeof(IUserType).IsAssignableFrom(typeClass))
                    {
                        type = new CustomType(typeClass);
                    }
                    else if (typeof(ILifecycle).IsAssignableFrom(typeClass))
                    {
                        type = NHibernateUtil.Entity(typeClass);
                    }
                    else if (typeClass.IsEnum)
                    {
                        type = NHibernateUtil.Enum(typeClass);
                    }
                    else if (typeClass.IsSerializable)
                    {
                        if (typeClassification == TypeClassification.Length)
                        {
                            type = GetSerializableType(typeClass, Int32.Parse(parsedTypeName[1]));
                        }
                        else
                        {
                            type = GetSerializableType(typeClass);
                        }
                    }
                }
            }
            return(type);
        }