Assembly problem

T

Theodore

Hi,
from within an executable I am calling class A located in a dll file which
in turn calls class B located in another dll file. I want class B to be able
to resolve the last entry assembly. By calling the ExecutingAssembly i am
getting Class B assembly, if i call the CallingAssembly i get back the
System.Windows.Forms Assembly and finally if a call the GetEntryAssembly i
get the assembly of the executable.
So how can i get the Class A assembly?

Thanks
Theodore
 
D

Dave

You need to use GetCallingAssembly. The reason this is returning System.Windows.Forms is that the method you are calling
GetCallingAssembly in is being invoked directly from the System.Windows.Forms assembly. This is probably because you are handling a
Control event and the delegate you registered is being invoked by the Control, which is located in the System.Windows.Forms
assembly.

To get GetCallingAssembly to return the assembly that you want, you should save a reference to that assembly when the class is
constructed as long as the constructor is being invoked by assembly A. If A does not construct the object in B, then you should
implement an Initialize() method that A may call:

// in Assembly 'A'
class A
{
// When 'A' constructs 'B':
public A()
{
B = new B();
}

// When 'A' is passed a reference of 'B':
public A(B b)
{
b.Initialize(this.GetType().Assembly);
}
}

// in Assembly 'B'
class B
{
System.Runtime.Reflection.Assembly callingAssembly;

public B()
{
callingAssembly = System.Runtime.Reflection.Assembly.GetCallingAssembly();
}

// This is another option. Performing this task explicitly:
public B(System.Runtime.Reflection.Assembly caller)
{
if (caller == null)
throw new ArgumentNullException("caller");

callingAssembly = caller;
}

// Yet another option if class 'A' was not the object that constructed an instance of 'B':
public void Initialize(System.Runtime.Reflection.Assembly caller)
{
if (caller == null)
throw new ArgumentNullException("caller");

callingAssembly = caller;
}
}
 

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