2

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 ?

6
  • You mean a Visual Studio feature/shortcut? Commented May 23, 2013 at 16:53
  • Yes a visual studio feature or a macro (but C# doesnt have macros) Commented May 23, 2013 at 16:54
  • I think Resharper has something along that lines, but I can't comment for sure. Commented May 23, 2013 at 16:55
  • Oh, if your question is only "can I have auto-properties in an interface", then the answer is "yes". Thought you were looking for a keyboard/function shortcut helper in Visual Studio that would do it automatically (like Refactor -> Extract Method) and update all references/usages to use standard get/set syntax rather than invoking the non-existent methods. Commented May 23, 2013 at 16:56
  • @ChrisSinclair I believe his question is "Can I have auto-proerties in a COM Interface", is the answer still "Yes" in that case? Commented May 23, 2013 at 16:58

2 Answers 2

1

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);
};
Sign up to request clarification or add additional context in comments.

1 Comment

You covered really well what I was going to go over in my answer (you just beat me to it) I added in the IDL definition for the sample interface in to your answer (I was going to put in my own answer).
0

You can declare properties in interfaces just fine!

2 Comments

I believe his question is "Can I have auto-proerties in a COM Interface", does the fact that this will be used for COM and possibly consumed by non .NET programs affect the answer? (I am not saying it does, I just think that factor needs clarification)
How would the implementation of this interface look like if I used an auto-property ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.