zoukankan      html  css  js  c++  java
  • 反射的坑

    主要内容:

    • 由对象获取反射类型,由对象获取反射值
    • 由反射值获取反射类型
    • 反射值重新转换成对象
    • 遍历字段
    • 遍历方法
    • 易知go语言的struct是值类型,如果修改需要使用引用传递。反射修改值类型,要获取其指针的值类型。此方法适用于int string 以及struct。
    • 反射调用的注意点:
      • 类方法要大写,以便能被其它包,也就是reflect包调用。小写的方法可以由反射查询,但是不能调用。
      • 要获取其引用的反射。
      • 反射调用方法,最好检测m.String是否为,避免程序因不能正确获取反射方法而发生panic。
      • 即使是调用无形参的反射方法,也要传入[]reflect.Value{}或者nil。
      • 反射调用不定参方法,传入切片打散后,再转成reflect.Value。
    • 最后附上一个反射调用的包装,用来排除以上坑点。
    type User struct {
        Id int
        Name string
    }
    
    func (c *User)getId() int {
        return c.Id
    }
    
    func (c *User)getName() string {
        return c.Name
    }
    
    func (c *User)AppendString(s1 string,s2 ...string) (sRes string) {
        sRes = s1
        for _,str := range s2{
            sRes += str
        }
        return
    }
    
    func main()  {
        //获取反射信息
        u := User{1,"one"}
        uType := reflect.TypeOf(u)
        uValue := reflect.ValueOf(u)
    
        uFf := uValue.Interface().(User)
        fmt.Println(uFf)
    
        uType2 := uValue.Type()
        fmt.Printf("get type from uValue:%s
    ",uType2.Name())
    
        for i:=0;i<uType.NumField();i++{
            fmt.Printf("field:%s,value:%+v
    ",uType.Field(i).Name,uValue.Field(i).Interface())
        }
    
        for i:=0;i<uType.NumMethod();i++{
            m := uType.Method(i)
            fmt.Printf("method name is %s
    ",m.Name)
        }
    
        //对值类型改值
        x := 10
        xF := reflect.ValueOf(x)
        fmt.Printf("could change:%t
    ",xF.CanSet())//false值类型不可修改
    
        xPF := reflect.ValueOf(&x).Elem()
        fmt.Printf("could change value:%t
    ",xPF.CanSet()) //xpf必须是指针类型,而且值本身可以修改,或者是结构体的某个大写字段才会返回true,否则都是false,比如下面
        /*type student struct {
            name string
            Age  int
        }
          ss := reflect.ValueOf(&stu2).Elem()
          //canSet返回真条件1必须是指针类型,Field拿到的字段必须是可以导出即大写,
          fmt.Println(ss.Field(0).CanSet()) 这里为false,因为拿到的是name是小写
          fmt.Println(ss.Field(1).CanSet()) 这里是true,因为拿到的是Age是大写
          */
        xPF.SetInt(11)
        fmt.Printf("changed value is:%v
    ",xPF.Interface())
    
        //反射修改struct
        u1 := User{1,"One"}
        u1PF := reflect.ValueOf(&u1).Elem()
        if u1PF.CanSet(){
            u1PF.Field(0).SetInt(2)
            u1PF.Field(1).SetString("two")
        }
        fmt.Printf("changed struct value is %v
    ",u1PF.Interface().(User))
    
        //反射调用方法
        reflectMethodInput := []reflect.Value{reflect.ValueOf("http"),reflect.ValueOf("Request")}
        m := reflect.ValueOf(&u1).MethodByName("AppendString")
        fmt.Println(m.String())
        mR := m.Call(reflectMethodInput)
        fmt.Println(mR)
    }
    func reflectCall(obj interface{}, method string, args... interface{}) []reflect.Value{
        argsR := make([]reflect.Value, len(args))
        for i, _ := range args {
            argsR[i] = reflect.ValueOf(args[i])
        }
        if v := reflect.ValueOf(&obj).MethodByName(method); v.String() == "<invalid Value>" {
            return nil
        }else {
            return v.Call(argsR)
        }
    }




  • 相关阅读:
    PHP Mysql-插入多条数据
    PHP Mysql-插入数据
    PHP Mysql-创建数据表
    PHP Mysql-创建数据库
    PHP Mysql-连接
    PHP Mysql-简介
    PHP-7
    postgresql 创建函数
    在psql客户端中修改函数
    修改PostgreSQL数据库的默认用户postgres的密码
  • 原文地址:https://www.cnblogs.com/hualou/p/12069713.html
Copyright © 2011-2022 走看看