Javascript无法做动态类型转换 [英] Javascript unable to do dynamic type cast
问题描述
看起来WinRT中的javascript在调用WINRT DLL组件时无法进行动态类型转换。这是一个简单的例子:
It looks like javascript in WinRT is unable to do dynamic type cast when calling into WINRT DLL component. Here is a simple example:
//在js / default.js中,我添加了以下几行:
// In js/default.js, I added the following lines:
+ var testObj = new WinRTComponentDll.Test();
+ var derivedClass = new WinRTComponentDll.DerivedClass();
+ testObj.testTypeDynamicCast(derivedClass);
+ var testObj = new WinRTComponentDll.Test();
+ var derivedClass = new WinRTComponentDll.DerivedClass();
+ testObj.testTypeDynamicCast(derivedClass);
这里我们传递DerivedClass对象指针,尽管该方法是使用BaseClass类型的参数定义的。运行应用程序时,javascript无法动态地将DerivedClass类型转换为BaseClass类型,抛出"类型不匹配"。例外:
Here we pass a DerivedClass object pointer although the method is defined with an argument of BaseClass type. When running the app, javascript fails to dynamically cast DerivedClass type to BaseClass type, throws a "Type mismatch" exception:
" ms-appx://e10bc6bb-8304-4b2a-a9a4-7311b5807830-4sdaedqydyjdm/js/default.js第33行第5行未处理的异常>
0x800a000d - JavaScript运行时错误:类型不匹配
文件:default.js,行:33列:5"
"Unhandled exception at line 33, column 5 in ms-appx://e10bc6bb-8304-4b2a-a9a4-7311b5807830-4sdaedqydyjdm/js/default.js
0x800a000d - JavaScript runtime error: Type mismatch
File: default.js, line: 33 column: 5"
BaseClass,DerivedClass和Test类的定义如下:
$
///////////////////////// ///////////////////////////////////////
The definition of the BaseClass, DerivedClass and Test classes is as follows:
////////////////////////////////////////////////////////////////
// WinRTComponent.h
// WinRTComponent.h
#pragma once
#pragma once
命名空间WinRTComponentDll
{
  ;  公共参考级别BaseClass
{
公开:
$
BaseClass();
};
namespace WinRTComponentDll
{
public ref class BaseClass
{
public:
BaseClass();
};
public ref class DerivedClass sealed:public BaseClass
{
公开:
$
DerivedClass();
};
public ref class DerivedClass sealed : public BaseClass
{
public:
DerivedClass();
};
公共参考等级测试密封
{
public:
void TestTypeDynamicCast(BaseClass ^ base){}
};
};
public ref class Test sealed
{
public:
void TestTypeDynamicCast (BaseClass^ base) {}
};
};
// WinRTComponent.cpp >
#include" pch.h"
#include" WinRTComponent.h"
// WinRTComponent.cpp
#include "pch.h"
#include "WinRTComponent.h"
使用命名空间WinRTComponentDll;
using namespace Platform;
using namespace WinRTComponentDll;
using namespace Platform;
BaseClass :: BaseClass()
{}
BaseClass::BaseClass()
{}
DerivedClass :: DerivedClass()
{}
DerivedClass::DerivedClass()
{}
推荐答案
这并不让我感到惊讶。 请记住,您需要确保只有密封的类才能通过WinRT传递。 你不能从密封的类派生,所以我希望这会失败。
That does not surprise me. Remember you need to ensure only classes that are sealed can be passed through WinRT. You cannot derive from sealed classes so I would expect this to fail.
这篇关于Javascript无法做动态类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!