Get Types referenced by an assembly


G

Guest

The Assembly class has a GetReferencedAssemblies method that returns the
referenced assemblies. Is there a way to find what Types are referenced?
 
Ad

Advertisements

V

Vadym Stetsyak

You can get the types that are in the referenced assembly.

To find what types are referenced you have to operate on the level of source
code ( parse source code, get type declarations and search the assessmblies
they are stored in )

Another way is using reflection to iterate through class and get info about
params and return types etc. However you'll miss methods body, where the
type can be used.
 
C

Carlos J. Quintero [VB MVP]

You can get the public types of an assembly using the
Assembly.GetExportedTypes function, but to know which types are actually
used you need to check every declaration in the calling assembly, using
Reflection, as Vadym said.

Vadym,

At least using VS 2005 (not sure about .NET Framework 1.x) you can get the
variable declarations inside a body using MethodBody.LocalVariables.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
G

Guest

I am not interested in ALL the types in the referenced assembly. I only want
the types that I referenced. For example:

Assembly A:

object x = new Thing1();

Assembly B:

class Thing1() { ... }
class Thing2() { ... }
class Thing999() { ... }

I need something like this:

Assembly x = (reference to Assembly A);
Type[] refTypes = x.GetReferencedTypes();

refTypes should contain only Thing1; not Thing2, Thing999, etc.

Sorry I wasn't more explicity in my original posting.
 
C

Carlos J. Quintero [VB MVP]

Then you are going to use a lot of Reflection to "parse" the assembly: for
each module you get the types, the methods, its return types, its parameter
types, its member variables types, its procedure variables types, and so on.
I am not sure if a compiled assembly has already this information stored in
its metadata tables, because the compiler already did that job. Search for
articles talking about assembly internals and use ildasm.exe before taking
the long way. Start here:

http://blogs.msdn.com/trobbins/articles/404939.aspx

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Doug said:
I am not interested in ALL the types in the referenced assembly. I only
want
the types that I referenced. For example:

Assembly A:

object x = new Thing1();

Assembly B:

class Thing1() { ... }
class Thing2() { ... }
class Thing999() { ... }

I need something like this:

Assembly x = (reference to Assembly A);
Type[] refTypes = x.GetReferencedTypes();

refTypes should contain only Thing1; not Thing2, Thing999, etc.

Sorry I wasn't more explicity in my original posting.

Vadym Stetsyak said:
You can get the types that are in the referenced assembly.

To find what types are referenced you have to operate on the level of
source
code ( parse source code, get type declarations and search the
assessmblies
they are stored in )

Another way is using reflection to iterate through class and get info
about
params and return types etc. However you'll miss methods body, where the
type can be used.
 
M

Mattias Sjögren

The Assembly class has a GetReferencedAssemblies method that returns the
referenced assemblies. Is there a way to find what Types are referenced?
That's not easy to do with Reflection, but trivial if you're using
some other metaqdata reading library that gives you access to the
metadata tables. Then you just have to look for rows in the TypeRef
table scoped by the referenced assembly.


Mattias
 
Ad

Advertisements

V

Vadym Stetsyak

Mattias proposed good way how you can deal with your problem.

Here are the links to some resources:

(
http://www.dcl.hpi.uni-potsdam.de/research/loom/weblog/index.htm?&subaction=showfull&id=1097235170&archive=&start_from=&ucat=&file=/cms/research/loom/weblog/index.htm )

( http://msdn.microsoft.com/msdnmag/issues/01/03/hood/default.aspx )

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com


Doug said:
I am not interested in ALL the types in the referenced assembly. I only want
the types that I referenced. For example:

Assembly A:

object x = new Thing1();

Assembly B:

class Thing1() { ... }
class Thing2() { ... }
class Thing999() { ... }

I need something like this:

Assembly x = (reference to Assembly A);
Type[] refTypes = x.GetReferencedTypes();

refTypes should contain only Thing1; not Thing2, Thing999, etc.

Sorry I wasn't more explicity in my original posting.

Vadym Stetsyak said:
You can get the types that are in the referenced assembly.

To find what types are referenced you have to operate on the level of source
code ( parse source code, get type declarations and search the assessmblies
they are stored in )

Another way is using reflection to iterate through class and get info about
params and return types etc. However you'll miss methods body, where the
type can be used.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com


referenced?
 
G

Guest

Thanks to all of you for the excellent information, but this is looking like
way too much effort for such a "simple" thing. This seemed like such a
simple request that I was sure there was a class buried in the Framework that
I hadn't found. At least I know better now. :)

Thanks again.
 
Ad

Advertisements


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