-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Search type in all loaded assemblies
PostPosted: Thu Jun 15, 2006 4:43 pm 
Newbie

Joined: Thu Jun 15, 2006 3:40 pm
Posts: 4
I started using NHibernate with Visual Web Developer Express and had some troubles to get it to work. The main problem is that we don't know the names that will be generated for the assemblies, as they are random. To solve this I do not define the assembly name in the mapping files and made a minor change to the ReflectHelper to search for the type definition in all loaded assemblies. I would like that any NHibernate team member evaluate if this change could be incorporated to the releases of HNibernate. The change was done in TypeFromAssembly method:

Code:
      public static System.Type TypeFromAssembly( AssemblyQualifiedTypeName name )
      {
         System.Type type = null;

         try
         {
            /* original code

            // Try to get the type from an already loaded assembly
            System.Type type = System.Type.GetType( name.ToString() );

            if( type != null )
            {
               return type;
            }

            */

            ////////
            /// Search type in all loaded assemblies

            AppDomain currentDomain = AppDomain.CurrentDomain;
            Assembly[] loadedAssemblies = currentDomain.GetAssemblies();

            for ( int i = 0; i < loadedAssemblies.Length; i++ )
            {
                AssemblyQualifiedTypeName auxName = new AssemblyQualifiedTypeName( name.Type, loadedAssemblies[i].FullName );
                type = System.Type.GetType( auxName.ToString() );

               if ( log.IsDebugEnabled )
               {
                  log.Debug(" Trying to load " + auxName.ToString() + " from " + loadedAssemblies[i].FullName );
               }

               if ( type != null ) return type;
            }

            ////////


            if( name.Assembly == null )
            {
               // No assembly was specified for the type, so just fail
               log.Warn( "Could not load type " + name + ". Possible cause: no assembly name specified." );
               return null;
            }

            Assembly assembly = Assembly.Load( name.Assembly );

            if( assembly == null )
            {
               log.Warn( "Could not load type " + name + ". Possible cause: incorrect assembly name specified." );
               return null;
            }
         
            type = assembly.GetType( name.Type );

            if( type == null )
            {
               log.Warn( "Could not load type " + name + "." );
               return null;
            }

            return type;
         }
         catch( Exception e )
         {
            if( log.IsErrorEnabled )
            {
               log.Error( "Could not load type " + name + ".", e );
            }
            return null;
         }
      }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 6:16 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This is a hack, it's not 100% correct (what if two assemblies contain a type with the same namespace and name?) and probably will also be very slow, so no chance, sorry. But if it works for you, just create your own private version of NH and use it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 7:01 pm 
Newbie

Joined: Thu Jun 15, 2006 3:40 pm
Posts: 4
Ok sergey, thank you for your fast reply. I will use this hack only at development time.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 1:16 pm 
Newbie

Joined: Fri Jul 18, 2008 5:53 pm
Posts: 2
Maybe a hack, but thanks for this. I've chosen a different route, but this problem is quite real in the VS 2005 "Web Site Project" project template.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.