It’s not what you do, but the way that you do it

Take a simple few lines of C#.


static
void Main()
{
 
A foo = new A();
}

If it compiles, then it can’t fail at runtime, right?

C# has got some extensions to make it "easier" to deal with legacy COM components (though I’m not sure I believe that). If an interface is marked with appropriate attributes then you’re allowed to use new on the interface type and the compiler will, behind the scenes, transform this instantiation into a construction of another class. The compiler has no problem with this class being unrelated to the interface that we started with.

[System.Runtime.InteropServices.

CoClass(typeof(Test)),
 System.Runtime.InteropServices.
ComImport,
 System.Runtime.InteropServices.
Guid("00000000-0000-0000-0000-000000000000")]
public interface A
{
}

public class Test
{
}

 

There’s some discussion of this here on Stack Overflow.

 

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s