本文整理汇总了Golang中reflect.Value.MethodByName方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.MethodByName方法的具体用法?Golang Value.MethodByName怎么用?Golang Value.MethodByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.MethodByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getReflectValue
//get the value with with the matching name inside v.
//This value can be a struct field, a method attached to a struct or a value in a map
func getReflectValue(v reflect.Value, valueName string) (value reflect.Value, ok bool) {
// first check if input was a map and handle that.
// otherwise input was a struct or pointer to a struct
if v.Kind() == reflect.Map {
value = v.MapIndex(reflect.ValueOf(valueName))
if value.IsValid() {
if value.Kind() == reflect.Func {
value = getFuncSingleReturnValue(value)
}
ok = true
return value, true
}
}
value = reflect.Indirect(v).FieldByName(valueName)
if value.IsValid() {
if value.Kind() == reflect.Func {
value = getFuncSingleReturnValue(value)
}
ok = true
return
}
value = v.MethodByName(valueName)
if value.IsValid() {
value = getFuncSingleReturnValue(value)
ok = true
return
}
return
}
示例2: GetMethod
func GetMethod(s reflect.Value, name string) reflect.Value {
method := s.MethodByName(name)
if !method.IsValid() {
method = s.Elem().MethodByName(name)
}
return method
}
示例3: Invoke
func (p *Bean) Invoke(methodName string, args Args, callback ...interface{}) (returnFunc func(), err error) {
var beanValue reflect.Value
beanValue = reflect.ValueOf(p.instance)
inputs := make([]reflect.Value, len(args))
for i := range args {
inputs[i] = reflect.ValueOf(args[i])
}
values := beanValue.MethodByName(methodName).Call(inputs)
if values != nil && len(values) > 0 {
lastV := values[len(values)-1]
if lastV.Interface() != nil {
if errV, ok := lastV.Interface().(error); ok {
if errV != nil {
err = errV
return
}
}
}
}
if callback != nil && len(callback) > 0 {
returnFunc = func() {
reflect.ValueOf(callback[0]).Call(values)
}
}
return
}
示例4: runMethodIfExists
func runMethodIfExists(v reflect.Value, name string, args ...interface{}) {
method := v.MethodByName(name)
if method.Kind() == reflect.Invalid {
return
}
if method.Type().NumIn() != len(args) {
panic(fmt.Sprintf(
"%s: expected %d args, actually %d.",
name,
len(args),
method.Type().NumIn()))
}
// Create a slice of reflect.Values to pass to the method. Simultaneously
// check types.
argVals := make([]reflect.Value, len(args))
for i, arg := range args {
argVal := reflect.ValueOf(arg)
if argVal.Type() != method.Type().In(i) {
panic(fmt.Sprintf(
"%s: expected arg %d to have type %v.",
name,
i,
argVal.Type()))
}
argVals[i] = argVal
}
method.Call(argVals)
}
示例5: applyToStruct
func applyToStruct(obj reflect.Value, head P, mid string, tail P, ctx *Context) error {
if mid != "*" {
result := obj.FieldByName(mid)
if result.Kind() == reflect.Invalid {
result = obj.MethodByName(mid)
}
if result.Kind() == reflect.Invalid {
return fmt.Errorf("no field '%s' in type '%s' at '%s'", mid, obj.Type(), head)
}
return apply(result, append(head, mid), tail, ctx)
}
typ := obj.Type()
for i := 0; i < typ.NumField() && !ctx.stop; i++ {
if err := applyToStruct(obj, head, typ.Field(i).Name, tail, ctx); err != nil && err != ErrMissing {
return err
}
}
return nil
}
示例6: isEmptyValue
// from encoding/json
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
z := v.MethodByName("IsZero")
if z.IsValid() {
return z.Call(nil)[0].Interface().(bool)
}
// This shouldn't really happen...
zero := reflect.Zero(v.Type()).Interface()
current := v.Interface()
return reflect.DeepEqual(current, zero)
}
示例7: getReflectValue
//get the value with with the matching name inside v.
//This value can be a struct field, a method attached to a struct or a value in a map
func getReflectValue(v reflect.Value, valueName string) (reflect.Value, bool) {
value := reflect.Indirect(v).FieldByName(valueName)
if value.IsValid() {
if value.Kind() == reflect.Func {
value = getFuncSingleReturnValue(value)
}
return value, true
}
if v.Kind() == reflect.Map {
value = v.MapIndex(reflect.ValueOf(valueName))
if value.IsValid() {
if value.Kind() == reflect.Func {
value = getFuncSingleReturnValue(value)
}
return value, true
}
}
value = v.MethodByName(valueName)
if value.IsValid() {
value = getFuncSingleReturnValue(value)
return value, true
}
return reflect.Value{}, false
}
示例8: getResult
func getResult(method string, vResourcePtr *reflect.Value) (result interface{}) {
defer func() {
if r := recover(); r != nil {
rstr := fmt.Sprintf("%s", r)
result = internalError{
where: lineInfo(3),
why: rstr + fullTrace(5, "\n\t"),
code: errorCode(rstr),
}
}
}()
methodName := strings.Title(strings.ToLower(method))
if methodName == "Head" {
methodName = "Get"
}
vMethod := vResourcePtr.MethodByName(methodName)
if vMethod.IsValid() {
result = vMethod.Call([]reflect.Value{})[0].Interface()
}
if result == nil {
return methodNotAllowed{getAllowed(vResourcePtr)}
}
return
}
示例9: MethodByName
// get public method by name
func MethodByName(v reflect.Value, name string) (m reflect.Value, ok bool) {
m = v.MethodByName(strings.Title(name))
if m.IsValid() && m.Kind() == reflect.Func {
return m, true
}
return
}
示例10: getMethodValue
// getMethodValue the method value from the method context and method name
func getMethodValue(t *Task) (method reflect.Value, err error) {
// Will look for the method on both a
// pointer and a value receiver
var ptr reflect.Value
var val reflect.Value
val = reflect.ValueOf(t.MethodState)
if val.Type().Kind() == reflect.Ptr {
ptr = val
val = ptr.Elem()
} else {
ptr = reflect.New(val.Type())
ptr.Elem().Set(val)
}
method = val.MethodByName(t.Method)
if method.IsValid() {
return
}
method = ptr.MethodByName(t.Method)
if method.IsValid() {
return
}
err = errors.New("Method not found")
return
}
示例11: function2Func
func function2Func(in *reflect.Value, t reflect.Type) {
fn := in.MethodByName("Call")
wrap := func(args []reflect.Value) (results []reflect.Value) {
ret := fn.Call(args)[0]
n := t.NumOut()
if n == 0 {
return
}
if n == 1 {
if t.Out(0) != typeIntf {
ret = ret.Elem()
}
return []reflect.Value{ret}
}
if ret.Kind() != reflect.Slice || ret.Len() != n {
panic(fmt.Sprintf("unexpected return value count, we need `%d` values", n))
}
results = make([]reflect.Value, n)
for i := 0; i < n; i++ {
result := ret.Index(i)
if t.Out(i) != typeIntf {
result = result.Elem()
}
results[i] = result
}
return
}
*in = reflect.MakeFunc(t, wrap)
}
示例12: SafelyCall
// safelyCall invokes `function` in recover block
func (a *App) SafelyCall(vc reflect.Value, method string, args []reflect.Value) (resp []reflect.Value, err error) {
defer func() {
if e := recover(); e != nil {
if !a.Server.Config.RecoverPanic {
// go back to panic
panic(e)
} else {
resp = nil
var content string
content = fmt.Sprintf("Handler crashed with error: %v", e)
for i := 1; ; i += 1 {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
} else {
content += "\n"
}
content += fmt.Sprintf("%v %v", file, line)
}
a.Error(content)
err = errors.New(content)
return
}
}
}()
function := vc.MethodByName(method)
return function.Call(args), err
}
示例13: structCanBeMarshalled
func structCanBeMarshalled(v reflect.Value) bool {
m := v.MethodByName("String")
if m.IsValid() && !m.IsNil() {
return true
}
return false
}
示例14: callMethod
func (scope *Scope) callMethod(methodName string, reflectValue reflect.Value) {
// Only get address from non-pointer
if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
reflectValue = reflectValue.Addr()
}
if methodValue := reflectValue.MethodByName(methodName); methodValue.IsValid() {
switch method := methodValue.Interface().(type) {
case func():
method()
case func(*Scope):
method(scope)
case func(*DB):
newDB := scope.NewDB()
method(newDB)
scope.Err(newDB.Error)
case func() error:
scope.Err(method())
case func(*Scope) error:
scope.Err(method(scope))
case func(*DB) error:
newDB := scope.NewDB()
scope.Err(method(newDB))
scope.Err(newDB.Error)
default:
scope.Err(fmt.Errorf("unsupported function %v", methodName))
}
}
}
示例15: callGoMethod
func callGoMethod(L *lua.State, name string, st reflect.Value) {
ret := st.MethodByName(name)
if !ret.IsValid() {
fmt.Println("whoops")
}
L.PushGoFunction(GoLuaFunc(L, ret))
}