Skip to content

Commit

Permalink
改进函数调用方法
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Jul 31, 2020
1 parent 64f393b commit be16c77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 5 additions & 3 deletions method_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ func (method MethodInfo) IsValid() bool {

// Invoke : invoke the method with interface params.
func (method MethodInfo) Invoke(instance interface{}, params ...interface{}) []interface{} {
paramsCount := len(method.Parameters)
paramsValues := make([]reflect.Value, paramsCount)
paramsCount := len(params)
//paramsValues := make([]reflect.Value, paramsCount)
var paramsValues []reflect.Value
for idx := 0; idx < paramsCount; idx++ {
paramsValues[idx] = reflect.ValueOf(params[idx])
paramsValues = append(paramsValues, reflect.ValueOf(params[idx]))

}
return method.InvokeWithValue(reflect.ValueOf(instance), paramsValues...)
}
Expand Down
8 changes: 6 additions & 2 deletions reflect_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ func getMethodInfo(method reflect.Method) MethodInfo {
methodInfo := MethodInfo{}
methodInfo.Name = method.Name
methodInfo.MethodInfoType = method.Type
paramsCount := method.Type.NumIn() - 1
paramsCount := method.Type.NumIn()
methodInfo.Parameters = make([]MethodParameterInfo, paramsCount)

for idx := 0; idx < paramsCount; idx++ {
methodInfo.Parameters[idx].ParameterType = methodInfo.MethodInfoType.In(idx)
methodInfo.Parameters[idx].Name = methodInfo.Parameters[idx].ParameterType.Name()
parameterType := methodInfo.Parameters[idx].ParameterType
if parameterType.Kind() == reflect.Ptr {
parameterType = parameterType.Elem()
}
methodInfo.Parameters[idx].Name = parameterType.Name()
}
if methodInfo.MethodInfoType.NumOut() > 0 {
methodInfo.OutType = methodInfo.MethodInfoType.Out(0)
Expand Down

0 comments on commit be16c77

Please sign in to comment.