-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_info.go
49 lines (42 loc) · 1.44 KB
/
method_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package reflectx
import (
"reflect"
)
// Method Info
type MethodInfo struct {
Name string //Method Name
MethodInfoType reflect.Type //method type
Parameters []MethodParameterInfo //method's Parameters
OutType reflect.Type //function's return type.
}
// IsValid : method is valid
func (method MethodInfo) IsValid() bool {
return true
}
// Invoke : invoke the method with interface params.
func (method MethodInfo) Invoke(instance interface{}, params ...interface{}) []interface{} {
paramsCount := len(params)
//paramsValues := make([]reflect.Value, paramsCount)
var paramsValues []reflect.Value
for idx := 0; idx < paramsCount; idx++ {
paramsValues = append(paramsValues, reflect.ValueOf(params[idx]))
}
return method.InvokeWithValue(reflect.ValueOf(instance), paramsValues...)
}
// InvokeWithValue: invoke the method with value params.
func (method MethodInfo) InvokeWithValue(instance reflect.Value, paramsValues ...reflect.Value) []interface{} {
methodInfoVal := instance.MethodByName(method.Name)
returns := methodInfoVal.Call(paramsValues)
outNum := method.MethodInfoType.NumOut()
results := make([]interface{}, outNum)
if len(returns) > 0 {
for i, res := range returns {
results[i] = res.Interface()
}
}
return results
}
// AsTypeInfo : convert method to TypeInfo
func (method MethodInfo) AsTypeInfo() (TypeInfo, error) {
return GetTypeInfoWithValueType(method.MethodInfoType)
}