Reflection...

  • Thread starter Thread starter me
  • Start date Start date
M

me

Hello,

What I would like to accomplish is scan a directory for
*.dll, then find if any of the .dlls in the directory
contain a certain method.

I have code to determine if the method is in the class...
method, what I don't understand is how to "open" the dll
find the class and then apply the code to determine if
the method is in the class

Thanks
 
On top of my head: create a new AppDomain, load the DLL into this domain,
and then look for the method.
 
How about:



Assembly assemblyChecker = typeof(Object).Module.Assembly;

// Create a reference to the current directory.

DirectoryInfo di = new DirectoryInfo
(Environment.CurrentDirectory);

// Create an array representing the files in the current
directory.
FileInfo[] directoryFiles = di.GetFiles();
Console.WriteLine("The following files exist in the
directory:");
// Print out the names of the files in the current
directory.
foreach (FileInfo fi in directoryFiles)
{
string fullPath;
fullPath = Path.GetFullPath( fi.Name );
string ext;
ext = fi.Extension.ToLower();
if( ext != ".dll" )
{
continue;
}


Console.WriteLine("Checking: '{0}'", fullPath );

// Loads an assembly using its file name
try
{
assemblyChecker = Assembly.LoadFrom(
fullPath );

// Gets the type names from the assembly.
Type [] types2 = assemblyChecker.GetTypes
();
foreach (Type t in types2)
{
Console.WriteLine
("Assembly: '{0}'", t.FullName );

MemberInfo[] mbrInfo =
t.GetMethods
(BindingFlags.Public|BindingFlags.Instance|BindingFlags.De
claredOnly);

foreach (MemberInfo m in mbrInfo)
{
Trace.WriteLine(
m.Name.ToString() );
}

}
}
catch(Exception e)
{
Console.WriteLine( "No type information
available" );
}
}
 
PERFECT! ... Just what I needed !!

Thanks

-----Original Message-----
How about:



Assembly assemblyChecker = typeof (Object).Module.Assembly;

// Create a reference to the current directory.

DirectoryInfo di = new DirectoryInfo
(Environment.CurrentDirectory);

// Create an array representing the files in the current
directory.
FileInfo[] directoryFiles = di.GetFiles();
Console.WriteLine("The following files exist in the
directory:");
// Print out the names of the files in the current
directory.
foreach (FileInfo fi in directoryFiles)
{
string fullPath;
fullPath = Path.GetFullPath( fi.Name );
string ext;
ext = fi.Extension.ToLower();
if( ext != ".dll" )
{
continue;
}


Console.WriteLine("Checking: '{0}'", fullPath );

// Loads an assembly using its file name
try
{
assemblyChecker = Assembly.LoadFrom(
fullPath );

// Gets the type names from the assembly.
Type [] types2 = assemblyChecker.GetTypes
();
foreach (Type t in types2)
{
Console.WriteLine
("Assembly: '{0}'", t.FullName );

MemberInfo[] mbrInfo =
t.GetMethods
(BindingFlags.Public|BindingFlags.Instance|BindingFlags.De
claredOnly);

foreach (MemberInfo m in mbrInfo)
{
Trace.WriteLine(
m.Name.ToString() );
}

}
}
catch(Exception e)
{
Console.WriteLine( "No type information
available" );
}
}



-----Original Message-----
On top of my head: create a new AppDomain, load the DLL into this domain,
and then look for the method.


.
.
 
Back
Top