// 12/31/2009
///
/// Allows LateBinding some Assembly (dll) file, and then will
/// instantiate the given 'ClassName', and return the object to the caller.
/// ///
AssemblyFile name to load
///
Class Name to instantiate within Assembly
///
(OUT) Instantiated object
///
True/False of success private bool LateBindAssembly(string assemblyFile, string className, out object instantiatedObject)
{
instantiatedObject = null;
try
{
// set platform location to load from.
#if XBOX360
const string platformType = "XBox360";
#else
const string platformType = "x86";
#endif
var a = Assembly.LoadFrom("0LateBinds/" + platformType + "/" + assemblyFile);
var mytypes = a.GetTypes();
//const BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static |
// BindingFlags.Instance);
// Search for Instance to instantiate from Assembly.
foreach (var t in mytypes)
{
//var mi = t.GetMethods(flags);
#if XBOX360
// locate class instance to instantiate.
if (t.Name == className)
{
instantiatedObject = Activator.CreateInstance(t);
((ICommonInitilization) instantiatedObject).CommonInitilization(this);
return true;
}
#else
// locate class instance to instantiate.
if (t.Name == className)
{
instantiatedObject = Activator.CreateInstance(t, this);
return true;
}
#endif
/*foreach (var m in mi)
{
m.Invoke(obj, null);
}*/
}
// Name not found
return false;
}
// 1/1/2010 - Capture the possibility of the DLL not being in the folder at all.
catch (FileNotFoundException) // PC throws this.
{
System.Console.WriteLine(@"DLL Component {0} not found. Therefore, this will be skipped for late binding.", assemblyFile);
return false;
}
catch(IOException) // XBOX throws this.
{
System.Console.WriteLine(@"DLL Component {0} not found. Therefore, this will be skipped for late binding.", assemblyFile);
return false;
}
#if !XBOX360
catch (ReflectionTypeLoadException err)
{
if (err.LoaderExceptions != null)
{
// List out each LoaderException error to console.
foreach (var loaderException in err.LoaderExceptions)
{
if (loaderException.Message != null)
System.Console.WriteLine(@"LoaderExceptions reflection error - {0}", loaderException.Message);
}
MessageBox.Show(
@"Late-Binding failed, due to a Loading Exception on the Interface! This usually occurs if you have an outdated interface; please update your interface for the assembly you are trying to late-bind.",
@"LateBind Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
System.Console.WriteLine(@"DLL Component {0} reflection error. Therefore, this will be skipped for late binding.", assemblyFile);
return false;
}
#endif
}