文章目录
说在前面
- go版本:go1.14.1 windows/amd64
问题提出
type XStruct strcut {
Data int
}
a := function("XStruct")
fmt.Println(a.Data)
其他语言
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
注册式方法
type XStruct struct {
Data int
}
type YStruct struct {
Data string
}
var typeRegistry = make(map[string]reflect.Type)
// 注册
func Register() {
typeList := []interface{}{
&XStruct{},
&YStruct{},
}
for idx := range typeList {
inter := typeList[idx]
typeRegistry[fmt.Sprintf("%T", inter)] = reflect.TypeOf(inter)
}
}
func main() {
Register()
t, ok := typeRegistry["*main.XStruct"]
if !ok {
return
}
v := reflect.New(t)
// 这个转换也太傻了
s := (*XStruct)(unsafe.Pointer(v.Pointer()))
s.Data = 1
fmt.Println(s)
}
PS E:\Mscript> go run .\main.go
&{1}
注册式处理指针类型
type XStruct struct {
Data int
}
func (this *XStruct) Out() {
fmt.Println(this.Data)
}
type YStruct struct {
Data string
}
var typeRegistry = make(map[string]reflect.Type)
var valRegistry = make(map[string]reflect.Value)
func Register() {
typeList := []interface{}{
new(XStruct),
new(YStruct),
}
for idx := range typeList {
inter := typeList[idx]
typeRegistry[fmt.Sprintf("%T", inter)] = reflect.TypeOf(inter)
valRegistry[fmt.Sprintf("%T", inter)] = reflect.ValueOf(inter)
}
}
func main() {
Register()
t, ok := valRegistry["*main.XStruct"]
if !ok {
return
}
v := reflect.New(t.Type().Elem()).Interface()
fmt.Println(v)
}
神奇的方法
|--common
|--common.s
|--handler.go
|--main.go
TEXT ·typelinks(SB), $0-0
JMP reflect·typelinks(SB)
func Typelinks() (sections []unsafe.Pointer, offset [][]int32) {
return typelinks()
}
func typelinks() (sections []unsafe.Pointer, offset [][]int32)
func Add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
return add(p, x, whySafe)
}
func add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
func main() {
sections, offsets := common.Typelinks()
for i, base := range sections {
for _, offset := range offsets[i] {
typeAddr := common.Add(base, uintptr(offset), "")
typ := reflect.TypeOf(*(*interface{})(unsafe.Pointer(&typeAddr)))
val := reflect.ValueOf(*(*interface{})(unsafe.Pointer(&typeAddr)))
fmt.Println(typ, val)
}
}
}
struct { root runtime.semaRoot; pad [40]uint8 }
struct { runtime.gList; n int32 }
struct { runtime.mutex; runtime.persistentAlloc }
struct { scheme tls.SignatureScheme; minModulusBytes int; maxVersion uint16 }
struct { size uint32; nmalloc uint64; nfree uint64 }
struct { sync.Mutex; m sync.Map }
struct { sync.Mutex; table [64]big.divisor }
struct { sync.Once; val int }
struct { user bool; runnable runtime.gQueue; n int32 }
struct { v interface {}; tag string; l []string }
struct {}
接口调用
interface{}interface{}
参考