- Consolidated and improved error handling in LoadFile.

- Added warning when LoadFile returns previously loaded assembly, instead of the one specified.
This commit is contained in:
jfrijters 2010-05-12 08:43:13 +00:00
Родитель 34874d8355
Коммит 54763e3a6f
1 изменённых файлов: 45 добавлений и 30 удалений

Просмотреть файл

@ -69,17 +69,46 @@ namespace IKVM.Internal
internal Assembly LoadFile(string path) internal Assembly LoadFile(string path)
{ {
Assembly asm = universe.LoadFile(path); string ex = null;
// to avoid problems (i.e. weird exceptions), we don't allow assemblies to load that reference a newer version of mscorlib try
foreach (AssemblyName asmref in asm.GetReferencedAssemblies())
{ {
if (asmref.Name == "mscorlib" && asmref.Version > mscorlibVersion) using (RawModule module = universe.OpenRawModule(path))
{ {
Console.Error.WriteLine("Error: unable to load assembly '{0}' as it depends on a higher version of mscorlib than the one currently loaded", path); if (mscorlibVersion != null)
Environment.Exit(1); {
// to avoid problems (i.e. weird exceptions), we don't allow assemblies to load that reference a newer version of mscorlib
foreach (AssemblyName asmref in module.GetReferencedAssemblies())
{
if (asmref.Name == "mscorlib" && asmref.Version > mscorlibVersion)
{
Console.Error.WriteLine("Error: unable to load assembly '{0}' as it depends on a higher version of mscorlib than the one currently loaded", path);
Environment.Exit(1);
}
}
}
Assembly asm = universe.LoadAssembly(module);
if (asm.Location != module.Location)
{
Console.Error.WriteLine("Warning: assembly '{0}' is ignored as previously loaded assembly '{1}' has the same identity '{2}'", path, asm.Location, asm.FullName);
}
return asm;
} }
} }
return asm; catch (IOException x)
{
ex = x.Message;
}
catch (UnauthorizedAccessException x)
{
ex = x.Message;
}
catch (IKVM.Reflection.BadImageFormatException x)
{
ex = x.Message;
}
Console.Error.WriteLine("Error: unable to load assembly '{0}'" + Environment.NewLine + " ({1})", path, ex);
Environment.Exit(1);
return null;
} }
internal Assembly LoadWithPartialName(string name) internal Assembly LoadWithPartialName(string name)
@ -109,21 +138,15 @@ namespace IKVM.Internal
{ {
Assembly asm = null; Assembly asm = null;
cache.TryGetValue(reference, out asm); cache.TryGetValue(reference, out asm);
try if (asm == null)
{ {
if (asm == null) foreach (string found in FindAssemblyPath(reference))
{ {
foreach (string found in FindAssemblyPath(reference)) asm = LoadFile(found);
{ cache.Add(reference, asm);
asm = LoadFile(found); break;
cache.Add(reference, asm);
break;
}
} }
} }
catch (FileLoadException)
{
}
if (asm == null) if (asm == null)
{ {
Console.Error.WriteLine("Error: reference not found: {0}", reference); Console.Error.WriteLine("Error: reference not found: {0}", reference);
@ -135,20 +158,12 @@ namespace IKVM.Internal
{ {
foreach (string file in files) foreach (string file in files)
{ {
try Assembly asm;
if (!cache.TryGetValue(file, out asm))
{ {
Assembly asm; asm = LoadFile(file);
if (!cache.TryGetValue(file, out asm))
{
asm = LoadFile(file);
}
ArrayAppend(ref references, asm);
}
catch (FileLoadException)
{
Console.Error.WriteLine("Error: reference not found: {0}", file);
return 1;
} }
ArrayAppend(ref references, asm);
} }
} }
return 0; return 0;