我正在编写一些代码,需要能够从调用类的方法中获取参数的值.我知道如何一直到ParameterInfo []数组,但我不知道如何获取值.这甚至可能吗?
如果是,我认为它与使用MethodInfo对象的MethodBody属性有关,它允许你检查IL流,包括属性,但我不知道怎么做,我还没找到适用于Google的代码.
// Finds calling method from class that called into this one
public class SomeClass
{
public static void FindMethod()
{
for (int i = 1; i < frameCount; i++)
{
var frame = new StackFrame(i);
var methodInfo = frame.GetMethod();
if (methodInfo.DeclaringType != this.GetType())
{
string methodName = frame.GetMethod().Name;
var paramInfos = methodInfo.GetParameters();
// Now what?? How do I get the values from the paramInfos
break;
}
else if (i == frameCount - 1) …Run Code Online (Sandbox Code Playgroud) 我想确定通用对象类型("T")方法类型参数是否是集合类型.我通常会将T通过作为Generic.List发送,但它可以是任何集合类型,因为它在辅助函数中使用.
我是否最好测试它是否实现IEnumerable <T>?
如果是这样,代码会是什么样的?
更新格林尼治标准时间14:17 + 10可能会扩展到这里的解决方案(但是如果列表派生的话,它只适用于List <T>而不是IEnumerable <T>)
T currentObj;
// works if currentObj is List<T>
currentObj.GetType().GetGenericTypeDefinition() == typeof(List<>)
// does not work if currentObj is List<T>
currentObj.GetType().GetGenericTypeDefinition() == typeof(IEnumerable<>)
Run Code Online (Sandbox Code Playgroud) public class Foo
{
public string Bar {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何通过反射获得字符串属性Bar的值?如果PropertyInfo类型是System.String,则以下代码将引发异常
Foo f = new Foo();
f.Bar = "Jon Skeet is god.";
foreach(var property in f.GetType().GetProperties())
{
object o = property.GetValue(f,null); //throws exception TargetParameterCountException for String type
}
Run Code Online (Sandbox Code Playgroud)
看来我的问题是该属性是一个索引器类型,带有System.String.
另外,如何判断该属性是否为索引器?
因此,经过几个小时的解决方法,目前在Google App Engine上禁用了Reflection的限制,我想知道是否有人可以帮助我理解为什么对象反射可能构成威胁.是因为我可以检查一个类的私有变量还是有其他更深层次的原因?
我试图使用Java Reflection获取和调用驻留在不同类中的受保护方法以及不同的包.
包含受保护方法的类:
package com.myapp;
public class MyServiceImpl {
protected List<String> retrieveItems(String status) {
// Implementation
}
}
Run Code Online (Sandbox Code Playgroud)
通话课程:
package xxx.myapp.tests;
import com.myapp.MyServiceImpl;
public class MyTestCase {
List<String> items;
public void setUp() throws Exception {
MyServiceImpl service = new MyServiceImpl();
Class clazz = service.getClass();
// Fails at the next line:
Method retrieveItems = clazz.getDeclaredMethod("retrieveItems");
// How to invoke the method and return List<String> items?
// tried this but it fails?
retrieveItems.invoke(clazz, "S");
}
}
Run Code Online (Sandbox Code Playgroud)
编译器抛出此异常:
java.lang.NoSuchMethodException: com.myapp.MyServiceImpl.retrieveItems()
Run Code Online (Sandbox Code Playgroud) 关于" 通过反射实现接口 "的答案显示了如何获得接口的所有实现.但是,给定通用接口,IInterface<T>以下不起作用:
var types = TypesImplementingInterface(typeof(IInterface<>))
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释我如何修改该方法?
我正在尝试读取给定对象的所有属性,只读取在对象类型上声明的那些属性,不包括那些继承的对象.IE:
class Parent {
public string A { get; set; }
}
class Child : Parent {
public string B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以我想只让B回来.阅读文档,我假设下面是我需要的,但实际上根本没有返回任何东西.
var names = InstanceOfChild.GetType().GetProperties(BindingFlags.DeclaredOnly).Select(pi => pi.Name).ToList();
Run Code Online (Sandbox Code Playgroud) 我曾经认为元编程涉及修改程序,并且(对于什么是反射及其为什么有用?)的一些答案,反思仅仅包括对程序的内省.但是,反射标签维基说
反射是程序在运行时可以观察和 修改自身结构和行为的过程.
反思是程序可以执行内省的过程.这种内省通常涉及在运行时观察和修改自身结构和行为的能力.从理论角度来看,反思涉及程序指令作为数据存储的事实.程序代码和数据之间的区别在于如何处理信息.因此,程序可以将自己的代码视为数据并观察或修改它们.
[强调补充]
元编程的描述是
元编程正在编写编写或操纵其他程序作为其数据的程序.
元编程很有用,因为它可以节省程序员宝贵的时间.有些语言支持元编程本身,这允许创建具有强大表现力的代码.
(我认为"写"并不意味着将源代码写入文件,因为这是代码生成.)
这会使元编程只是反射的一个子集吗?
或者这些术语是不同的,因为某些编程语言是由另一种语言元编程的,在这种情况下,元编程会发生而不是反射?(在维基百科的元编程文章中有一个未被引用的句子声称这个)
或者"反射"和"元编程"这两个术语的使用方式会有所不同,具体取决于该人使用的编程语言?
是否有可以执行以下操作的库?:
给定一个Object和一个HashMap,它枚举Hashmap的键,并在Object中查找这些键的setter并设置相关的值.看起来像这样的东西:
public Object setData(Object object, HashMap<String, Object> fields) {
for (Entry<String, Object> entry : fields.entrySet()) {
Method m = object.getClass().getMethod("set" + entry.getKey(), entry.getValue().getClass());
if (m != null) {
m.invoke(object, entry.getValue());
}
}
return object;
}
Run Code Online (Sandbox Code Playgroud)
这个任务看起来很简单,但我希望有人已经注意到了一些细微差别.如你所知,重新发明轮子(好轮子)是一种糟糕的方法.
没有继承但只有反射是否可以在C#中动态更改方法的代码?
就像是 :
nameSpaceA.Foo.method1 = aDelegate;
Run Code Online (Sandbox Code Playgroud)
我不能改变/编辑Foo类.
namespace nameSpaceA
{
class Foo
{
private void method1()
{
// ... some Code
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是动态改变代码:
public static IList<XPathNavigator> EnsureNodeSet(IList<XPathItem> listItems);
Run Code Online (Sandbox Code Playgroud)
在System.Xml.Xsl.Runtime.XslConvert.cs中
转:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, string.Empty);
Run Code Online (Sandbox Code Playgroud)
进入:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, item.value);
Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×6
java ×3
.net ×2
dynamic ×1
generics ×1
interface ×1
junit ×1
pojo ×1
properties ×1
protected ×1
security ×1
string ×1
terminology ×1