Search for Type in .NET Framework

  • Thread starter Thread starter Dan Manges
  • Start date Start date
D

Dan Manges

Given a type as a string, is there an easy way using reflection to find
where it resides in the .NET framework?

Example:
Given "XmlNode", get "System.Xml"
Given "ArrayList", get "System.Collections"
Given "DataSet", get "System.Data"

Thanks in advance,

Dan Manges
 
Hello Dan,

I'm not sure that you can do it with Assembly class, because you need to
know the asssebly name, at least part name.

But there exists a bit tricky way to complete you task - Refrector API
There is a good tool "Reflector" to scrutinize assemblies and this tool provides
u API (just add reflector.exe to the references)

The idea is that: read the folder path to the .net FW asseblies from the
registry, get the list of .dll from this folder and check each assembly for
the class that u need.

DM> Given a type as a string, is there an easy way using reflection to
DM> find where it resides in the .NET framework?
DM>
DM> Example:
DM> Given "XmlNode", get "System.Xml"
DM> Given "ArrayList", get "System.Collections"
DM> Given "DataSet", get "System.Data"
DM> Thanks in advance,
DM>
DM> Dan Manges
DM>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
Follow is a small snippet where i locate a type in asemblies.
public static Type FindType(String typeToFind)
{
// look in standard .Net types
Type ct = Type.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in the current assembly i have loaded with LoadFrom
ct = currentAssembly.assembly.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in assemblies collection built from references in the current
assembly
foreach(Assembly assm in extAssemblies)
{
ct = assm.GetType(typeToFind,false);
if (ct != null)
return(ct);
}
// brute force way by going through modules in the current assembly
Module[] mods = currentAssembly.assembly.GetModules();
foreach(Module mod in mods)
{
Type[] found = mod.FindTypes(Module.FilterTypeName, typeToFind);
if (found.Length == 1)
return(found[0]);
}
throw new Exception("Type not found " + typeToFind);
}

Hope this helps
Leon Lambert
 
Leon,

Thanks - this really helps. In your code, where does extAssemblies come
from?

Thanks again,

Dan
 
It is built in an assembly resolver callback routine. Below is a snippet.

// following are the data members in my worker class
private static ArrayList extAssemblies = new ArrayList();
private static Hashtable assembliesLookup = new Hashtable();
private static ILAssembly currentAssembly;
private String assemblyName;
private Assembly assembly;
private ArrayList classes = new ArrayList();
private ResolveEventHandler resolveEventHandler;
private static String assemblyNameToResolve;
private static ArrayList extAssemblyDirectory;

// Somewhere in you worker class upon initialization install an
// Assembly resolver.
resolveEventHandler = new ResolveEventHandler(AssemblyResolver);
Thread.GetDomain().AssemblyResolve += resolveEventHandler;

// This will now get called when .Net needs to resolve an assembly.
public Assembly AssemblyResolver(Object sender,ResolveEventArgs args)
{
String path;

// assembly is the current assembly i am loading
// get the path of the current assembly to be used
// in trying to load referenced assemblies
path =
assembly.Location.Substring(0,assembly.Location.LastIndexOf("\\") + 1);
assemblyNameToResolve = args.Name;

// just get the raw assembly name
if ((assemblyNameToResolve.IndexOf(",") != -1) &&
(assemblyNameToResolve.IndexOf("Culture") != -1))
assemblyNameToResolve =
assemblyNameToResolve.Substring(0,assemblyNameToResolve.IndexOf(","));

// see if i already looked it up
Assembly assm = assembliesLookup[assemblyNameToResolve] as Assembly;
if (assm != null)
return(assm);
try
{
// try in the current assembly directory
assm =
Assembly.LoadFrom(String.Concat(path,assemblyNameToResolve,".dll"));
}
catch (Exception)
{
}
if (assm == null)
{
// extAssemblyDirectory is a collection of paths that can be passed
// into my tool to specify additional paths where to look for
// assemblies. These would be paths to referenced assemblies
// not in the GAC or the current directory.

foreach (String assemblyPath in extAssemblyDirectory)
{
try
{
// try looking in the external path
assm =
Assembly.LoadFrom(String.Concat(assemblyPath,assemblyNameToResolve,".dll"));
if (assm != null)
break;
}
catch (Exception)
{
}
}
}
if (assm == null)
throw(new Exception("Cant find assembly " +
assemblyNameToResolve));
// add to collection to be used for finding types
extAssemblies.Add(assm);
assembliesLookup.Add(assemblyNameToResolve,assm);
assemblyNameToResolve = null;
return(assm);
}

Hope there is enough info here to help. Sorry for word wrap from the
usenet tool.

Leon Lambert

Dan said:
Leon,

Thanks - this really helps. In your code, where does extAssemblies come
from?

Thanks again,

Dan

Leon said:
Follow is a small snippet where i locate a type in asemblies.
public static Type FindType(String typeToFind)
{
// look in standard .Net types
Type ct = Type.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in the current assembly i have loaded with LoadFrom
ct = currentAssembly.assembly.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in assemblies collection built from references in the current
assembly
foreach(Assembly assm in extAssemblies)
{
ct = assm.GetType(typeToFind,false);
if (ct != null)
return(ct);
}
// brute force way by going through modules in the current assembly
Module[] mods = currentAssembly.assembly.GetModules();
foreach(Module mod in mods)
{
Type[] found = mod.FindTypes(Module.FilterTypeName, typeToFind);
if (found.Length == 1)
return(found[0]);
}
throw new Exception("Type not found " + typeToFind);
}

Hope this helps
Leon Lambert
 

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

Back
Top