草庐IT

Reflection

全部标签

reflection - 为什么可以使用反射在 slice 中设置值?

根据TheLawsofReflection无法按如下方式设置元素的值,您需要使用其地址。这行不通:varxfloat64=3.4v:=reflect.ValueOf(x)v.SetFloat(7.1)//Error:willpanic.这会起作用:varxfloat64=3.4p:=reflect.ValueOf(&x)//Note:taketheaddressofx.p.Elem().SetFloat(7.1)那么为什么在使用slice时以下会起作用?floats:=[]float64{3}v:=reflect.ValueOf(floats)e:=v.Index(0)e.SetFlo

reflection - 反射(reflect) []byte 的值

如何获取此接口(interface)的[]byte值?packagemainimport("reflect")funcbyteInterface()interface{}{return[]byte("foo")}funcmain(){//varb[]bytei:=byteInterface()switch{casereflect.TypeOf(i).Kind()==reflect.Slice&&(reflect.TypeOf(i)==reflect.TypeOf([]byte(nil))):default:panic("shouldhavebytes")}}

reflection - 无需编译即可检查变量实现接口(interface)

我想知Prop体类型是否实现了特定接口(interface)并打印出来。我已经编写了一个示例[0],其中包含一个自定义结构(MyPoint),而不是接口(interface)类型。MyPoint具有在io.Reader接口(interface)中定义的Read函数:typeMyPointstruct{X,Yint}func(pnt*MyPoint)Read(p[]byte)(nint,errerror){return42,nil}目的是获取具体类型p实现接口(interface)io.Writer的信息。因此,我写了一个简短的maintrieng来获得一个真实的支票。funcmain(

reflection - golang reflect如何判断零值

packagemainimport("fmt""reflect")funcmain(){varp1*string=nilvarvinterface{}=p1val:=reflect.Indirect(reflect.ValueOf(v))ifv==nil{fmt.Printf("NULL")}else{ifval.CanInterface(){fmt.Printf("ifis%v\n",val.Interface())}}}这个程序的输出是:···panic:reflect:callofreflect.Value.CanInterfaceonzeroValuegoroutine1[ru

reflection - 如何使用反射在golang中创建具有给定名称(字符串)的数组

我想在golang中创建带有名称的数组,但是我遇到了一些错误这是我的代码包主import("fmt""reflect")typeMystruct{NamestringIdint}funcmain(){my:=&My{}myType:=reflect.TypeOf(my)fmt.Println(myType)//v:=reflect.New(myType).Elem().Interface()//IwanttomakearraywithMy//a:=make([](myType.(type),0)//cancompile//a:=make([]v.(type),0)////cancomp

reflection - 如何打开 reflect.Type?

我已经设法做到了,但看起来效率不高:vartreflect.Typeswitcht{casereflect.TypeOf(([]uint8)(nil))://handle[]uint8arraytype} 最佳答案 第一个问题,你确定要打开reflect.Type而不是使用类型开关吗?示例:switchx:=y.(type){case[]uint8://xisnowa[]uint8}假设这对您的情况不起作用,我的建议是创建这些包变量。示例:varuint8SliceType=reflect.TypeOf(([]uint8)(nil)

reflection - 如何在接口(interface) slice 上设置结构变量的值?

如何使用反射设置[]interface{}{}中包含的testEntity.Val?typetestEntitystruct{Valint}funcmain(){slice:=[]interface{}{testEntity{Val:3}}sliceValue:=reflect.ValueOf(slice)elemValue:=sliceValue.Index(0)//Prints:cansetfalsefmt.Println("canset",elemValue.Elem().Field(0).CanSet())}http://play.golang.org/p/lxtmu9ydda

reflection - 在 Go 中应该避免使用反射吗?

我是Go的新手,也是反射的新手,但是应该和可以reflect的用法在Go中避免包?是否存在无法避免反射的场景? 最佳答案 在一些问题领域中,反射使编写可重用库变得更加容易:编码/解码,标准库中有大量示例,例如encoding/json,encoding/xml格式化,例如text/template,html/template,fmt.Printf.但是使用反射需要付出代价:编译时错误变成运行时错误(例如fmt.Printf("%d",stringVariable))性能变差通常存在不需要反射的替代解决方案,例如代码生成,它被prot

c# - Reflection.Emit 的 Java 等价物

据我所知,Java没有与C#的Reflection.Emit类似的东西。是否还有其他提供类似功能的Java库?有什么区别(反射发射)? 最佳答案 TheByteCodeEngineeringLibrary(BCEL) 关于c#-Reflection.Emit的Java等价物,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2259323/

java - 反射(reflection) - Java 8 - 无效的常量类型

我对Reflections有疑问图书馆。我正在尝试动态加载所有实现特定接口(interface)的类。只要我不在这些类(java8)中使用lambda表达式,一切正常(所有类都已加载)。我尝试升级lib版本但效果相同(java.io.IOException:无效常量类型:18)。依赖和构建在pom.xmlorg.reflectionsreflections0.9.10javassistjavassistorg.javassistjavassist3.19.0-GAorg.apache.maven.pluginsmaven-compiler-plugin1.81.8不排除也是一样的效果。