缺少的编译器需要成员‘ microsoft.csharp.runtimebinder.binder.Convert’

我第一次使用 Excel 在 Selenium WebDriver 中读取 c # 中的数据,但是当我构建这段代码时,它会弹出一个错误:

”缺少编译器需要成员‘ microsoft.csharp.runtimebinder.binder.Conver’”

使用 excel 的代码用红色的波纹管标明:

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


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

请让我知道缺少什么? 谢谢!

83391 次浏览

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.

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

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

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

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.

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

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