首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将reflect.value转换为reflect.method

将reflect.value转换为reflect.method
EN

Stack Overflow用户
提问于 2015-08-25 15:51:39
回答 1查看 395关注 0票数 2

我有一种休闲感:

代码语言:javascript
复制
func NewMethodDescriptor(typ interface{}) *MethodDescriptor {

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

但当我尝试时:

代码语言:javascript
复制
NewMethodDescriptor(func(){})

我得到了这个编译时错误:

代码语言:javascript
复制
methodType.NumIn undefined (type func() reflect.Type has no field or method NumIn)
EN

回答 1

Stack Overflow用户

发布于 2018-07-05 05:16:22

更具体地说:

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

代码语言:javascript
复制
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())
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32198387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档