A new interface?

Source: Internet
Author: User

If someone asks you, can I create a new interface in C ?, What would you do?

 


If ITestInterface is an interface, is there a problem with such code?


ITestInterface testInterface = new ITestInterface ();


Many books will say that, of course, there is a problem, the interface cannot use new, and then you will think that the above statement must not pass the compilation of the compiler.

 

But there is no such thing as absolute. C # should allow you to write this. Of course, you need to add some "material.

 

Create a console program CA2005. add Microsoft. Office. Interop. Excel reference in VS2005

 

The Main function of Program has only one sentence:

 

Note: You can check the definition of the Application through compilation:

 

Obviously, the Application is an interface,

Here I want to talk about it. I often see some people say that string is a class or a structure or something. Let's look at the definition of string:

 

String is modified by class, so string 100% is a class.


Let's pull it back. Application is an interface, but we can use new. Why?


Let's take a look at the decompiled code:

 

We can see that although we write new Application, the compiler generates new ApplicationClass () for us ();


Is there anything special about the Application?

 

After careful consideration, we can see that the Application is modified by these two features:

 

[CoClass (typeof (ApplicationClass)]

 

[Guid ("000208D5-0000-0000-C000-000000000046")]

 

For more information about CoClass, see msdn:

 

Some people do not like msdn, but one of the reasons for reading blogs is that msdn is too straightforward.

 


My personal understanding is that CoClass is like concrete Class (specific Class ).

 


This feature instructs the compiler to use ApplicationClass to compile the Application.

 

 

Back to the above initial issue:

 

 

 

 

How to compile this Code:


ITestInterface testInterface = new ITestInterface ();

 

Through the above analysis, we can easily modify our own interfaces with this feature:

 

 

Namespace CA2005

 


{

 


[CoClass (typeof (TestClass)]

 


[Guid ("6C8BF7FE-1F6B-437E-BCC8-6D2FF04E66B3")]

 


Public interface ITestInterface

 

 

{

 


Void DoSomething ();

 


}
 

 

[Guid ("68C7CB18-0DEE-4689-845D-741525281C76")]

 


Public class TestClass: ITestInterface

 

 

{

 


Public void DoSomething ()

 


{

 


Console. WriteLine ("TestClass: DoSomething ");

 


}

 


}

 

 

Class Program

 

 

{

 


Static void Main (string [] args)

 


{

 


Microsoft. Office. Interop. Excel. Application excelApplication =

 


New Microsoft. Office. Interop. Excel. Application ();
 

 

ITestInterface testInterface = new ITestInterface ();

 


TestInterface. DoSomething ();

 

 

}

 


}

 


}

 

 


The result is as follows:

 

The interface is marked with coclassattriattribute instead of ComImportAttribute.

 

In the past, a new interface was intended to use the compiler's optimization and support for COM.


Obviously, the above Application is a COM object, so you can use the new Application


Add the ComImportAttribute feature to ITestApplication:

 

Run the command again and the result is as follows:

 

View the decompiled code:

 

The reason why I use the red font for VS2005 is that if you use the program created in VS2010, you will see different decompilation results:

 


Public static void Main ()

 


{

 


Application application1 = (Application) Activator. CreateInstance (Type. GetTypeFromCLSID (new Guid ("00024500-0000-0000-c000-0000000000000046 ")));

 


ITestInterface interface1 = new TestClass ();

 


Interface1.DoSomething ();

 


Console. ReadLine ();

 


}

 


Here, the guid in Type. GetTypeFromCLSID is the Guid of ApplicationClass, that is, the Guid of Type in CoClass:

 

 

[ComSourceInterfaces ("Microsoft. Office. Interop. Excel. AppEvents")]

 


[Guid ("00024500-0000-0000-C000-000000000046")]

 


[TypeLibType (2)]

 


[ClassInterface (0)]

 


Public class ApplicationClass: _ Application, Application, AppEvents_Event

 

 

{

 


}

 


Pay attention to this.

Author: LoveJenny
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.