I know that in C# in general you can't create an instance of an abstract class or an interface. Can someone help me understand this code (it compile without any errors).
Shell32.Shell shObj = new Shell32.Shell();
Shell32.Shell
is an interface from 'shell32.dll'
I tried the following, but it did NOT compile:
[CoClass(typeof(ShellClass))]
[Guid("286E6F1B-7113-4355-9562-96B7E9D64C54")]
public interface OtherShell : Shell
{
}
OtherShell shObj = new OtherShell();
Update:
To make it work, I just needed to add the ComImport
attribute and change the co-class (I can't choose Shell32.ShellClass).
Thanks guys!
In addition to all the other information, here is the how it results in compiled code. The usage of the interface is merely a code nicety.
That is, it is a compile-time transformation from the interface "to" the class, based on the [CoClass] attribute.
Per, https://stackoverflow.com/a/1094140/2864740, which shows a minimal example case: