package main

import (
    "fmt"
    "reflect"
)

type YourT1 struct {
}

func (y *YourT1) MethodBar() {
    fmt.Println("MethodBar called")
}

type YourT2 struct {
}

func (y *YourT2) MethodFoo(i int, oo string) {
    fmt.Println("MethodFoo called", i, oo)
}

func InvokeObjectMethod(object interface{}, methodName string, args ...interface{}) {
    inputs := make([]reflect.Value, len(args))
    for i, _ := range args {
        inputs[i] = reflect.ValueOf(args[i])
    }
    reflect.ValueOf(object).MethodByName(methodName).Call(inputs)
}

func main() {
    InvokeObjectMethod(new(YourT2), "MethodFoo", 10, "abc")
    InvokeObjectMethod(new(YourT1), "MethodBar")
}

 

相关文章:

  • 2021-11-01
  • 2021-08-02
  • 2022-01-23
  • 2021-10-26
  • 2021-10-21
  • 2021-05-25
  • 2021-06-18
猜你喜欢
  • 2021-11-02
  • 2022-01-18
  • 2022-12-23
  • 2023-03-14
  • 2023-03-20
  • 2021-12-18
  • 2022-12-23
相关资源
相似解决方案