I case people don't want to go to the other group. Here is what I am trying 
to accomplish.

I have an assembly that is dynamically created at runtime using 
Mono.CSharp. The assembly contains the POCOs as well as the Class Mappings. 
I have a default class that I instantiate from the assembly in order to get 
at its assembly name and type information. I then proceed to create my 
session factory using the type information in the dynamic assembly to 
create the class mappings. It fails with the following error message: 
"Could not load file or assembly 'eval-3' or one of its dependencies. The 
system cannot find the file specified." It appears that the assembly is 
trying to be loaded from the drive, but it does not exist on the drive, 
only in memory.

Another gentleman, named Randy Burden, suggested that I try to use 
AppDomain.ResolveAssembly. However, it too fails with the following 
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.

Updating the ReflectHelper.cs TypeFromAssembly method as follows fixes the 
issue.

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;
}
}

My questions are as follows...

1. Can anyone thinl of a different approach that I am overlooking?
2. If not, what is the best approach to getting this into NHibernate trunk? 
I did already fork the "nhibernate-core" project on Github and I am in the 
process of writing a unit test that shows the issue.
3. Should I generate an issue on the bug tracker?

I am not sure what the best practices are with regards to resolving issues 
with or requesting features to NHibernate. Perhaps I overlooked a document 
that explains the process?

Once again, thanks in advance for any feedback.

Leo

On Wednesday, February 6, 2013 9:16:39 AM UTC-7, Leo Duran wrote:
>
> I started a thread on the nhusers group about an issue I am having with 
> dynamic assemblies. I figured that instead of retyping the information 
> here, I would just link to the thread in the other group.
>
> https://groups.google.com/forum/#!topic/nhusers/21O2_5pumjE
>
> Does anyone here have any feedback?
>
> Thanks in advance.
>
> Leo
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"nhibernate-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to