Reputation: 48496
This is in AssemblyA
namespace AssemblyA
{
public class ClassA
{
public static void Main(string[] args)
{
ClassB b = new ClassB();
Console.WriteLine(b.MultiplyTheSumByFactor(2, 3, 4));
Console.ReadKey();
}
public int Multiply(int left, int right)
{
return left * right;
}
}
}
This is in AssemblyB
namespace AssemblyB
{
public class ClassB
{
public int Sum(int left, int right)
{
return left + right;
}
public int MultiplyTheSumByFactor(int left, int right, int factor)
{
// ClassA a = new ClassA(); // can't reference AssemblyA
int sum = Sum(left, right);
Type type = Assembly.GetCallingAssembly().GetTypes().First();
object a = Activator.CreateInstance(type);
return (int)type.GetMethod("Multiply").Invoke(a, new object[] { sum, factor });
}
}
}
AssemblyA
is a console application that references AssemblyB.
Why can't I reference AssemblyA
from AssemblyB
, though I can load AssemblyA
through
reflection and still call it's methods?
Obviously there is virtually no reason for wanting to do this, I'm just curious as to how the compiler / CLR works in order to allow this to work through reflection.
Upvotes: 4
Views: 773
Reputation: 51349
Because compiling an assembly requires linking to it's dependencies. If the dependencies for A are not yet compiled (because they reference A) you end up with a chicken and egg problem.
Upvotes: 5