Managed method in native class

Is possible to have a managed method within a Native(un-managed) class within a \clr project

E.g.

class  myClass
{
public:
       #pragma managed
       void myMethod(void);

};

I get an error when attempting to compile above about #pragma managed must be used at global scope!

Is there some way to achieve this
If not, why not

Thanks in advance.



Answer this question

Managed method in native class

  • Drir Sinan

    If i compiled all my unmanaged classes as \clr and then marked all the current methods with #pragma unmanaged and then added my new method which is implicitly mixed mode then would work.

    But this would be a mountain of work because of header dependencies etc. 

    I really require the inverse of this...

    I need to iterate through a large collection of MFC derived objects via a common interface, something like:

    void   Convert2DotNet( DotNetConversionContext& );

    For each MFC class implementing this method, this method will be compiled managed, all the other methods will remain native.

    In this method i will reference native member data and create new managed classes, these new classes will eventually be serialized in .NET format.

    If its not possible then i know i have to come up with some horrid functional solution - which is alot of work also, much of it messy.

    If its not currently possible, i dont see why this cannot be made available as it would be the most expressive for me.


    Thanks.




  • underdog

    Its interesting to note that given

    //myClass.h

    class  myClass
    {
    public:
           void myMethod(void);

    };

    And the following module compiled as \clr

    // myClass.cpp
    #pragma unmanaged
    void myClass::myMethod(void)
    {

    }

    works correctly, if i put any managed data in here, will generate
    errors.

    But if i compile the module unmanaged with a \clr project and then

    // myClass.cpp
    #pragma managed
    void myClass::myMethod(void)
    {

    }

    Will not work. Big shame for me!
    Why cannot this be made to work

    Please help...


  • Daniel.K

    In a nutshell Brandon.

    I have since resolved this problem, exactly as you have stated.
    Thanks.

  • Deforge

    If I understand C++/CLR correctly, classes must be either managed or unmanaged, they can't be mixed. If you want to mix them, you require the use of external helper functions or maybe you can mix managed and unmanaged in managed classes, I'm not too sure on this point though.



  • DRD

    The /clr switch enables functions to be managed. If the individual file is not compiled with /clr, then anything inside that file cannot contain or use managed code.

    If you want a particular member function to be managed, you can define it outside of the class with #pragma managed, as you've already shown. If other parts of the file should not contain managed code, I'd suggest moving that function to another file that can be compiled with /clr.

  • Managed method in native class