The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,782 other followers

.NET/C#: Resolved errors “The type ‘NameSpace.TypeNameClass’ has no constructors defined” and “Interop type ‘NameSpace.TypeNameClass’ cannot be embedded. Use the applicable interface instead.” (via Stack Overflow: Interop type cannot be embedded)

Posted by jpluimers on 2012/03/20

When moving the Microsoft Scripting Runtime interop code from .NET 1.x to 4.x, I got these errors:

Error1: The type 'Scripting.FileSystemObjectClass' has no constructors defined
Error21: Interop type 'Scripting.FileSystemObjectClass' cannot be embedded. Use the applicable interface instead.

Though the first answers on the question seem to adequately resolve the problem, they merely cure the symptom: turning off the embedding of the PIA (Primary Interop Assembly).

The below answer by Michael Gustus (which only has a few votes, so please vote it up) actually explains what is going on, and solves the cause:

In most cases this error is the result of code which tries to instantiate a COM object e.g. here piece of code starting up Excel:

Excel.ApplicationClass xlapp = new Excel.ApplicationClass();

Typically, in .Net 4 you just need to remove the ‘Class’ suffix and compile the code:

Excel.Application xlapp = new Excel.Application();

MSDN explanation here.

Hence I like the comment by Tyrsius on this answer as well:

This was more useful than the marked answer, as I needed the functionality of the embedded Interop. This solved both problems, thank you!

The above answer tells you to not use the class type, but the interface type, just like the error states. And the answer implicitly tells you the class type is ApplicatoinClass, and the interface is Application.

These are the declarations for the PIA interface:

using System.Runtime.InteropServices;

namespace Scripting
{
    [Guid("2A0B9D10-4B87-11D3-A97A-00104B365C9F")]
    [CoClass(typeof(FileSystemObjectClass))]
    public interface FileSystemObject : IFileSystem3
    {
    }
}

and the class (partial, hence the //… comment):

using System;
using System.Runtime.InteropServices;

namespace Scripting
{
    [ClassInterface(0)]
    [Guid("0D43FE01-F093-11CF-8940-00A0C9054228")]
    [TypeLibType(2)]
    public class FileSystemObjectClass : IFileSystem3, FileSystemObject
    {
        public FileSystemObjectClass();

        [DispId(10010)]
        public virtual Drives Drives { get; }

        [DispId(10000)]
        public virtual string BuildPath(string Path, string Name);
        //...
        [DispId(1100)]
        public virtual TextStream OpenTextFile(string FileName, IOMode IOMode = IOMode.ForReading, bool Create = false, Tristate Format = Tristate.TristateFalse);
    }
}

For .NET 4.0, it is enough to use the interface type, as that links to the underlying class by means of the CoClass attribute.

As a result, I used FileSystemObject in stead of FileSystemObjectClass, and now it compiles fine, without changing the embedding flag.

–jeroen

via: c# – Interop type cannot be embedded – Stack Overflow.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: