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