Circular reference

G

Guest

Hi

I Have a solution with about 50 projects and each project have References to
1 to n of the projects in the solution. I try go to a project and try to add
a reference to another project and I get a Circular Reference" error and
cannot reference the project.

Is there any way to find out the what Method or Project is causing the problem
If I am on Project A tring to add Project B - I checked that A doesn't
include B and B doesn't include A - it must be some other project that is
referenced and calling someone else.

Just looking for a suggestion to track down the culprit :)
Thanks
 
M

Marc Gravell

It is actually pretty easy to list the entire reference tree for an
assembly, either through reflection (if compiled) or through parsing
the csproj files (if not). I have samples of both, but they are tied
into other code that I don't want to have to remove - but you may find
this useful for pulling it apart. Note that you need to be a little
careful : the 2 core MS libs at the bottom are themselves circular, so
you need to check against that when enumerating.

Get an Assembly either from a known Type or from the startup Assembly.
From an Assembly you can use GetReferencedAssemblies() to get the
references, which you can then attempt to load via the static
Assembly.Load method (which IIRC returns the existing Assembly if it
is already loaded) - but note that this can throw an exception if the
necessary reference isn't found.

I would probably just use the above to output the tree of your problem
assembly (where you can't add the reference), which should quickly
reveal why... no more that 20 minutes coding I'd guess.

Marc
 
W

WenYuan Wang [MSFT]

Hi,
According to your description, I understand you want know how to track down
the "Circular Reference" error.

For example: we have three Projects: ProjectA, ProjectB, ProjectC.
ProjectA has a reference to ProjectB, (ProjectA->ProjectB)
ProjectB has a reference to ProjectC. (ProjectB->ProjectC)
When we are on ProjectC trying to add referece to ProjectB, we will receive
an error massage "Circular Reference". (Because ProjectB has a reference to
ProjectC)
Additionally, if we are going to add referece to ProjectA, we will also
receive the same error meassage "Circular Reference".
(ProjectA->ProjectB->ProjectC).

Apparently, I think the second scenario is similar to your solution.
To trouble down such issue, as Marc said, add some code to draw a
dependence tree of your project.
Another choice, you may use the feature "Project References" in Visual
Studio 2005 IDE.
Right Click your Project, Select "Project dependences..." option in context
menu.
If there is a Circular Reference between two projects, there must have the
same project entry in Project dependences dialog.
In this scenario, it is ProjectB.

By the way, I think "Reflector" is also a good option. Thanks for Marc's
suggestion.:)

Hope this helps. If you have anything unclear, please feel free to update
here and I'm glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
===============================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
===============================
(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