Reflection to find the assemblies referenced by a C# project


J

Jimi

Given a C# project file path, can I use reflection to get all the
assemblies referenced by the project?

e.g., I know the path of a C# project, say, "c:\SomeProject.csproj",
and I want to load its referenced assemblies to construct a list of
their members for display. Note that "c:\SomeProject.csproj" is not
the current project, it's just a project we're displaying information
about in the utility I'm trying to construct.
 
Ad

Advertisements

J

Justin Rogers

..csproj files are XML. You can use XML to process this file. The issue that
you'll run into is loading project references as opposed to physical/assembly
references. You'll have to do work, to make sure the project reference is
built so that you can inspect it's types via reflection.
 
D

Dmitriy Lapshin [C# / .NET MVP]

I think it would be easier to parse the project file and extract this
information right from the data contained therein.
 
J

Jimi

Thanks Justin and Dmitriy.

From your answers, and a little more digging, I think I'm going to do
the following:

1. parse the C# project file for the references (easy - done this
before)

2. search to see if the references have a built dll/exe somewhere

3. use something like:
"System.Reflection.Assembly asm =
System.Reflection.Assembly.LoadFrom(sPath);" to get load the
assemblies so I can examine its types and members (easy - done this
before)

1 & 3 are easy - I've done these sort of things before. But 2 is
a bit messy - if the reference is an assembly registered in the GAC,
then no problem (I just use the assembly name), but if the reference
is to another .NET project, then all the first "csproj" file will
tell me is the Name, Project, and Package of the referenced project -
none of these tell me where to find the other referenced project. Do
I then have to search for solution files that may have these ? This
just gets messier each step. Is there a more elegant way to do this
part?
 
Ad

Advertisements

J

Jimi

Any ideas on part 2 of this?

I can get the assembly names of referenced C# project via the
GetReferencedAssemblies method on the Assembly object, but there
seems to be no way to get from the assembly name to an actual
assembly object (since it's not in the GAC), without knowing the
path. Do I need to scan entire drives to find the referenced
project's assembly dll? This seems a bit crazy - there must be an
easy way to get to the debug assemblies of referenced projects
without knowing the paths. Or, there must be an easy way to find the
path of a referenced project? (it's not in the "csproj" file, by the
way - it's in solution files, but this still leads to scanning drives
to find any solution files that may contain the C# project we're
interested in). This is driving me crazy.
 

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