从ArkTs向Native传复杂参数-Reflect及Class篇

HarmonyOS怎样处理反射及调用class的方法。

HarmonyOS
2024-05-22 23:31:56
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
进击的鱼白

使用的核心API

Reflect.set
Reflect.get
napi_get_named_property
napi_call_function
  • 1.
  • 2.
  • 3.
  • 4.

核心代码解释

TestClass.ets文件定义反射相关的类的相关方法:

import { hilog } from '@kit.PerformanceAnalysisKit'; 
 
export class MyClass { 
  value:number = 9; 
  PrintValue() { 
    hilog.info(0x0000, 'testTag', 'enter MyClass PrintValue,value:%{public}d', this.value); 
  } 
  SetValue(val:number) { 
    hilog.info(0x0000, 'testTag', 'enter MyClass SetValue,old value:%{public}d, new value:%{public}d', this.value, val); 
    this.value = val; 
  } 
} 
 
export class MyStaticClass { 
  value:number = 88; 
  GetValue():number { 
    return this.value; 
  } 
  SetValue(val:number) { 
    hilog.info(0x0000, 'testTag', 'enter MyStaticClass SetValue,old value:%{public}d, new value:%{public}d', this.value, val); 
    this.value = val; 
  } 
  PrintValue() { 
    hilog.info(0x0000, 'testTag', 'enter MyStaticClass PrintValue,value:%{public}d', this.value); 
  } 
} 
 
export default new MyStaticClass();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

Index.ets文件定义反射对象:

import { hilog } from '@kit.PerformanceAnalysisKit'; 
import util from "@ohos.util"; 
import testNapi from 'libentry.so'; 
import MyStaticClass, { MyClass } from './TestClass' 
 
function TestReflectFun(par:number) 
{ 
  hilog.info(0x0000, 'testTag', 'TestReflectFun is called,par: %{public}d', par); 
} 
 
let myClass = new MyClass(); 
Reflect.set(myClass, "key", "keyValue"); 
let str:string = Reflect.get(myClass, "key"); 
hilog.info(0x0000, 'testTag', 'strKeyValue : %{public}s', str); 
 
Reflect.set(myClass, "DefineLambaFun", () => { 
  hilog.info(0x0000, 'testTag', 'Define LambaFun is called.'); 
}); 
let lamFun: Function = Reflect.get(myClass, "DefineLambaFun"); 
lamFun(); 
 
Reflect.set(myClass, "DefineCommonFun", TestReflectFun); 
let globeFun: Function = Reflect.get(myClass, "DefineCommonFun"); 
globeFun(666); 
 
Reflect.set(myClass, "value", 1000); 
myClass.PrintValue();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

C++层调用ArkTS方法:

static napi_value MyClassCall(napi_env env, napi_callback_info info) { 
 size_t argc = 1; 
 napi_value args[1] = {nullptr}; 
 
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 
 
napi_value newValue; 
napi_create_int32(env, 9999, &newValue); 
napi_value myClassSetMethod; 
napi_get_named_property(env, args[0], "SetValue", &myClassSetMethod); 
napi_call_function(env, args[0], myClassSetMethod, 1, &newValue, nullptr); 
 
OH_LOG_INFO(LOG_APP, "MyClassCall SetValue finished"); 
 
napi_value myClassPrintMethod; 
napi_get_named_property(env, args[0], "PrintValue", &myClassPrintMethod); 
napi_call_function(env, args[0], myClassPrintMethod, 0, nullptr, nullptr); 
 
OH_LOG_INFO(LOG_APP, "MyClassCall PrintValue finished"); 
 
napi_value myClassCommonMethod; 
napi_create_int32(env, 7777, &newValue); 
napi_get_named_property(env, args[0], "DefineCommonFun", &myClassCommonMethod); 
napi_call_function(env, args[0], myClassCommonMethod, 1, &newValue, nullptr); 
 
OH_LOG_INFO(LOG_APP, "MyClassCall DefineCommonFun finished"); 
 
return nullptr; 
} 
 
static napi_value MyStaticClassCall(napi_env env, napi_callback_info info) { 
 size_t argc = 1; 
 napi_value args[1] = {nullptr}; 
 
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 
 
napi_value newValue; 
napi_create_int32(env, 8888, &newValue); 
napi_value myStaticClassSetMethod; 
napi_get_named_property(env, args[0], "SetValue", &myStaticClassSetMethod); 
napi_call_function(env, args[0], myStaticClassSetMethod, 1, &newValue, nullptr); 
 
OH_LOG_INFO(LOG_APP, "MyStaticClassCall SetValue finished"); 
 
napi_value staticClassMethod; 
napi_get_named_property(env, args[0], "PrintValue", &staticClassMethod); 
napi_call_function(env, args[0], staticClassMethod, 0, nullptr, nullptr); 
 
OH_LOG_INFO(LOG_APP, "MyStaticClassCall call finished"); 
 
return nullptr;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.

实现效果

手机上执行结果如下:

适配的版本信息

IDE:DevEco Studio NEXT Developer Preview2 4.1.3.600

SDK:HarmoneyOS 4.1.6.5

分享
微博
QQ
微信
回复
2024-05-23 21:21:01
相关问题
ArkTsNative复杂参数---List参数
1487浏览 • 1回复 待解决
HarmonyOS 构造参数失败问题
395浏览 • 1回复 待解决
HarmonyOS Web runJavaScript 如何参数
535浏览 • 1回复 待解决
HarmonyOS 参数参问题
590浏览 • 1回复 待解决
ArkTs支持java或者TS Reflect的反射用法吗
3490浏览 • 1回复 待解决
如何支持HarmonyOS调用JS方法参?
1044浏览 • 1回复 待解决
TCPExtraOptions中各个参数的设置影响
601浏览 • 1回复 待解决
HarmonyOS关于AXIOS动态参数问题
1130浏览 • 1回复 待解决
xargs命令中多个参数实例?
10085浏览 • 1回复 待解决
Native与TS互传自定义obj class
1267浏览 • 1回复 待解决