简体   繁体   中英

Can a C# class inherit from a C++/cx class

I have defined a class in a "windows runtime component" project in c++/cx (UWP project). And I want to inherit that class in a c# UWP project

namespace WindowsRuntimeComponent1
{
    public ref class NetCapability
    {
    protected:
        virtual Platform::String^ GetName() { return L"NetCapability"; }

        virtual void OnConnected() {};
        virtual void OnDisconnected() {};
    };
}

when a C# class inherits from NetCapability , compiles it and errors occured

WindowsRuntimeComponent1::NetCapability': A WinRT 'public ref class' must either be sealed or derive from an existing unsealed class

and the virtual function cannot be public.

So a c# class cannot inherit from a c++/cx public class??

You should expose it via c++/CLI approach.

Check this link .

Cheers,

No, it cannot, directly. Though there are indirect ways of mimicking inheritance in some cases. Essentially this involves creating a C# class that wraps the methods exposed by the C++ class, so it's not exactly a clean and straightforward approach. See: https://devblogs.microsoft.com/cppblog/inheriting-from-a-native-c-class-in-c/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM