108

I first time using Excel to reading data in c# with Selenium WebDriver, but when I build this code, it pops up an error:

"Missing compiler required member 'microsoft.csharp.runtimebinder.binder.convert'"

and the code for using excel is marked in red bellow:

 excel.Application x1Appl = new excel.Application();
 excel.Workbook x1WorkBook = x1Appl.Workbooks.Open(@"C:\app\o\SearchBy.xlsx");

 excel._Worksheet x1WorkSheet = x1WorkBook.Sheets[1];

Please let me know what is missing? Thank you!

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Excellent
  • 1,081
  • 2
  • 7
  • 3
  • 1
    MissingMethodException is a DLL Hell problem. Very unusual for Microsoft.CSharp.dll, there have not been a lot of versions of it. Use Fuslogvw.exe and log all binds, show us the trace you get. – Hans Passant Apr 03 '18 at 20:39

7 Answers7

184

The reference assemblies for Office are exposed via the dynamic return type. To be able to compile you need to add a reference to Microsoft.CSharp.dll.

Alex Ghiondea - MSFT
  • 3,182
  • 1
  • 8
  • 11
  • 1
    This works like a charm, I was having this issue while running a UnitTest that returns "dynamic" type. Thanks. – Junaid Khan Oct 23 '18 at 11:09
  • 32
    For those who are passing by here. My newly created .NET Standard package had this build error so I added the [Microsoft.CSharp Nuget package](https://www.nuget.org/packages/Microsoft.CSharp/) and **then closed and reopened Visual Studio**! – Jérôme MEVEL Nov 13 '18 at 09:37
  • I had the issue after casting in a UnitTest. Thanks. – Peter van Kekem Aug 09 '19 at 11:01
86

In addition to what @Alex Ghiondea says, go to the references section of your project:

Right click on references and check the prompted options.

  1. Click on add reference and a modal with the left menu (assemblies, projects, COM and browse) will appear.
  2. Click Assemblies
  3. Check Microsoft.CSharp and click Ok.
  4. Clean and build your project and the error should disappear.

enter image description here

eduardo92
  • 1,085
  • 6
  • 9
56

If your project is targeting .Net Core or .Net Standard, then installing the Microsoft.CSharp NuGet package will solve this error.

Kols
  • 3,641
  • 2
  • 34
  • 42
31

Add a reference of Microsoft.CSharp to your project by using NuGet.

enter image description here

or

Install-Package Microsoft.CSharp -Version 4.7.0 for the project

Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
10

right click on project name (in solution explorer), Add refrence : Microsoft.CSharp in the assemblies , then right click again and Clean. that's all.

Sharif Lotfi
  • 544
  • 6
  • 13
8

I'm using Visual Studio 2017 Version 15.7.1 (not sure if this matters or not, but this error seems to have cropped up after I updated). I had a project that was targeting .NET Framework 3.5. So, in addition to the other answers provided for adding Microsoft.CSharp, I needed to update this project to .NET Framework 4.5, and then Microsoft.CSharp showed up under Assemblies when I went to add the reference. Before then, I had to find the absolute path to the DLL, which didn't seem to work.

For those who can't upgrade to 4.5, you can try setting EmbedInteropTypes to False for all interop references in your csproj file, as shown here: http://answers.flyppdevportal.com/MVC/Post/Thread/b1554cdd-ad9e-4453-b4d6-8eb03da175ea?category=visualstudiogeneral

brrrrth
  • 177
  • 1
  • 10
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/19745667) – Laurent S. May 16 '18 at 13:32
  • 4
    I had the exact same question as this and had to do extra things to fix it, hence the extra response. Also, turns out I can't update the one project without updating the rest of the solution, which is a no go, hence the edit with the alternative solution which isn't provided elsewhere. – brrrrth May 16 '18 at 13:49
  • 2
    Thanks a lot for your message. It's been hours I'm looking for a solution for a 3.5 project. – nkoniishvt May 25 '18 at 13:14
  • `EmbedInteropTypes = False` was exactly what I needed - many thanks! – RichieHindle Jun 03 '18 at 19:59
1

I had this problem as well. If you right click and select Properties (While project is highlighted), there is a checkbox that says: Auto-generate binding redirects. This fixed it for me.

I am using Visual Studio 2017 and it is a C# class library.

Pat
  • 11
  • 1