go在函数内部获取函数本身信息
type Person struct {
Name string
Age int
}
func (this *Person) Eat(food string) {
fmt.Println("eatting :", food)
}
func (this *Person) SayHi(name string, age int) {
rvalue := reflect.ValueOf(this)
rtype := reflect.TypeOf(this)
for i := 0; i < rvalue.NumMethod(); i++ {
methodType := reflect.TypeOf(reflect.ValueOf(this).Method(i).Interface())
//methodValue := reflect.ValueOf(reflect.ValueOf(this).Method(i).Interface())
fmt.Println("函数名:", rtype.Method(i).Name)
fmt.Println("参数个数:", methodType.NumIn())
for j := 0; j < methodType.NumIn(); j++ {
fmt.Printf("参数索引:%v\r\n参数类型:%v\r\n", j, methodType.In(j).Name())
}
}
}
func TestReflectMethod(t *testing.T) {
p := &Person{}
p.SayHi("leo", 23)
}
blog comments powered by Disqus