S
ScottP
Hi all,
I am trying to create a plug-in handler that discovers and enumerates my
plug-ins at runtime. I've got it working, but I had to defensively trap
and swallow a lot of exceptions. Is there a better way to discover if
an executable is a valid assembly? Is there a better way to discover is
an assembly has a particular type?
Please see the code below that I am using to discover types in a
directory and add the valid assemblies to a list.
Thanks,
Scott
private void GetAvailableAnalysis()
{
Debug.Assert(m_PathToAnalysis != null);
string appPath = Path.GetDirectoryName(m_PathToAnalysis);
string[] files = Directory.GetFiles(appPath, "*.exe");
m_AnalysisNames.Clear();
m_AssemblyNames.Clear();
if (files != null )
{
foreach (string fileName in files)
{
Assembly thisAssembly;
try
{
thisAssembly =
Assembly.Load(Path.GetFileNameWithoutExtension(fileName));
if (thisAssembly != null)
{
Debug.WriteLine(string.Format("Assembly: {0}", thisAssembly));
Type[] assemblyTypes =
thisAssembly.GetTypes();
Type interfaceType =
Type.GetType("AnalysisProxy.IAnalyze");
if (assemblyTypes != null &&
interfaceType != null)
{
foreach (Type thisType in
assemblyTypes)
{
if
(interfaceType.IsAssignableFrom(thisType))
{
m_AnalysisNames.Add(thisType.FullName);
m_AssemblyNames.Add(thisAssembly.FullName);
Debug.WriteLine(string.Format("Found IAnalyze in Type: {0}", thisType));
}
else
{
Debug.WriteLine(string.Format("Not IAnalyze Type: {0}", thisType));
}
}
}
}
}
catch (ReflectionTypeLoadException ex)
{
// Swallow the exception. Type was not found.
Debug.WriteLine(string.Format("Type not
found in .NET assembly: {0}\n\n{1}", fileName, ex.Message));
}
catch (BadImageFormatException ex)
{
// do nothing, this is just not a .net assembly
Debug.WriteLine(string.Format("Not a .NET
assembly: {0}\n\n{1}", fileName, ex.Message));
}
catch (FileLoadException ex)
{
Debug.WriteLine(string.Format("FileLoadException loading: {0}\n\n{1}",
fileName, ex.Message));
}
catch (Exception ex)
{
string exceptionMessage = "Error occurred
trying to load: " + fileName;
throw new Exception(exceptionMessage,
ex.InnerException);
}
}
}
}
I am trying to create a plug-in handler that discovers and enumerates my
plug-ins at runtime. I've got it working, but I had to defensively trap
and swallow a lot of exceptions. Is there a better way to discover if
an executable is a valid assembly? Is there a better way to discover is
an assembly has a particular type?
Please see the code below that I am using to discover types in a
directory and add the valid assemblies to a list.
Thanks,
Scott
private void GetAvailableAnalysis()
{
Debug.Assert(m_PathToAnalysis != null);
string appPath = Path.GetDirectoryName(m_PathToAnalysis);
string[] files = Directory.GetFiles(appPath, "*.exe");
m_AnalysisNames.Clear();
m_AssemblyNames.Clear();
if (files != null )
{
foreach (string fileName in files)
{
Assembly thisAssembly;
try
{
thisAssembly =
Assembly.Load(Path.GetFileNameWithoutExtension(fileName));
if (thisAssembly != null)
{
Debug.WriteLine(string.Format("Assembly: {0}", thisAssembly));
Type[] assemblyTypes =
thisAssembly.GetTypes();
Type interfaceType =
Type.GetType("AnalysisProxy.IAnalyze");
if (assemblyTypes != null &&
interfaceType != null)
{
foreach (Type thisType in
assemblyTypes)
{
if
(interfaceType.IsAssignableFrom(thisType))
{
m_AnalysisNames.Add(thisType.FullName);
m_AssemblyNames.Add(thisAssembly.FullName);
Debug.WriteLine(string.Format("Found IAnalyze in Type: {0}", thisType));
}
else
{
Debug.WriteLine(string.Format("Not IAnalyze Type: {0}", thisType));
}
}
}
}
}
catch (ReflectionTypeLoadException ex)
{
// Swallow the exception. Type was not found.
Debug.WriteLine(string.Format("Type not
found in .NET assembly: {0}\n\n{1}", fileName, ex.Message));
}
catch (BadImageFormatException ex)
{
// do nothing, this is just not a .net assembly
Debug.WriteLine(string.Format("Not a .NET
assembly: {0}\n\n{1}", fileName, ex.Message));
}
catch (FileLoadException ex)
{
Debug.WriteLine(string.Format("FileLoadException loading: {0}\n\n{1}",
fileName, ex.Message));
}
catch (Exception ex)
{
string exceptionMessage = "Error occurred
trying to load: " + fileName;
throw new Exception(exceptionMessage,
ex.InnerException);
}
}
}
}