gpt4 book ai didi

c++ - 如何为 WinRT (ref) 类覆盖 IInspectable::GetRuntimeClassName()

转载 作者:行者123 更新时间:2023-11-28 07:08:58 25 4
gpt4 key购买 nike

根据 WinRT 规范,私有(private)类应该覆盖 IInspectable::GetRuntimeClassName()并返回一个不同的名称(或 NULL)。但是由于 IInspectable 不是一个 ref 类(它是一个结构),我不能从它继承,也不能重写这些方法。

从 MSDN 定义:

HRESULT GetRuntimeClassName(
[out] HSTRING *className
);

所以问题是,如何为 WinRT (ref) 类覆盖 IInspectable::GetRuntimeClassName()

示例尝试:

#include "Hstring.h"
#include "Inspectable.h"
#include <sal.h>

namespace PhoneVoIPApp
{
namespace BackEnd
{
namespace CallController
{
public interface class IHelloWorld
{
Platform::String^ SayHello();
};

private ref class HelloWorldImpl sealed :IHelloWorld
{
public:
virtual Platform::String^ SayHello();

virtual HRESULT GetRuntimeClassName(_Out_ HSTRING *className) override;
};

Platform::String^ HelloWorldImpl::SayHello()
{
return L"Hello World!";
}

HRESULT HelloWorldImpl::GetRuntimeClassName(_Out_ HSTRING *className)
{
*className = nullptr;

return E_NOTIMPL;
}
}
}
}

最佳答案

我与 CLR 团队进行了交谈,他们为我指出了正确答案。

当使用私有(private)类时,可以将一个 secret 的未记录属性 Platform::Metadata::RuntimeClassName 添加到代码中。

所以问题的解决方法是:

#include "Hstring.h"
#include "Inspectable.h"
#include <sal.h>

namespace PhoneVoIPApp
{
namespace BackEnd
{
namespace CallController
{
public interface class IHelloWorld
{
Platform::String^ SayHello();
};

private ref class HelloWorldImpl sealed : [Platform::Metadata::RuntimeClassName]IHelloWorld
{
public:
virtual Platform::String^ SayHello();
};

Platform::String^ HelloWorldImpl::SayHello()
{
return L"Hello World!";
}
}
}
}

关于c++ - 如何为 WinRT (ref) 类覆盖 IInspectable::GetRuntimeClassName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com