Tag: reflection

我可以从当前正在执行的函数中程序地获取参数名称/值吗?

我想要做这样的事情: public MyFunction(int integerParameter, string stringParameter){ //Do this: LogParameters(); //Instead of this: //Log.Debug("integerParameter: " + integerParameter + // ", stringParameter: " + stringParameter); } public LogParameters(){ //Look up 1 level in the call stack (if possible), //Programmatically loop through the function's parameters/values //and log them to a file (with the function name as well). //If […]

C# – 从静态类获取静态属性的值

我试图循环通过一个简单的静态类中的一些静态属性,以填充他们的价值combobox,但有困难。 这是简单的课程: public static MyStaticClass() { public static string property1 = "NumberOne"; public static string property2 = "NumberTwo"; public static string property3 = "NumberThree"; } …和试图检索值的代码: Type myType = typeof(MyStaticClass); PropertyInfo[] properties = myType.GetProperties( BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (PropertyInfo property in properties) { MyComboBox.Items.Add(property.GetValue(myType, null).ToString()); } 如果我不提供任何绑定标志,那么我得到大约57个属性,包括像System.Reflection.Module模块和其他各种我不关心的其他inheritance的东西。 我的3个声明的属性不存在。 如果我提供其他标志的各种组合,那么它总是返回0属性。 大。 我的静态类实际上是在另一个非静态类中声明的吗? 我究竟做错了什么?

有没有办法使用reflection设置结构实例的属性?

我试图写一些代码,在结构上设置一个属性(重要的是它是一个结构上的属性),它是失败的: System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(); PropertyInfo propertyInfo = typeof(System.Drawing.Rectangle).GetProperty("Height"); propertyInfo.SetValue(rectangle, 5, null); 高度值(由debugging器报告)永远不会被设置为任何值 – 它保持默认值0。 之前我已经做了大量的关于类的反思,而且这个工作正常。 另外,我知道在处理结构时,如果设置一个字段,则需要使用FieldInfo.SetValueDirect,但是我不知道PropertyInfo的等价物。

界面的实现通过Reflection

如何通过在C#中reflection来获取接口的所有实现?

对于一个对象,我可以使用reflection或其他方式获得其所有的子类?

对于一个对象,我可以使用reflection来获得它的所有子类吗?

如何testingMethodInfo.ReturnType是否是System.Void的types?

使用reflection来获得一个MethodInfo,我想testing返回的types是typeof System.Void。 testing它是否是System.Int32正常工作 myMethodInfo.ReturnType == typeof(System.Int32) 但 myMethodInfo.ReturnType == typeof(System.Void) 不编译? 目前我正在testing如果名称的string表示是“System.Void”,这似乎是非常错误的。

Java的toString()使用reflection?

有一天,我正在为Java中的类写一个toString(),方法是将类的每个元素手动写入一个string,然后发现使用reflection可能会创build一个可以工作的通用toString()方法在所有类上。 IE会找出字段名称和值,并把它们发送到一个string。 获取字段名称非常简单,这是一名同事想出来的: public static List initFieldArray(String className) throws ClassNotFoundException { Class c = Class.forName(className); Field field[] = c.getFields(); List<String> classFields = new ArrayList(field.length); for (int i = 0; i < field.length; i++) { String cf = field[i].toString(); classFields.add(cf.substring(cf.lastIndexOf(".") + 1)); } return classFields; } 使用工厂,我可以通过一次存储字段来减less性能开销,第一次调用toString()。 然而,find价值可能会更昂贵。 由于reflection的performance,这可能是比较实际的假设。 但是我对反思的想法感兴趣,并且我可以用它来改进我的日常节目。

为什么我应该关心Delphi中的RTTI?

我听说过很多关于Delphi 2010新的/改进的RTTIfunction ,但我必须承认我的无知……我不明白这一点。 我知道Delphi的每个版本都支持RTTI …我知道RTTI(运行时types信息)允许我在运行应用程序时访问types信息。 但究竟是什么意思呢? Delphi 2010的RTTI支持与.NET中的reflection相同吗? 有人可以解释为什么RTTI是有用的吗? 假装我是你尖尖的头发老板,并帮助我理解为什么RTTI很酷。 我如何在真实世界的应用程序中使用它?

如何让Java方法返回任何types的generics列表?

我想写一个方法,将返回任何types的java.util.List , 而不需要任何types的注释 : List<User> users = magicalListGetter(User.class); List<Vehicle> vehicles = magicalListGetter(Vehicle.class); List<String> strings = magicalListGetter(String.class); 方法签名是什么样的? 像这样的东西,也许(?): public List<<?> ?> magicalListGetter(Class<?> clazz) { List<?> list = doMagicalVooDooHere(); return list; } 提前致谢!

为什么允许通过reflection访问Java私有字段?

考虑这个例子: import java.lang.reflect.Field; public class Test { public static void main(String[] args) { C c = new C(); try { Field f = C.class.getDeclaredField("a"); f.setAccessible(true); Integer i = (Integer)f.get(c); System.out.println(i); } catch (Exception e) {} } } class C { private Integer a =6; } 你被允许通过反思访问私人领域的课程似乎是不合逻辑的。 为什么这样的function可用? 允许这种访问不是“危险的”吗?