15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using a .neT Component from a CoM Client ❘ 717<br />

aTTribuTe<br />

InterfaceType<br />

ClassInterface<br />

DispId<br />

In Out<br />

Optional<br />

desCriPTion<br />

This attribute, if set to a ComInterfaceType enumeration value, enables<br />

you to modify the default dual interface type that is created for .<strong>NET</strong> interfaces.<br />

ComInterfaceType has the values InterfaceIsDual, InterfaceIsIDispatch,<br />

<strong>and</strong> InterfaceIsIUnknown. If you want to apply a custom interface type to a<br />

.<strong>NET</strong> interface, set the attribute like this: InterfaceType(ComInterfaceType<br />

.InterfaceIsIUnknown).<br />

This attribute enables you to modify the default dispatch interface that is created<br />

for a class. ClassInterface accepts an argument of a ClassInterfaceType<br />

enumeration. The possible values are AutoDispatch, AutoDual, <strong>and</strong> None. In the<br />

previous example, you have seen that the default is AutoDispatch, because a dispatch<br />

interface is created. If the class should only be accessible by the defined interfaces,<br />

apply the attribute ClassInterface(ClassInterfaceType.None) to the class.<br />

This attribute can be used with dual <strong>and</strong> dispatch interfaces to define the DispId of<br />

methods <strong>and</strong> properties.<br />

COM allows the specification of attributes for parameter types if the parameter<br />

should be sent to the component In, from the component to the client Out, or in both<br />

directions In, Out.<br />

Parameters of COM methods may be optional. Parameters that should be optional can<br />

be marked with the Optional attribute.<br />

Now, you can change the <strong>C#</strong> code to specify a dual interface type for the IWelcome interface <strong>and</strong> a custom<br />

interface type for the IMath interface. With the class DotnetComponent, the attribute ClassInterface<br />

with the argument ClassInterfaceType.None specifies that no separate COM interface will be generated.<br />

The attributes ProgId <strong>and</strong> Guid specify a ProgID <strong>and</strong> a GUID:<br />

[InterfaceType(ComInterfaceType.InterfaceIsDual)]<br />

[ComVisible(true)]<br />

public interface IWelcome<br />

{<br />

[DispId(60040)]<br />

string Greeting(string name);<br />

}<br />

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]<br />

[ComVisible(true)]<br />

public interface IMath<br />

{<br />

int Add(int val1, int val2);<br />

int Sub(int val1, int val2);<br />

}<br />

[ClassInterface(ClassInterfaceType.None)]<br />

[ProgId("Wrox.DotnetComponent")]<br />

[Guid("77839717-40DD-4876-8297-35B98A8402C7")]<br />

[ComVisible(true)]<br />

public class DotnetComponent: IWelcome, IMath<br />

{<br />

public DotnetComponent()<br />

{<br />

}<br />

code snippet DotnetServer/DotnetServer.cs<br />

Rebuilding the class library <strong>and</strong> the type library changes the interface definition. You can verify this with<br />

OleView.exe. IWelcome is now a dual interface, IMath a custom interface that derives from IUnknown<br />

instead of IDispatch, <strong>and</strong> the coclass section no longer has a _DotnetComponent interface.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!