Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / June 2009

Tip: Looking for answers? Try searching our database.

Have 2 projects that need to reference each other

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 15 Jun 2009 05:35 GMT
Hi;

Here's the basic problem, I have a boatload of code that is J# and a
small amount that is C#. In a perfect world VS 2005 would let me put
both in the same project. But VS 2005 does not support that.

The root problem is that the C# code implements an interface in the J#
code. To make this work I think I would end up having to have 5 DLLs
(things are very intertwined in the interface dependencies).

Is there any way to get two projects to build where each uses code in
the other on VS 2005?

If not, can I use reflection in the project that does not reference
the other to create objects of it's class? Or does reflection only
work for classes in the same project?

thanks - dave

david@at-at-at@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
Jialiang Ge [MSFT] - 15 Jun 2009 11:22 GMT
Hello Dave

Visual Studio does not allow circular dependency. The best practice is to
refactor the app to remove the circular dependency. If you insist on the
demand, one workaround is to use .NET Reflection (see sample 1), the other
one is to use some tricks of csc compiler (see sample 2).

Sample 1 Use .NET Reflection:

1. Add reference "Assembly1.dll" to project Assembly2 in IDE. Then, the
code snippet looks like:
//project Assembly2
namespace Assembly2
{
   public class Class2
   {
       public static string someData1 = "some data 1 in Assembly2.Class2";
       public string someData2 = "some data 2 in Assembly2.Class2";

       public static string SomeMethod1()
       {
           return Assembly1.Class1.someData1;
       }

       public string SomeMethod2()
       {
           Assembly1.Class1 c = new Assembly1.Class1();
           return c.someData2;
       }
   }
}

2. Now, you cannot add reference "Assembly2.dll" to project Assembly1,
which will cause "circular dependency". So, please use reflection to call
the methods of Assembly2 in Assembly1. The code snippet looks like:

namespace Assembly1
{
   public class Class1
   {
       public static string someData1 = "some data 1 in Assembly1.Class1";
       public string someData2 = "some data 2 in Assembly1.Class1";

       public static string SomeMethod1()
       {
           Assembly assembly2 = Assembly.Load("Assembly2");
           Type type = assembly2.GetType("Assembly2.Class2");
           return type.GetField("someData1").GetValue(null) as string;
       }

       public string SomeMethod2()
       {
           Assembly assembly2 = Assembly.Load("Assembly2");
           Type type = assembly2.GetType("Assembly2.Class2");
           object inst = assembly2.CreateInstance("Assembly2.Class2");
           return type.GetField("someData2").GetValue(inst) as string;
       }
   }
}

Sample 2 Use csc.exe to build the app (I do not recommend the trick)

1. Assembly1 and Assembly2  look like:

//project Assembly2
namespace Assembly2
{
   public class Class2
   {
       public static string someData1 = "some data 1 in Assembly2.Class2";
       public string someData2 = "some data 2 in Assembly2.Class2";

       public static string SomeMethod1()
       {
           return Assembly1.Class1.someData1;
       }

       public string SomeMethod2()
       {
           Assembly1.Class1 c = new Assembly1.Class1();
           return c.someData2;
       }
   }
}

namespace Assembly1
{
   public class Class1
   {
       public static string someData1 = "some data 1 in Assembly1.Class1";
       public string someData2 = "some data 2 in Assembly1.Class1";

       public static string SomeMethod1()
       {
           //return Assembly2.Class2.someData1;
           return null;
       }

       public string SomeMethod2()
       {
           //Assembly2.Class2 c = new Assembly2.Class2();
           //return c.someData2;
           return null;
       }
   }
}

Pay attention to Assembly1, here is a trick. I replace "return
Assembly2.Class2.someData1;" with "return null;" in SomeMethods1() and do
similar thing in SomeMethods2().

2. Use the following command to build Assembly1 firstly:
csc .exe /t:library /out: Assembly1\bin\Debug\Assembly1.dll Assembly1\*.cs

3. Build Assembly2:
csc .exe /t:library /out: Assembly2\bin\Debug\Assembly2.dll /reference:
Assembly1\bin\Debug\Assembly1.dll Assembly2\*.cs

4. Uncomment the "return Assembly2.Class2.someData1;" and comment "return
null;" in Assembly1.Class1.SomeMethod1(), and do similar thing in
SomeMethods2(). Now, the code in Assembly1 looks like the following:

namespace Assembly1
{
   public class Class1
   {
       public static string someData1 = "some data 1 in Assembly1.Class1";
       public string someData2 = "some data 2 in Assembly1.Class1";

       public static string SomeMethod1()
       {
           return Assembly2.Class2.someData1;
           //return null;
       }

       public string SomeMethod2()
       {
           Assembly2.Class2 c = new Assembly2.Class2();
           return c.someData2;
           //return null;
       }
   }
}

5. Build Assembly1:
csc .exe /t:library /out: Assembly1\bin\Debug\Assembly1.dll /reference:
Assembly2\bin\Debug\Assembly2.dll Assembly1\*.cs

6. Build the whole app
csc /t:exe /out:App\bin\Debug\App.exe
/reference:Assembly1\bin\Debug\Assembly1.dll,Assembly2\bin\Debug\Assembly2.d
ll App\*.cs

7. Now, the two assemblies reference each other.

Regards,
Jialiang Ge (jialge@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Maxwell - 17 Jun 2009 16:20 GMT
> Hi;
>
[quoted text clipped - 20 lines]
>
> Cubicle Wars - http://www.windwardreports.com/film.htm

Pull the interface out to a second J# project. Let both original
projects reference the interface project.

Cheers,

Maxwell
David Thielen - 19 Jun 2009 21:38 GMT
Tried - those interfaces reference classes that reference classes
that... When we walked it the whole way through it turned out to be
circular - can't split it.

>Pull the interface out to a second J# project. Let both original
>projects reference the interface project.
>
>Cheers,
>
>Maxwell

david@at-at-at@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.