MistyD
MistyD

Reputation: 17223

Can I have auto-properties in a COM Interface

I currently have an interface for a COM component that is something like this:

[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("aa950e58-7c6e-4818-8fc9-adecbc7a8f14")]
    public interface MyIObjects
    {
        void set_price(float rx);
        void set_size(long rx);

        float get_price();
        long get_size();
    }

Now is there a simple shortcut that might reduce two lines of the following to one line

            void set_price(float rx);
            float get_price();

I know in classes I can do something like this

int Price { get; set; }

but will this work in an interface ?

Upvotes: 2

Views: 122

Answers (2)

Hans Passant
Hans Passant

Reputation: 941208

COM only cares about interfaces, not about their implementation. You already declare a property with a syntax that resembles an auto-property definition.

[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyObjects
{
    float price { get; set; }
    int size { get; set; }
}

How you implement the property is entirely up to you. And yes, using an auto-property in the class that implements the interface is fine.

[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class MyObjects : IMyObjects {
    public float price { get; set; }
    public int size { get; set; }
}

Note that the long type as it appears in COM type libraries or native code is the same as the int type in C#.


Here is what the IDL definition of the interface would be (assuming inside the namespace CSDllCOMServer)

[
  odl,
  uuid(AA950E58-7C6E-4818-8FC9-ADECBC7A8F14),
  version(1.0),
  oleautomation,
  custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "CSDllCOMServer.MyIObjects")

]
interface MyIObjects : IUnknown {
    [propget]
    HRESULT _stdcall price([out, retval] single* pRetVal);
    [propput]
    HRESULT _stdcall price([in] single pRetVal);
    [propget]
    HRESULT _stdcall size([out, retval] long* pRetVal);
    [propput]
    HRESULT _stdcall size([in] long pRetVal);
};

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190897

You can declare properties in interfaces just fine!

Upvotes: 0

Related Questions