reflection for plug in

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

Mattias Sjögren

J

Jeffrey Tan[MSFT]

Hi Scott,

Yes, the article Mattias provided checks the PE file header to determine if
the .Net header exists. This approach is useful to identify whether the PE
file is .Net assembly or native unmanaged image. However, if you want to go
deeper to check verify if the .Net assembly is a valid one, you should use
PEVerify to verify the assembly. This will eliminate your need of handling
BadImageFormatException and FileLoadException.

By reviewing your code, I am not sure ReflectionTypeLoadException is used
to handle which method's exception. I did not see any code in your method
will throw ReflectionTypeLoadException. MSDN stated that
ReflectionTypeLoadException is thrown by the Module.GetTypes method,
however, it seems that you did not call Module.GetTypes method in your
code.

Normally, we may enumerate all the types in the assembly and check the
"Name" property of each type to see if certain type existed in the
assembly. A better design is as Mattias suggested to declare the supported
types in assembly level attribute, however, we can not control the assembly
from other develoeprs such as .Net FCL assemblies.

Hope this information helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

ScottP

Thanks for the input.

I agree that an attribute would be a good solution. I went down this
path for some reason and now I've got to retrace my steps.

Looking at the PE file header is a good trick.

I didn't use a config file, because I wanted to allow the runtime
discovery of the plugins without configuring the app. However, I'm not
sure that was a valid requirement...Oh well.

Thanks again.

-Scott
 
J

Jeffrey Tan[MSFT]

Hi Scott,

Glad to hear Mattias' suggestion can help you. If you need further help,
please feel free to post. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top