当前位置: 首页>>代码示例>>Golang>>正文


Golang Type.MethodByName方法代码示例

本文整理汇总了Golang中reflect.Type.MethodByName方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.MethodByName方法的具体用法?Golang Type.MethodByName怎么用?Golang Type.MethodByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Type的用法示例。


在下文中一共展示了Type.MethodByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: findEnterFunc

func (h *Handler) findEnterFunc(t reflect.Type) *reflect.Method {
	method, ok := t.MethodByName(flaglyEnter)
	if ok {
		return &method
	}
	return nil
}
开发者ID:chzyer,项目名称:flagly,代码行数:7,代码来源:handler.go

示例2: compileField

func compileField(typ reflect.Type, name string, args []parse.Node, final reflect.Type) (fn lookupFn, elem reflect.Type) {
	if isNilType(typ) {
		fn = compileFieldDynamic(name, args)
		return
	}

	if m, exist := typ.MethodByName(name); exist {
		fn = compileMethodCall(typ, m.Func, args, final)
		elem = m.Type.Out(0)
		return
	}

	switch typ.Kind() {
	case reflect.Struct:
		structField, found := typ.FieldByName(name)
		if !found {
			panic(fmt.Errorf("%s has no field %s", typ, name))
		}
		fn = func(s state, v reflect.Value, final interface{}) reflect.Value {
			return v.FieldByIndex(structField.Index)
		}
		elem = structField.Type
		return
	case reflect.Map:
		k := reflect.ValueOf(name)
		fn = func(s state, v reflect.Value, final interface{}) reflect.Value {
			return v.MapIndex(k)
		}
		elem = typ.Key()
		return
	}
	panic(fmt.Errorf("struct or map expected, but got %s", typ))
}
开发者ID:rui314,项目名称:template,代码行数:33,代码来源:template.go

示例3: CheckMethod

func CheckMethod(t *testing.T, typ reflect.Type, desc Method) {
	method, found := typ.MethodByName(desc.Name)
	if !found {
		t.Fatalf("Le type %v ne définit pas de méthode '%s'", typ, desc.Name)
	}

	if method.Type.NumIn() != len(desc.InTypes)+1 {
		t.Fatalf("La méthode %v.%s doit prendre %d paramètre(s)", typ, desc.Name, len(desc.InTypes))
	}

	if method.Type.NumOut() != len(desc.OutTypes) {
		t.Fatalf("La méthode %v.%s doit renvoyer %d résultat(s)", typ, desc.Name, len(desc.OutTypes))
	}

	for i, inType := range desc.InTypes {
		if method.Type.In(i+1) != inType {
			t.Fatalf("La méthode %v.%s doit prendre un type %v en paramètre #%d", typ, desc.Name, inType, i+1)
		}
	}

	for i, outType := range desc.OutTypes {
		if method.Type.Out(i) != outType {
			t.Fatalf("La méthode %v.%s doit renvoyer un type %v en résultat #%d", typ, desc.Name, outType, i+1)
		}
	}
}
开发者ID:ahmedbenouezdou,项目名称:hands-on-go,代码行数:26,代码来源:reflect.go

示例4: addMethods

func (info *Info) addMethods(jt *Type, t reflect.Type) {
	// Add any methods.
	var vt reflect.Type
	if t.Kind() != reflect.Interface && !isWithoutReceiver(t) {
		t = reflect.PtrTo(t)
		vt = t.Elem()
	}
	for i := 0; i < t.NumMethod(); i++ {
		m := t.Method(i)
		if m.PkgPath != "" {
			continue
		}
		if t.Kind() != reflect.Interface {
			m.Type = withoutReceiverType{m.Type}
		}
		jm := Method{
			Name: m.Name,
			Type: info.Ref(m.Type),
		}
		if vt != nil {
			_, hasValueMethod := vt.MethodByName(m.Name)
			jm.PtrReceiver = !hasValueMethod
		}
		if jt.Methods == nil {
			jt.Methods = make(map[string]*Method)
		}
		jt.Methods[jm.Name] = &jm
	}
}
开发者ID:rogpeppe,项目名称:apicompat,代码行数:29,代码来源:jsontypes.go

示例5: dispose

