locked
extension method in C++

    Question

  • In C# I can write extension method such as

    public static class Ext
    {
      public static int GetID(this object obj) { return 0; }
    }

    And then I can "call" this method on that object, i.e. myObj.GetID().

    This is just a compiler trickery but it's quite handy!
    It has the added advantage to define a fake "implementation" on an interface!!!

    Now I'm writing a C++ component that is meant to be used in C#.
    I am writing a utility class to provide to utility method for some of my interfaces and I wonder if it's possible to expose them as extension method as well? 

    Monday, February 4, 2013 12:28 PM

Answers

  • C++ doesn't have the concept of extension methods. They are one of the advantages to C#.

    Come to think of it I might write the extension class in C#, calling the utility C++ class. Unelegant but functional....

    This is what the extension methods for the Windows Runtime classes do. They are .Net methods to wrap the underlying projections.

    --Rob

    • Marked as answer by Lloyd Tuesday, February 5, 2013 9:28 AM
    Tuesday, February 5, 2013 8:15 AM
    Owner
  • Actualy, you can write an extention method in C++/CLI and use it in C#. The example follows.

    Suppose you have a class 'ExtendingClass'.

    In C++, you write the following extentions class

    #pragma once
    
    using namespace	System;
    using namespace	System::Runtime::CompilerServices;
    
    namespace	Vendor { namespace	Project { namespace Extentions
    {
    	[ExtensionAttribute()]
    	public ref class	ExtentionMethods abstract sealed
    	{
    	public:
    		[ExtensionAttribute()]
    		static void	Extention1(ExtendingClass ^ instance)
    		{
    			throw gcnew Exception(String::Format("The Extention1 work's for the instance {0}.", instance->GetHashCode()));
    		}
    	};
    
    } } }

    Now you can use the extention in C# just as usual.

    using Vendor.Project.Extentions;
    
    class ExtentionComsumer
    {
       void UseExnention1()
       {
          _instance.Extention1();
       }
       ExtendingClass _instance;
    }


    • Proposed as answer by _hyperdiamond_ Friday, September 6, 2013 7:02 AM
    • Marked as answer by Lloyd Friday, September 6, 2013 10:24 AM
    • Edited by _hyperdiamond_ Friday, September 6, 2013 11:03 AM refined that it's possible under C++/CLI
    Friday, September 6, 2013 6:59 AM

All replies

  • Hi,

    I think you want to write a static member function in C++/CX. If so , please follow these codes.

      public ref class KnownValues sealed
        {
        public:
            static int GetZero() { return 0; }
        private:
            KnownValues(); // This type can't be constructed
        };
    http://blogs.msdn.com/b/vcblog/archive/2012/10/19/cxxcxpart04staticmemberfunctions.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 5, 2013 7:29 AM
  • Thanks Jesse but it doesn't help!
    I'm writing a DirectX wrapper (http://directwinrt.codeplex.com), ID2D1Geometry(ies) have so many method I don't want to implement them on each wrapped component (seeing as they can't inherit from each other!)

    ID2D1Geometry* are wrapped into a IGeometry^

    So I (already) have a "static class" Geometry with method common to all ID2D1Geometry, such as:
    static Geometry::Length(IGeometry^);

    But in my C# code I can't write

    IGeometry g = ...
    g.Length()

    I have to write
    IGeometry g;
    var l = Geometry.Length(g)

    This is cumbersome...
    Come to think of it I might write the extension class in C#, calling the utility C++ class. Unelegant but functional....

    Tuesday, February 5, 2013 7:44 AM
  • C++ doesn't have the concept of extension methods. They are one of the advantages to C#.

    Come to think of it I might write the extension class in C#, calling the utility C++ class. Unelegant but functional....

    This is what the extension methods for the Windows Runtime classes do. They are .Net methods to wrap the underlying projections.

    --Rob

    • Marked as answer by Lloyd Tuesday, February 5, 2013 9:28 AM
    Tuesday, February 5, 2013 8:15 AM
    Owner
  • I see...
    I was hoping there was some kind of attribute I can tag my method parameter wih to make it an extension method!! ^^

    Well, I was not really holding my breath, going the tedious way I guess...

    Tuesday, February 5, 2013 9:28 AM
  • Actualy, you can write an extention method in C++/CLI and use it in C#. The example follows.

    Suppose you have a class 'ExtendingClass'.

    In C++, you write the following extentions class

    #pragma once
    
    using namespace	System;
    using namespace	System::Runtime::CompilerServices;
    
    namespace	Vendor { namespace	Project { namespace Extentions
    {
    	[ExtensionAttribute()]
    	public ref class	ExtentionMethods abstract sealed
    	{
    	public:
    		[ExtensionAttribute()]
    		static void	Extention1(ExtendingClass ^ instance)
    		{
    			throw gcnew Exception(String::Format("The Extention1 work's for the instance {0}.", instance->GetHashCode()));
    		}
    	};
    
    } } }

    Now you can use the extention in C# just as usual.

    using Vendor.Project.Extentions;
    
    class ExtentionComsumer
    {
       void UseExnention1()
       {
          _instance.Extention1();
       }
       ExtendingClass _instance;
    }


    • Proposed as answer by _hyperdiamond_ Friday, September 6, 2013 7:02 AM
    • Marked as answer by Lloyd Friday, September 6, 2013 10:24 AM
    • Edited by _hyperdiamond_ Friday, September 6, 2013 11:03 AM refined that it's possible under C++/CLI
    Friday, September 6, 2013 6:59 AM
  • mm... you should link with mscorlib or something? :~ 
    The compiler doesn't know [System::Runtime::CompilerServices::ExtensionAttribute()]

    mmm... thinking..

    but thanks for the tip! ;)

    Friday, September 6, 2013 9:43 AM
  • Well, the exmaple is not turnkey, of course.

    Indeed, it seems that the attribute is defined in the mscorlib.dll.

    However, C# says that it's defined in "System.Core.dll". But my C++ project references only System.dll, System.Data.dll and System.Xml.dll.

    So, the attribute is out there...


    Friday, September 6, 2013 10:20 AM
  • I mean my C++ project only reference native library, and though I do write with my C# project in mind as consumer (indeed I'm not even creating a .lib anyway) it is moot, but I'm loathe to add .NET dependencies at this level...

    Even though, it might not really matter...

    Friday, September 6, 2013 10:25 AM
  • The article states that under ".NET for Windows Store apps" the attribute is only supported starting with Windows 8. Maybe that's the reason why your compiler doesn't know it.
    Friday, September 6, 2013 10:26 AM
  • Mm.. is it C++ CLI?

    I'm trying to use that in earnest in a simple sample C++Cx project and I just cant... :~

    In fact my project doesn't have any .NET reference! And I can't can't seem to be able to add one...

    Friday, September 6, 2013 10:40 AM
  • Yes, it's about C++/CLI. Sorry for misleading then...
    Friday, September 6, 2013 10:49 AM