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

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

HarmonyOS
2024-05-22 23:31:56
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
yijuefei

使用的核心API

Reflect.set
Reflect.get
napi_get_named_property
napi_call_function

核心代码解释

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();

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();

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;

实现效果

手机上执行结果如下:

适配的版本信息

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参数
377浏览 • 1回复 待解决
xargs命令中多个参数实例?
7926浏览 • 1回复 待解决
Native与TS互传自定义obj class
499浏览 • 1回复 待解决
ArrayBuffer是否一定要固定长度参数
739浏览 • 1回复 待解决
Native调用ArkTS侧类函数
368浏览 • 1回复 待解决
arkts侧hashmap转为native
447浏览 • 1回复 待解决
Native如何调ArkTS的方法
966浏览 • 1回复 待解决
arkts父子组件组件怎么通信值啊?
3456浏览 • 1回复 待解决
ArkTSNative互传数组类型数据
560浏览 • 1回复 待解决
如何在Native侧释放ArkTS对象
1004浏览 • 1回复 待解决
ArkTs怎么传递对象或者类给Native
886浏览 • 1回复 待解决
ArkTSNative如何动态加载、卸载so
835浏览 • 1回复 待解决