func dispose(v uintptr, t r.Type) {
	if m, ok := t.MethodByName("Dispose"); ok {
		// && m.Func.Type().NumIn() == 2 {
		f := m.Func
		tv := r.NewAt(f.Type().In(1).Elem(),
			unsafe.Pointer(v))
		f.Call([]r.Value{r.New(t).Elem(), tv})
	}
}
开发者ID:postfix,项目名称:outside,代码行数:9,代码来源:outside.go

示例6: missingMethod

func missingMethod(iT, xT reflect.Type) (method string) {
	numMethod := iT.NumMethod()
	for i := 0; i < numMethod; i += 1 {
		method = iT.Method(i).Name
		if _, ok := xT.MethodByName(method); !ok {
			return
		}
	}
	return ""
}
开发者ID:philipmulcahy,项目名称:godebug,代码行数:10,代码来源:errors.go

示例7: getMapMethods

func getMapMethods(baseType reflect.Type) (get, set reflect.Type, err error) {
	getAll, ok := baseType.MethodByName("GetAll")
	if !ok {
		return nil, nil, errors.Errorf("no method named GetAll")
	}
	setAll, ok := baseType.MethodByName("SetAll")
	if !ok {
		return nil, nil, errors.Errorf("no method named SetAll")
	}
	return getAll.Type, setAll.Type, nil
}
开发者ID:opentable,项目名称:sous,代码行数:11,代码来源:specialmap.go

示例8: MethodsMissingFromType

func MethodsMissingFromType(inter, typ reflect.Type) []string {
	missingMethods := make([]string, 0)
	for n := 0; n < inter.NumMethod(); n++ {
		_, present := typ.MethodByName(inter.Method(n).Name)
		if !present {
			fmt.Println(inter.Method(n).Name)
			missingMethods = append(missingMethods, inter.Method(n).Name)
		}
	}
	return missingMethods
}
开发者ID:nshepperd,项目名称:goFingerTree,代码行数:11,代码来源:fingerTree_test.go

示例9: enumSymbols

func enumSymbols(t reflect.Type) []string {
	if _, ok := t.MethodByName("SymbolSet"); ok {
		//an enum type that RDL generated. Create a dummy instance and call to get the symbol set
		tmp := reflect.New(t)
		vv := tmp.Elem()
		vv.SetInt(1)
		symbols := tmp.MethodByName("SymbolSet").Call([]reflect.Value{})[0].Interface()
		return symbols.([]string)[1:]
	}
	return nil
}
开发者ID:dmitris,项目名称:ardielle-go,代码行数:11,代码来源:common.go

示例10: findParameterGetMethod

func findParameterGetMethod(handlerParametersType reflect.Type, field reflect.Type) (reflect.Method, bool) {
	var name []rune
	switch field.Kind() {
	case reflect.Ptr:
		name = []rune(field.Elem().Kind().String())
	default:
		name = []rune(field.Kind().String())
	}
	name[0] = unicode.ToUpper(name[0])
	methodName := "Get" + string(name)
	return handlerParametersType.MethodByName(methodName)
}
开发者ID:bunin,项目名称:gorpc,代码行数:12,代码来源:handlers_manager.go

示例11: methodValue

func methodValue(key string, typ reflect.Type) (mapFunc, reflect.Type, error) {
	if m, ok := typ.MethodByName(key); ok {
		if m.Type.NumIn() > 1 {
			return nil, nil, fmt.Errorf("method %s on type %s has %d arguments, must have none", key, typ, m.Type.NumIn()-1)
		}
		if m.Type.NumOut() != 1 {
			return nil, nil, fmt.Errorf("method %s on type %s returns %d values, must return one", key, typ, m.Type.NumOut())
		}
		return methodValueFunc(m), m.Type.Out(0), nil
	}
	return nil, nil, nil
}
开发者ID:rainycape,项目名称:gondola,代码行数:12,代码来源:funcs.go

示例12: getTypes

