Slight
Slight

Reputation: 1582

How to access a referenced assembly's types?

If I reference an assembly A.dll in my project and A.dll references B.dll. My project cannot access the exported namespaces/classes of B.dll, only A.dll. Is there something I can do so I don't have to directly reference B.dll in my project in order to access its classes?

Upvotes: 1

Views: 291

Answers (2)

quetzalcoatl
quetzalcoatl

Reputation: 33506

No, there is no way *) You have to reference all assemblies you want to use types for. This implies that if you use A and if you use anything from A that exposes a type from B, you must reference the B too.

quick edit: uh, ok, actually there is some way - if you don't want to use B.dll but if you wouldn't mind using a XYZ.dll instead, you could make a XYZ.dll that mimics relevant types from the B.dll, and then try to develop some ticks ased on i.e. assembly redirection.. with it you theoretically could have your app reference A.dll and XYZ.dll but in runtime it would use A.dll and B.dll. If speaking about project&compilation, there are some tricks the compiler uses (or used to use) to support cross-platform compilation (i.e. stubbing assemblies from WindowsPhone just to get references satisfied)..

*) afterthought: you could try using dynamic and its dynamic binding:

// in A.dll
public dynamic Method() {
    return new ClassFromB(); // thing from B.dll
}

// in your app:
// do not ever explicitely name ANY type from:
dynamic foo = ClassFromA.Method();
dynamic result = foo.CallSomething(1,2,3); // even when getting results
dynamic other = result.Boom(foo); // and even when passing params

as long as all reference types and methods are be shadowed by dynamic, there is some chance it won't ask you to reference the B.dll, since there's no explicit type reference.. - but I actually never tried it for this effect, so I can't tell for 100% sure

however, when using dynamic all the time, you will resign from many compile-time checks and any error in parameter types or method names will be rised at runtime only - just like using reflection

Upvotes: 1

Ondrej Tucny
Ondrej Tucny

Reputation: 27962

This behavior is by design. Except stuff like using reflection to access any entities in your B.dll on run-time, there's no other way than referencing B.dll directly.

Upvotes: 1

Related Questions