Freigeben über


How to create circularly referenced assemblies

In .Net framework System.dll and System.Xml.dll reference each other. How is it possible?

Circularly referenced assemblies are chicken-and-egg problem. The trick is how to break the dependency chain.

Here is one way I find working.

Step 1. Create assembly A, includes A only types. A references nothing. (Except framework assemblies).
Step 2. Create assembly B, uses A's types, and includes B only interfaces. B now references A.
Step 3. Re-write A, includes A only types, and uses types in B. Now A also references B.

Now you have A and B reference each other.

Let's put it in code.

Step 1: a.cs
using System;

public interface IA
{
    String IAMethod();
}

c:\>csc /t:library a.cs

Step 2: b.cs
using System;

public interface IB
{
    String IBMethod();
}

class InheritA: IA
{
    public String IAMethod()
    {
        return Type.GetType("InheritA").ToString();
    }
}

c:\>csc /t:library /r:a.dll b.cs

Step3: a.cs
using System;

public interface IA
{
    String IAMethod();
}

public class InheritB: IB
{
    public String IBMethod()
    {
        return Type.GetType("InheritB").ToString();
    }
}

c:\>csc /t:library /r:b.dll a.cs

Done!

Comments

  • Anonymous
    February 01, 2004
    Now, have you tested this with strongly named assemblies? And once done with the two assemblies actually make use of them in a third assembly (executable) to make sure all of the dependency chain still exists.
  • Anonymous
    February 01, 2004
    And could I modify b.cs, right?
  • Anonymous
    February 02, 2004
    Justin, this also works for strongly named assemblies. If you don't believe it, you can take the sample, add a key file, and try it for yourself.

    The dependencies are burned in in the meta data of the assembly. Once the assembly is compiled, the dependencies will always exist, no matter how you use it.

    Woo Soek Seo, the sample is to show you how to create circularly referenced assemblies. Once the assemblies are created, you can change them any way you want.
  • Anonymous
    February 02, 2004
    Why would you want to?

    To me it suggests that something is wrong with your design.
  • Anonymous
    February 02, 2004
    Sam, I did not suggest this. I just show how it can be done. In fact, personally I am against this practice. But regardless what you and I believe, someone would like to know how to do this.