func getTypes(t reflect.Type, s supportingTypes, c *int) *Type {
	n, p := t.String(), t.PkgPath()

	if _, ok := t.MethodByName("MarshalJSON"); ok {
		return custom(n, p)
	}

	switch t.Kind() {
	case reflect.String:
		return simple(String)
	case reflect.Int, reflect.Int8, reflect.Int16,
		reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16,
		reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return simple(Int)
	case reflect.Float32, reflect.Float64:
		return simple(Float)
	case reflect.Bool:
		return simple(Bool)
	case reflect.Ptr:
		return s.set(n, p, func() *Type {
			return ptrOf(getTypes(t.Elem(), s, c))
		})
	case reflect.Array, reflect.Slice:
		if t.Elem().Kind() == reflect.Uint8 {
			return simple(Bytes)
		}
		return s.set(n, p, func() *Type {
			return arrayOf(getTypes(t.Elem(), s, c))
		})
	case reflect.Struct:
		if p == "" {
			// gets an anonymous type name and fake the package
			n = anonTypeName(c)
			p = "github.com/shutej/go2flow/anonymous"
		}
		return s.set(n, p, func() *Type {
			return structOf(getFields(t, s, c))
		})
	case reflect.Map:
		if t.Key().Kind() != reflect.String {
			log.Fatalf("unexpected map key type: %v", t.Key())
		}
		return s.set(n, p, func() *Type {
			return mapOf(getTypes(t.Elem(), s, c))
		})
	}
	log.Fatalf("unknown kind for type: %v", t)
	return nil
}
开发者ID:shutej,项目名称:go2flow,代码行数:50,代码来源:type.go

示例13: createTypeDescribe

func createTypeDescribe(typ reflect.Type) *typeDescribe {
	td := &typeDescribe{}
	td.initMethod, td.hasInitMethod = typ.MethodByName("PostConstruct")
	for typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
	}
	for i := 0; i < typ.NumField(); i++ {
		f := typ.Field(i)
		tag := f.Tag.Get("inject")
		if tag != "" {
			td.fields = append(td.fields, f)
		}
	}
	typeDescribes[typ.String()] = td
	return td
}
开发者ID:cobersky,项目名称:gioc,代码行数:16,代码来源:typeDescribe.go

示例14: go2jsonType

func go2jsonType(t reflect.Type, canAddr bool) *Type {
	if _, ok := t.MethodByName("MarshalJSON"); ok {
		return CustomType(t.String())
	}
	if canAddr {
		if _, ok := reflect.PtrTo(t).MethodByName("MarshalJSON"); ok {
			return CustomType(t.String())
		}
	}
	switch t.Kind() {
	case reflect.Bool:
		return SimpleType(Bool)
	case reflect.Int, reflect.Int8, reflect.Int16,
		reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16,
		reflect.Uint32, reflect.Uint64, reflect.Uintptr,
		reflect.Float32, reflect.Float64:
		return SimpleType(Number)
	case reflect.String:
		return SimpleType(String)
	case reflect.Complex64, reflect.Complex128:
		return CustomType("complex")
	case reflect.Chan:
		return CustomType("chan")
	case reflect.Array, reflect.Slice:
		return ArrayOf(go2jsonType(t.Elem(), canAddr || t.Kind() == reflect.Slice))
	case reflect.Map:
		if t.Key().Kind() != reflect.String {
			// TODO better error recovery
			panic("unexpected map key kind")
		}
		return MapOf(go2jsonType(t.Elem(), false))
	case reflect.Ptr:
		return NullableOf(go2jsonType(t.Elem(), true))
	case reflect.Struct:
		return ObjectOf(jsonFields(t, canAddr))
	case reflect.UnsafePointer:
		return CustomType("unsafe.Pointer")
	case reflect.Interface:
		return CustomType("any")
	case reflect.Func:
		return CustomType("func")
	}
	panic(fmt.Errorf("unknown kind for type %s", t))
}
开发者ID:zxpbenson,项目名称:rog-go,代码行数:45,代码来源:rpcreflect.go

示例15: callMethod

func (p *Page) callMethod(tpc reflect.Type, vpc reflect.Value, action string, rvr reflect.Value, rvw reflect.Value) []reflect.Value {
	arv := []reflect.Value{}
	if rm, ok := tpc.MethodByName(action); ok {
		mt := rm.Type
		switch mt.NumIn() {
		case 2:
			if mt.In(1) == rvr.Type() {
				arv = vpc.MethodByName(action).Call([]reflect.Value{rvr})
			} else {
				arv = vpc.MethodByName(action).Call([]reflect.Value{rvw})
			}
		case 3:
			arv = vpc.MethodByName(action).Call([]reflect.Value{rvw, rvr})
		default:
			arv = vpc.MethodByName(action).Call([]reflect.Value{})
		}
	}

	return arv
}
开发者ID:JessonChan,项目名称:framework,代码行数:20,代码来源:page.go


注:本文中的reflect.Type.MethodByName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。