Reputation: 1491
I am trying to compile someone else's project. They wrote code that looks like this:
var doc = new Microsoft.Office.Interop.Word.Document(pathToFile);
I can compile it fine in Visual Studio 2010 (where it was developed), but when I compile the same project in Visual Studio 2015, it gives this error:
'Document' does not contain a constructor that takes 1 arguments
In this case, Document is an interface:
[Guid("0002096B-0000-0000-C000-000000000046")]
[CoClass(typeof(DocumentClass))]
public interface Document : _Document, DocumentEvents2_Event { }
And the CoClass looks like this:
[ComSourceInterfaces("Microsoft.Office.Interop.Word.DocumentEvents2")]
[Guid("00020906-0000-0000-C000-000000000046")]
[TypeLibType(2), ClassInterface(0)]
public class DocumentClass : _Document, Document, DocumentEvents2_Event, DocumentEvents_Event
{
public DocumentClass();
[DispId(-2147418112)] public virtual string _CodeName { get; set; }
...
}
The definitions looks identical in the metadata view from both versions of Visual Studio, and both are loading the interop file from:
C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll
In neither case does DocumentClass contain a non-default constructor, but it compiles in Visual Studio 2010.
Why does it compile in 2010? And given that it does compile in 2010, why doesn't it compile anymore in 2015?
And how can I discover the equivalent invocation for Visual Studio 2015 to make it work?
Upvotes: 2
Views: 500
Reputation: 1491
Based on @roryap test in the comments, it looks like Visual Studio 2010 was allowing invalid calls to the COM interop constructor.
I compiled the code in Visual Studio 2010 and used ILSpy to see what it generated:
doc = (Microsoft.Office.Interop.Word.Document)Activator.CreateInstance(
Type.GetTypeFromCLSID(
new Guid("00020906-0000-0000-C000-000000000046")));
The file path is nowhere to be seen. Visual Studio 2015 must have fixed the bug, and it no longer ignores constructor arguments like it used to.
Upvotes: 2