137

In Visual Studio, when adding one reference to the project, the properties window has an option Embed Inteop Types, should we set it to True or False? What's the difference?

Since we have a lot of projects, among some of them, reference was set to False, others were set to True, it is totally mess up. And the bulid server also have the same warnings:

What does “reference was created to embedded interop assembly” mean?

So we plan to change all the Embed Inteop Types to False, what risk would we get?

Community
  • 1
  • 1
Jerry Bian
  • 3,998
  • 6
  • 29
  • 54
  • 4
    Never do this, it is a very awesome feature that solves irksome deployment details. You have to understand COM to truly grok what this is all about. – Hans Passant Dec 11 '13 at 11:24
  • @HansPassant, are you saying never set them `false` or never change them (e.g. if they default to `false` already)? – noelicus Sep 30 '14 at 12:58
  • 3
    He plans to set it to False. Never do this. – Hans Passant Sep 30 '14 at 13:00
  • 3
    @HansPassant how comes you use irksome and grok. it's very brittish :D – Mafii Jul 14 '16 at 10:29
  • 4
    @Mafii I thought grok was from Stranger in a Strange Land (which is American). Doesn't mean the British might not use it more, of course! – StayOnTarget Jun 30 '17 at 13:37
  • Grok was indeed invented by Heinlein but it was quite common in the 80s and 90s for UK techs\programmers to use it. – Alan B Jul 17 '20 at 09:05
  • @HansPassant can you expand on why he should never do this? Can you explain or suggest resources so that we can 'truly grok what this is all about'? – Hugh W Oct 31 '22 at 08:54

2 Answers2

88

This option was introduced in order to remove the need to deploy very large PIAs (Primary Interop Assemblies) for interop.

It simply embeds the managed bridging code used that allows you to talk to unmanaged assemblies, but instead of embedding it all it only creates the stuff you actually use in code.

Read more in Scott Hanselman's blog post about it and other VS improvements here: CLR and DLR and BCL, oh my! - Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1.

As for whether it is advised or not, I'm not sure as I don't need to use this feature. A quick web search yields a few leads:

The only risk of turning them all to false is more deployment concerns with PIA files and a larger deployment if some of those files are large.

ChrisW
  • 54,973
  • 13
  • 116
  • 224
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • 1
    Note that there is a rather significant difference that may break existing code when turning *Embed Interop Types* off. The difference is mentioned [here](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/how-to-access-office-onterop-objects): *“C# 4, and later versions, converts the returned Object to dynamic automatically if the assembly is referenced by the /link compiler option or, equivalently, if the Excel Embed Interop Types property is set to true. True is the default value for this property.”* – Dirk Vollmar Mar 01 '18 at 10:45
  • 2
    Embedded interop assemblies gave me trouble in a plug-in-based system, in which both the host and the plugins depended on the same *COM*-object. Another problem was merging such assemblies with *ILMerge*. – Anton Shepelev Aug 04 '20 at 13:37
9

I noticed that when it's set to false, I'm able to see the value of an item using the debugger. When it was set to true, I was getting an error - item.FullName.GetValue The embedded interop type 'FullName' does not contain a definition for 'QBFC11Lib.IItemInventoryRet' since it was not used in the compiled assembly. Consider casting to object or changing the 'Embed Interop Types' property to true.

user890332
  • 1,315
  • 15
  • 15