Thank you for the suggestion. I did try to use the 
AppDomain.ResolveAssembly method but I get a completely different 
exception: "Resolving to a collectible assembly is not supported".

This error is generated by the call to System.Type type = System.Type.
GetType(name.ToString()); in the ReflectHelper.cs file on line 267. It 
appears that the assembly generated using Mono.CSharp is a collectible 
assembly. The documentation for System.Type.GetType does mention that it 
doesn't work with dynamically loaded assemblies.

After many hours of trying different options, I am left with modifying the 
source code. A colleague pointed out how naive my simple modification was 
and we enhanced it as follows.

public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, 
bool throwOnError)
{
try
{
// Try to get the type from an already loaded assembly
System.Type type = System.Type.GetType(name.ToString());

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

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

// See if the assembly is already loaded in the current AppDomain. 
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

Assembly assembly = null;

foreach (var loadedAssembly in loadedAssemblies)
{
var assemblyName = new AssemblyName(loadedAssembly.FullName);

if (loadedAssembly.FullName == name.Assembly || assemblyName.Name == 
name.Assembly)
{
assembly = loadedAssembly;
}
}

if(assembly == null)
{
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, throwOnError);

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);
}
if (throwOnError) throw;
return null;
}
}

Any other suggestions would be appreciated.


On Monday, February 4, 2013 10:40:34 PM UTC-7, Randy Burden wrote
>
> You don't have to modify the NHibernate source. You should be able to 
> leverage the .NET frameworks AppDomain.ResolveAssembly event which will 
> call whenever Assembly.Load or any other time when runtime attempts to load 
> and assembly. See the following MSDN link for more info: 
> http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to