Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Extension Methods feature over mixed solution (managed c++ to C#) [solved]

Extension Methods feature over mixed solution (managed c++ to C#) [solved]

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++comquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin 0
    wrote on last edited by
    #1

    Hello everyone. I'm doing some research about the extension methods[^]! I was very critical as I heard about that feature and after some research my doubt, if there is a need for it, is even higher. (But that was discussed so often now, I will not waste your time and leave it open ...) Now my question (proplem)! I did a test, with a solution including: 1) C# project "Provider", which contains a class Foo and a parameterless method DoFoo (all public) 2) C# project "Extensions"(which references "Provider"), containing the extension method

    public static void DoFoo(this Foo fo, int inValue) { ...}

    3 a) C# project "UserCs" (which reference "Provider and "User") 3 b) managed C++ project "UserCpp" (which reference "Provider and "User") Also another test, with a solution including: 1) managed C++ project "Provider", ... 2) C# project "Extensions"(which references "Provider"), ... 3 a) C# project "UserCs" (which reference "Provider and "User") 3 b) managed C++ project "UserCpp" (which reference "Provider and "User") What happens in both tests now is, that in "3a" I have access to the extensions Method but not in "3b". I know that managed c++ does not provide the syntactic features (ofter heard as "syntactic sugar"), but is aware of the attribute "ExtensionAttribute", which in fact is the base technology of the extention methods. So for me, it seems that the managed c++ compiler can not deal with the build in C# feature, where the extension method is "turned" into a static method which is placed in a static class, all "surrounded" by the "ExtensionAttribute". I know that I'm lacking at the basic knowledge in manage c++, so I'm really looking forwared reading some expert statement pointing out the parts I have missed so far!

    All the best, Martin

    modified on Wednesday, January 13, 2010 6:57 AM

    N 1 Reply Last reply
    0
    • M Martin 0

      Hello everyone. I'm doing some research about the extension methods[^]! I was very critical as I heard about that feature and after some research my doubt, if there is a need for it, is even higher. (But that was discussed so often now, I will not waste your time and leave it open ...) Now my question (proplem)! I did a test, with a solution including: 1) C# project "Provider", which contains a class Foo and a parameterless method DoFoo (all public) 2) C# project "Extensions"(which references "Provider"), containing the extension method

      public static void DoFoo(this Foo fo, int inValue) { ...}

      3 a) C# project "UserCs" (which reference "Provider and "User") 3 b) managed C++ project "UserCpp" (which reference "Provider and "User") Also another test, with a solution including: 1) managed C++ project "Provider", ... 2) C# project "Extensions"(which references "Provider"), ... 3 a) C# project "UserCs" (which reference "Provider and "User") 3 b) managed C++ project "UserCpp" (which reference "Provider and "User") What happens in both tests now is, that in "3a" I have access to the extensions Method but not in "3b". I know that managed c++ does not provide the syntactic features (ofter heard as "syntactic sugar"), but is aware of the attribute "ExtensionAttribute", which in fact is the base technology of the extention methods. So for me, it seems that the managed c++ compiler can not deal with the build in C# feature, where the extension method is "turned" into a static method which is placed in a static class, all "surrounded" by the "ExtensionAttribute". I know that I'm lacking at the basic knowledge in manage c++, so I'm really looking forwared reading some expert statement pointing out the parts I have missed so far!

      All the best, Martin

      modified on Wednesday, January 13, 2010 6:57 AM

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Not sure I got your question correctly. Are you saying that from C++/CLI you are unable to use the extension method like the way you used it in C#? If yes, C++/CLI won't support extension method and you can't use it like in C#. You need to specify the class name and call the method. Just to explain, I have the following C# classes on a project.

      namespace ClassLibrary1
      {
      public class Foo
      {
      public void NormalMethod()
      {
      }
      }
      }

      namespace ClassLibrary1
      {
      public static class FooExtensions
      {
      public static void ExtensionMethod(this Foo foo)
      {
      }
      }
      }

      Here is how I used it in C++/CLI.

      using namespace System;
      using namespace ClassLibrary1;

      int main(array<System::String ^> ^args)
      {
      Foo^ f = gcnew Foo;
      f->NormalMethod(); // Extension method will not be available to call like this
      FooExtensions::ExtensionMethod(f); // Calling extension method by specifying the class name
      return 0;
      }

      C++ doesn't require extension methods. It supports free functions and that is much superior than extension methods. IMO, Extension method is introduced to give something similar to C++'s free functions. :)

      Best wishes, Navaneeth

      M 1 Reply Last reply
      0
      • N N a v a n e e t h

        Not sure I got your question correctly. Are you saying that from C++/CLI you are unable to use the extension method like the way you used it in C#? If yes, C++/CLI won't support extension method and you can't use it like in C#. You need to specify the class name and call the method. Just to explain, I have the following C# classes on a project.

        namespace ClassLibrary1
        {
        public class Foo
        {
        public void NormalMethod()
        {
        }
        }
        }

        namespace ClassLibrary1
        {
        public static class FooExtensions
        {
        public static void ExtensionMethod(this Foo foo)
        {
        }
        }
        }

        Here is how I used it in C++/CLI.

        using namespace System;
        using namespace ClassLibrary1;

        int main(array<System::String ^> ^args)
        {
        Foo^ f = gcnew Foo;
        f->NormalMethod(); // Extension method will not be available to call like this
        FooExtensions::ExtensionMethod(f); // Calling extension method by specifying the class name
        return 0;
        }

        C++ doesn't require extension methods. It supports free functions and that is much superior than extension methods. IMO, Extension method is introduced to give something similar to C++'s free functions. :)

        Best wishes, Navaneeth

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello and thank you for taking time!

        N a v a n e e t h wrote:

        Are you saying that from C++/CLI you are unable to use the extension method like the way you used it in C#?

        That's correct!

        N a v a n e e t h wrote:

        C++/CLI won't support extension method and you can't use it like in C#

        I'd like to trust you, but is there a documentation which confirmes your statement? I'm a little confused because of the supportage of the "ExtensionAttribute", which is nicely descriped in tis cp article[^]! Maybe have a short look and tell me what you think about it. Thanks again!

        All the best, Martin

        N 1 Reply Last reply
        0
        • M Martin 0

          Hello and thank you for taking time!

          N a v a n e e t h wrote:

          Are you saying that from C++/CLI you are unable to use the extension method like the way you used it in C#?

          That's correct!

          N a v a n e e t h wrote:

          C++/CLI won't support extension method and you can't use it like in C#

          I'd like to trust you, but is there a documentation which confirmes your statement? I'm a little confused because of the supportage of the "ExtensionAttribute", which is nicely descriped in tis cp article[^]! Maybe have a short look and tell me what you think about it. Thanks again!

          All the best, Martin

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Martin# wrote:

          I'd like to trust you, but is there a documentation which confirmes your statement?

          You can trust me :). BTW, I am not aware of any document that says it is not supported in C++/CLI. But if you take a look at C++/CLI's standard, you will not find anything related to extension method. And a search on the web also tells the same.

          Martin# wrote:

          I'm a little confused because of the supportage of the "ExtensionAttribute

          There is no confusion here. ExtensionAttribute is a class which is part of .NET framework. C++/CLI can use any classes in the framework and it understands attribute classes. What it doesn't understand is to look for this specific attribute and show the method as an extension. This is simply because it is not implemented in the compiler. C# compiler can look for this attribute and show the method as extension. So as the article says, if you apply extension attribute to the class and method from C++/CLI and used in a C# project, C# compiler reads the extension attribute and display it as extension method.

          Martin# wrote:

          which is nicely descriped in tis cp article[^]!

          It was a good read. Thanks for the link. :)

          Best wishes, Navaneeth

          M 1 Reply Last reply
          0
          • N N a v a n e e t h

            Martin# wrote:

            I'd like to trust you, but is there a documentation which confirmes your statement?

            You can trust me :). BTW, I am not aware of any document that says it is not supported in C++/CLI. But if you take a look at C++/CLI's standard, you will not find anything related to extension method. And a search on the web also tells the same.

            Martin# wrote:

            I'm a little confused because of the supportage of the "ExtensionAttribute

            There is no confusion here. ExtensionAttribute is a class which is part of .NET framework. C++/CLI can use any classes in the framework and it understands attribute classes. What it doesn't understand is to look for this specific attribute and show the method as an extension. This is simply because it is not implemented in the compiler. C# compiler can look for this attribute and show the method as extension. So as the article says, if you apply extension attribute to the class and method from C++/CLI and used in a C# project, C# compiler reads the extension attribute and display it as extension method.

            Martin# wrote:

            which is nicely descriped in tis cp article[^]!

            It was a good read. Thanks for the link. :)

            Best wishes, Navaneeth

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            N a v a n e e t h wrote:

            You can trust me

            ;)

            N a v a n e e t h wrote:

            But if you take a look at C++/CLI's standard, you will not find anything related to extension method. And a search on the web also tells the same.

            So true!

            N a v a n e e t h wrote:

            What it doesn't understand is to look for this specific attribute and show the method as an extension. This is simply because it is not implemented in the compiler.

            You know what. I just wanted to ask you rereading the article, but as I was looking over it once again I found out that I completely missunderstood a sentence in the section "Making an Extension Method in C++/CLI". About "ExtensionAttribute": "Applying the attributes to the proper place in the C++/CLI project, I was able to get the extension method to show up" Yes, but only with the C# compiler, and not with the C++/CLI compiler. :doh:

            N a v a n e e t h wrote:

            It was a good read. Thanks for the link.

            It seems, that I learned more new stuff from providing that article, than you reading it! :) With your help, I think I got it now! Thank you! My conclusion about the .Net implementation of extension methods: :confused: :wtf: :(

            All the best, Martin

            T 1 Reply Last reply
            0
            • M Martin 0

              N a v a n e e t h wrote:

              You can trust me

              ;)

              N a v a n e e t h wrote:

              But if you take a look at C++/CLI's standard, you will not find anything related to extension method. And a search on the web also tells the same.

              So true!

              N a v a n e e t h wrote:

              What it doesn't understand is to look for this specific attribute and show the method as an extension. This is simply because it is not implemented in the compiler.

              You know what. I just wanted to ask you rereading the article, but as I was looking over it once again I found out that I completely missunderstood a sentence in the section "Making an Extension Method in C++/CLI". About "ExtensionAttribute": "Applying the attributes to the proper place in the C++/CLI project, I was able to get the extension method to show up" Yes, but only with the C# compiler, and not with the C++/CLI compiler. :doh:

              N a v a n e e t h wrote:

              It was a good read. Thanks for the link.

              It seems, that I learned more new stuff from providing that article, than you reading it! :) With your help, I think I got it now! Thank you! My conclusion about the .Net implementation of extension methods: :confused: :wtf: :(

              All the best, Martin

              T Offline
              T Offline
              tonyt
              wrote on last edited by
              #6

              I was a little confused by your original question, but if are are trying find out if it is possible to <b>implement</b> extension methods in C++/CLI (that can be consumed from VB and C# as extension methods), the answer is yes. You just declare a C++/CLI class that is both abstract and sealed, and you apply the [Extension] attribute to both the class and the extension methods you add to it, which must be public and static. The extension methods themselves do not use the 'this' keyword like they do in C#, but they're seen as extension methods by the VB and C# compilers. The example below implements a "Matches()" method for System.String: <pre> #pragma once using namespace System; using namespace System::Runtime::CompilerServices; using namespace System::Text::RegularExpressions; namespace System {    [System::Runtime::CompilerServices::Extension]    public ref class StringExtensionMethods abstract sealed    {    public:          [System::Runtime::CompilerServices::Extension]          static bool Matches( System::String^ string, System::String^ pattern )          {             return Regex::IsMatch( string, pattern );          }    }; } </pre>

              M 1 Reply Last reply
              0
              • T tonyt

                I was a little confused by your original question, but if are are trying find out if it is possible to <b>implement</b> extension methods in C++/CLI (that can be consumed from VB and C# as extension methods), the answer is yes. You just declare a C++/CLI class that is both abstract and sealed, and you apply the [Extension] attribute to both the class and the extension methods you add to it, which must be public and static. The extension methods themselves do not use the 'this' keyword like they do in C#, but they're seen as extension methods by the VB and C# compilers. The example below implements a "Matches()" method for System.String: <pre> #pragma once using namespace System; using namespace System::Runtime::CompilerServices; using namespace System::Text::RegularExpressions; namespace System {    [System::Runtime::CompilerServices::Extension]    public ref class StringExtensionMethods abstract sealed    {    public:          [System::Runtime::CompilerServices::Extension]          static bool Matches( System::String^ string, System::String^ pattern )          {             return Regex::IsMatch( string, pattern );          }    }; } </pre>

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                tonyt wrote:

                I was a little confused by your original question

                Me too! :) Thanks for the summary, but I finally understood all that! You get my 5 for careing that much.

                All the best, Martin

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups