reflection

Invoking Private / Protected Methods Via Reflection From The Same Object Instance (or Base)

余生长醉 提交于 2020-01-02 01:10:11
问题 Is it possible to call a protected method via reflection. I am using this: Me.GetType.InvokeMember(Stages(CurrentStage), Reflection.BindingFlags.InvokeMethod, Nothing, Me, Nothing) Which always calls a method with no params or return. The idea is that the user of my object (transactional processing of business logic) inherits the base class adds in a load of private methods to fire. They then add these method names to a list in the order they would like them fired and the code above will take

How to get constructor as MethodInfo using Reflection

送分小仙女□ 提交于 2020-01-02 00:50:10
问题 The constructor looks like this: public NameAndValue(string name, string value) I need to get it as a MethodInfo using Reflection. It tried the following, but it does not find the constructor ( GetMethod returns null ). MethodInfo constructor = typeof(NameAndValue).GetMethod(".ctor", new[] { typeof(string), typeof(string) }); What am I doing wrong? 回答1: Type.GetConstructor. Note this returns a ConstructorInfo rather than a MethodInfo, but they both derive from MethodBase so have mostly the

How to check if a property is virtual with reflection?

谁说胖子不能爱 提交于 2020-01-01 23:54:07
问题 Given an object, how can I tell if that object has virtual properties? var entity = repository.GetByID(entityId); I tried looking in: PropertyInfo[] properties = entity.GetType().GetProperties(); But couldn't discern if any of the properties would indicate virtual. 回答1: PropertyInfo[] properties = entity.GetType().GetProperties() .Where(p => p.GetMethod.IsVirtual).ToArray(); Or, for .NET 4 and below: PropertyInfo[] properties = entity.GetType().GetProperties() .Where(p => p.GetGetMethod()

Android: Extending a hidden class that is obtained through reflection

青春壹個敷衍的年華 提交于 2020-01-01 19:19:18
问题 How can I extend a class that is only accessible through reflection? Basically, I am trying to extend the com.samsung.bluetoothle.BluetoothLEClientProfile class that is found in the Galaxy S3 to enable communication it with my bluetooth LE device. The class is hidden and I need to extend it. 回答1: Add a dummy class in the right package ( com.samsung.bluetoothle ) in your project, then extend it. At runtime the system class will be loaded, and you should get the desired behaviour. 来源: https:/

How to find the parameterized type of the return type through inspection?

我只是一个虾纸丫 提交于 2020-01-01 18:18:21
问题 I am using reflection to get all the get all the methods in a class like this: Method[] allMethods = c.getDeclaredMethods(); After that I am iterating through the methods for (Method m: allMethods){ //I want to find out if the return is is a parameterized type or not m.getReturnType(); } For example: if I have a method like this one: public Set<Cat> getCats(); How can I use reflection to find out the return type contain Cat as the parameterized type? 回答1: Have you tried getGenericReturnType()

Getting the name of a property in a list

瘦欲@ 提交于 2020-01-01 17:36:28
问题 Example public class MyItems { public object Test1 {get ; set; } public object Test2 {get ; set; } public object Test3 {get ; set; } public object Test4 {get ; set; } public List<object> itemList { get { return new List<object> { Test1,Test2,Test3,Test4 } } } } public void LoadItems() { foreach (var item in MyItems.itemList) { //get name of item here (asin, Test1, Test2) } } ** I have tried this with reflection.. asin typeof(MyItems).GetFields() etc.. but that doesn't work. How can I find out

Reflectively get all packages in a project?

蹲街弑〆低调 提交于 2020-01-01 17:30:13
问题 How can I reflectively get all of the packages in the project? I started with Package.getPackages(), but that only got all the packages associated to the current package. Is there a way to do this? 回答1: @PhilippWendler's comment led me to a method of accomplishing what I needed. I tweaked the method a little to make it recursive. /** * Recursively fetches a list of all the classes in a given * directory (and sub-directories) that have the @UnitTestable * annotation. * @param packageName The

Reflection in swift 2

被刻印的时光 ゝ 提交于 2020-01-01 16:43:10
问题 I have a class User: import UIKit import ObjectMapper class User: NSObject, CustomStringConvertible, Mappable { var FirstName: NSString! ; var LastName: NSString! ; required init?(_ map: Map){ } func mapping(map: Map) { FirstName <- map["FirstName"] LastName <- map["LastName"] } override var description:String { var s:String="" //USE REFLECTION TO GET NAME AND VALUE OF DATA MEMBERS for var index=1; index<reflect(self).count; ++index { s += (reflect(self)[index].0 + ": "+reflect(self)[index].1

Change annotation value of field annotation in Java 8

佐手、 提交于 2020-01-01 14:39:35
问题 I think title describes the problem. Here's some code: import static org.junit.Assert.assertEquals; import java.lang.annotation.*; public class Main { public static void main(String[] args) throws Exception { assertEquals("foo", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); // the magic here -> set to bar assertEquals("bar", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); } @Anno(param = "foo") public void myMethod() {} @Retention

Change annotation value of field annotation in Java 8

﹥>﹥吖頭↗ 提交于 2020-01-01 14:37:56
问题 I think title describes the problem. Here's some code: import static org.junit.Assert.assertEquals; import java.lang.annotation.*; public class Main { public static void main(String[] args) throws Exception { assertEquals("foo", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); // the magic here -> set to bar assertEquals("bar", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param()); } @Anno(param = "foo") public void myMethod() {} @Retention