gpt4 book ai didi

reflection - 将 reflect.value 转换为 reflect.method

转载 作者:IT王子 更新时间:2023-10-29 01:48:07 28 4
gpt4 key购买 nike

我有:

func NewMethodDescriptor(typ interface{}) *MethodDescriptor {

reflectedMethod := reflect.ValueOf(typ)
methodType := reflectedMethod.Type
paramCount := methodType.NumIn() - 1
...

但是当我尝试时:

NewMethodDescriptor(func(){})

我得到这个编译时错误:

methodType.NumIn undefined (type func() reflect.Type has no field or method NumIn)

最佳答案

更具体地说:

您可以在“Fun with the reflection package to analyse any function.”中看到更完整的示例

func FuncAnalyse(m interface{}) {

//Reflection type of the underlying data of the interface
x := reflect.TypeOf(m)

numIn := x.NumIn() //Count inbound parameters
numOut := x.NumOut() //Count outbounding parameters

fmt.Println("Method:", x.String())
fmt.Println("Variadic:", x.IsVariadic()) // Used (<type> ...) ?
fmt.Println("Package:", x.PkgPath())
}

关于reflection - 将 reflect.value 转换为 reflect.method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198387/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com