package main
import (
"fmt"
"reflect"
)
type Type1 struct{}
func (t Type1) Method1() {
fmt.Println("Method 1")
}
type Type2 struct{}
func (t Type2) Method2(args0 string, args1 float64) {
fmt.Println("Method 2 ", args0, args1)
}
func Invoke(any interface{}, name string, args ...interface{}) {
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
func main() {
Invoke(Type1{}, "Method1")
Invoke(Type2{}, "Method2", "abc", 3.145926)
}
go 反射执行方法,要不要搞点事情
最新推荐文章于 2024-07-19 11:23:28 发布
1693

被折叠的 条评论
为什么被折叠?



