Tag: reflection

C#generics列表<T>如何获取T的types?

我正在做一个reflection项目,现在我卡住了。 如果我有一个可以容纳一个List的“myclass”对象,如果属性myclass.SomList是空的,任何人都知道如何获得下面的代码中的types? List<myclass> myList = dataGenerator.getMyClasses(); lbxObjects.ItemsSource = myList; lbxObjects.SelectionChanged += lbxObjects_SelectionChanged; private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e) { Reflect(); } Private void Reflect() { foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties()) { switch (pi.PropertyType.Name.ToLower()) { case "list`1": { // This works if the List<T> contains one or more elements. Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null)); // but […]

C#使用reflection来复制基类属性

我想使用reflection来将MyObject的所有属性更新到另一个属性。 我遇到的问题是,特定的对象是从基类inheritance而来的,这些基类的属性值是不会更新的。 下面的代码复制顶级属性值。 public void Update(MyObject o) { MyObject copyObject = … FieldInfo[] myObjectFields = o.GetType().GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo fi in myObjectFields) { fi.SetValue(copyObject, fi.GetValue(o)); } } 我正在查看是否有更多的BindingFlags属性,我可以用来帮助,但无济于事。

在运行时dynamic添加C#属性

我知道有一些问题可以解决这个问题,但答案通常遵循推荐字典或参数集合的方式,这在我的情况下是行不通的。 我正在使用一个通过reflection工作的库来完成许多具有属性的对象的聪明事情。 这与定义的类以及dynamic类一起工作。 我需要更进一步,沿着这些路线做些事情: public static object GetDynamicObject(Dictionary<string,object> properties) { var myObject = new object(); foreach (var property in properties) { //This next line obviously doesn't work… myObject.AddProperty(property.Key,property.Value); } return myObject; } public void Main() { var properties = new Dictionary<string,object>(); properties.Add("Property1",aCustomClassInstance); properties.Add("Property2","TestString2"); var myObject = GetDynamicObject(properties); //Then use them like this (or rather the […]

仅在执行时调用types参数的generics方法

编辑: 当然,我真正的代码看起来不像这样。 我试图编写半伪代码,使其更清楚我想做的事情。 看起来只是把事情搞砸了。 所以,我真正想要做的是这样的: Method<Interface1>(); Method<Interface2>(); Method<Interface3>(); … 那么…我想也许我可以把它变成一个循环使用reflection。 所以问题是:我该怎么做。 我对反思的知识很浅。 所以代码示例会很好。 该scheme如下所示: public void Method<T>() where T : class {} public void AnotherMethod() { Assembly assembly = Assembly.GetExecutingAssembly(); var interfaces = from i in assembly.GetTypes() where i.Namespace == "MyNamespace.Interface" // only interfaces stored here select i; foreach(var i in interfaces) { Method<i>(); // […]

Swift支持reflection吗?

Swift支持reflection吗? 例如是否有像valueForKeyPath:和setValue:forKeyPath:对于Swift对象? 实际上,它甚至有一个dynamictypes系统,像Objective-C中的obj.class ?

任何方式来调用私有方法?

我有一个类使用XML和reflection来返回Object到另一个类。 通常情况下,这些对象是外部对象的子字段,但偶尔也是我想要生成的东西。 我已经尝试过这样的事情,但无济于事。 我相信那是因为Java不允许你访问private方法进行reflection。 Element node = outerNode.item(0); String methodName = node.getAttribute("method"); String objectName = node.getAttribute("object"); if ("SomeObject".equals(objectName)) object = someObject; else object = this; method = object.getClass().getMethod(methodName, (Class[]) null); 如果提供的方法是private ,则会失败,并显示NoSuchMethodException 。 我可以通过public这个方法来解决这个问题,或者让另一个类从中派生出来。 长话短说,我只是想知道是否有办法通过reflection访问private方法。

如何从类path中的Java包中读取所有类?

我需要读取Java包中包含的类。 这些类都在classpath中。 我需要直接从Java程序执行此任务。 你知道一个简单的方法吗? List<Class> classes = readClassesFrom("my.package")

确定对象是否是原始types

我有一个Object[]数组,我试图find那些是原始的。 我试图使用Class.isPrimitive() ,但似乎我做错了什么: int i = 3; Object o = i; System.out.println(o.getClass().getName() + ", " + o.getClass().isPrimitive()); 打印java.lang.Integer, false 。 有没有正确的方法或一些替代方法?

setMobileDataEnabled方法在Android L及更高版本中不再可调用

我已经通过Googlelogging了78084号问题 ,关于setMobileDataEnabled()方法不再可以通过reflection来调用。 它是从Android 2.1(API 7)到Android 4.4(API 19)通过reflection来调用的,但是对于Android L和更高版本,即使使用root, setMobileDataEnabled()方法也是不可调用的。 官方的回应是这个问题是“closures”,并且状态设置为“WorkingAsIntended”。 Google的简单解释是: 私有API是私有的,因为它们不稳定,可能会在没有通知的情况下消失。 是的,谷歌,我们意识到使用reflection来调用隐藏的方法的风险 – 甚至在Android出现之前 – 但是你需要提供一个更可靠的答案,如果有的话,可以实现与setMobileDataEnabled() 。 (如果您不满意Google的决定,请login问题78084 ,并尽可能多地将其显示出来,让Google知道他们错误的方式。) 所以,我的问题是:在Android设备上以编程方式启用或禁用移动networkingfunction时,我们处于死路一条吗? 这种来自Google的粗暴的方式不能适应我的生活。 如果你有解决Android 5.0(棒棒糖)及更高版本的问题,我很乐意在这个主题中听到你的回答/讨论。 我已经使用下面的代码来查看setMobileDataEnabled()方法是否可用: final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName()); final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); iConnectivityManagerField.setAccessible(true); final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE)); final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); final Method[] methods = iConnectivityManagerClass.getDeclaredMethods(); for (final Method method […]

以stringforms获取财产的名称

(请参阅下面我使用我接受的答案创build的解决scheme) 我正试图提高涉及reflection的一些代码的可维护性。 该应用程序有一个.NET Remoting接口公开(除其他外)一个名为Execute的方法,以访问未包含在其已发布的远程接口中的应用程序部分。 下面是应用程序如何指定属性(在这个例子中是一个静态的属性),这些属性可以通过执行来访问: RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty"); 所以远程用户可以调用: string response = remoteObject.Execute("SomeSecret"); 并且应用程序将使用reflection来查找SomeClass.SomeProperty并以stringforms返回其值。 不幸的是,如果有人重命名SomeProperty并忘记更改ExposeProperty()的第三个参数,它会打破这个机制。 我需要相当于: SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString() 作为ExposeProperty中的第三个参数使用,所以重构工具将负责重命名。 有没有办法做到这一点? 提前致谢。 好的,这是我最终创build的(根据我select的答案和他所引用的问题): // <summary> // Get the name of a static or instance property from a property access lambda. // </summary> // <typeparam name="T">Type of the property</typeparam> // <param name="propertyLambda">lambda expression of the form: '() => […]