2

is it possible to use an interface type, that is defined in a huge external dll, without referencing that dll? in other words, there will be one core or global dll, that references the external dll, and all the projects reference this global one, so the external dlls are hidden from the other projects.

I want to use the type in my code, while knowing only about the global AllInterfaces project. can that work? and if so, what needs to be done for such a scenario?

4
  • Have a look into the dependency injection pattern, this might give you ideas. short outlook here : c-sharpcorner.com/UploadFile/ff2f08/… Commented Jan 29, 2012 at 13:08
  • I am wondering how DI would help here, if you need to statically use the interface
    – chandmk
    Commented Jan 29, 2012 at 13:11
  • If you only needs that one type why won't you introduce it into your project? Commented Jan 29, 2012 at 18:50
  • maybe I'm dense. what does it mean to introduce it to my project? the scenario is the external dll handles objects that implement its interface. I want to hide the external dll from my clients, and still be in static land.
    – kobi7
    Commented Jan 30, 2012 at 8:49

5 Answers 5

10

Is it possible to use an interface type that is defined in a huge external dll, without referencing that dll at compile time?

Not really, no. The compiler has the reasonable expectation that the types it needs are available.

Is it possible to use an interface type that is defined in a huge external dll, without referencing that dll at runtime?

Yes. We added that feature to C# 4. The "proper" name for the feature is something like "Type Embedding with Type Equivalence", but everyone just calls it "No PIA".

The motivation for the feature is the one faced most obviously by Visual Studio Tools For Office developers. VSTO developers write C# code that customizes, say, an Excel spreadsheet with some managed code. They communicate with Excel via a managed interface, but of course Excel actually exposes a set of COM interfaces. To bridge that gap, the Office team supplies a Primary Interop Assembly, or PIA. The PIA is a huge external library that contains nothing but metadata that describes how the managed interfaces correspond to the unmanaged interfaces of the COM objects.

The problem is that the Office team does not by default install the PIA when your customer buys Office! Therefore you have to ship the PIA with your customization. And the PIA is so large, it is often many times the size of the customization, which makes your download longer. And so on; it's not an ideal situation by any means.

The No-PIA feature allows the compiler to link only the portions of the PIA you actually use into your library, so that you do not have to ship the PIA with it.

Now, you might ask "what if I have two customizations that communicate with each other, and both use the IFoo interface from a PIA that I am not shipping?" The runtime identifies types by the assembly they came from, and so the two IFoo interfaces would be considered different types, and therefore not compatible.

The "No PIA" feature takes this into account as well. It does the same trick you use in COM to solve this problem: the assembly instructs the runtime to unify all interfaces that have the same GUID into the same logical type even if they come from different assemblies. This thereby explains the requirement that every interface that you use with "no PIA" has to be marked as though it were a COM interop interface with a GUID.

On the command line, use /L instead of /R to reference an assembly as a "no PIA" assembly.

Do a web search on "no PIA" and you'll find more information on this feature.

1
  • Type embedding only works for types with the [ComImport] attribute. "Huge external DLL" is ambiguous but odds seem low. If it is actually a pia then late binding with dynamic is an unmentioned alternative. Commented Jan 30, 2012 at 13:26
2

If you want to use that interface type in your code, that interface should be visible to your code. You code won't compile.

You can write adapter interface in your global dll, for the original interface and use that every where.

1
  • Will that work? if I understand you correctly, you're suggesting copying that interface into a namespace of mine. but don't the external dll classes implement the other interface?
    – kobi7
    Commented Jan 30, 2012 at 8:46
1

It cannot be done statically but you can do it using reflection.

0

With C# 4 you can use the dynamic keyword.

However, I fail to see how not knowing the interface in advance is going to help you - how are you going to know which methods to call?

5
  • I think you mean .net4 there is nothing named as c#4. Commented Jan 29, 2012 at 13:19
  • I do mean C# 4 (which comes with .NET 4 most of the times). dynamic is a language feature. I don't know if you can use dymanic with pre 4 versions of the CLR, though - worth a shot if you're intrigued.
    – zmbq
    Commented Jan 29, 2012 at 13:22
  • 1
    @Saeed Yeah you can call it C# 4.0
    – Omar
    Commented Jan 29, 2012 at 13:24
  • Turns out you can't, the compiler complains it's missing some types if you try to use dynamic with version 2.0 of the CLR. Makes sense.
    – zmbq
    Commented Jan 29, 2012 at 13:28
  • @zmbq The dynamic keyword isn't the best way, I think reflection can help in this case, because it's concerned with the CLR and it's more flexible.
    – Omar
    Commented Jan 29, 2012 at 13:43
0

You are trying to fool type identity. The CLR identifies a type by these properties:

  • Assembly display name
  • [AssemblyVersion]
  • [AssemblyCulture]
  • The assembly's PublicKeyToken value
  • The assembly's processor architecture (implicit)
  • The type's namespace name
  • The type's name.

Faking the type namespace name and name isn't difficult, the hard thing to do is faking the assembly properties. You are dead in the water if the assembly is strong-named (non-null PublicKeyToken) or if it is stored in the GAC, you can't get the substitute loaded. Faking the culture and architecture isn't hard to do, you'll have to get the display name and version right.

And of course, you'll have to get the interface declaration exactly right. Intentionally invoking DLL Hell like this is otherwise an Extremely Bad Idea. Not in the least because you now can never get the real assembly loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.