reflection

Run unit tests on dynamically created DLL

≯℡__Kan透↙ 提交于 2019-12-24 16:32:00
问题 I am thinking on how to implement this , and seems like my knowledge is too poor to understand the implementation. I have a code that compiles source code to DLL. Then I need somehow to run Unit test on this Dll , and check 3-4 methods inside. I am trying to run unit tests this way. CompilerResults compileResults = codeProvider.CompileAssemblyFromSource(compilerParameters, new string[] { sourceCode }); Assembly myAssembly = compileResults.CompiledAssembly; TestPackage testPackage = new

Cast a variable to a Type and call Methods

旧巷老猫 提交于 2019-12-24 16:28:43
问题 How would I go about invoking a method call for an Object after casting it to a Type? I have a KeyValuePair which stores the type of the object and the object itself. I then want to cast this object to its key type and invoke a method of that class type. KeyValuePair<Type, Object> client = myClients.Find( delegate(KeyValuePair<Type, Object> result) { return (result.Key == myClients[clientNumber].Key); // Match client of the same type } ); if (client.Value != null) { // cast client.Value to

Reflection : returning wrong fields

余生长醉 提交于 2019-12-24 16:13:02
问题 I was trying to use reflection API to get the fields of a class, I was doing it by passing the the class as an argument to the following method private void someMethod(Class<?> objClass) throws IOException { String className= objClass.getSimpleName(); Map<String, String> fieldsAndDataType = new LinkedHashMap<>(); for (Field field : objClass.getClass().getDeclaredFields()) { String fieldName = field.getName(); String fieldDataType = field.getType().toString(); fieldsAndDataType.put(fieldName,

C# generics wrapper generator

白昼怎懂夜的黑 提交于 2019-12-24 15:57:38
问题 I'm looking for a code generator (preferably with source) that will allow me to generate wrapper classes (using reflection) for generic classes that i have in my code. I've done a bit of searching but can't seem to find anything that will do what i need here, so i thought i'd ask here before i start writing my own. Ultimately, i'll probably have to write my own but would like to at least get a head start if someone's already written something like this. It's a little difficult to explain what

Can't Access Runtime Cache Using Reflection

别来无恙 提交于 2019-12-24 15:53:48
问题 I am trying to get the expiry date of a HttpRuntime.Cache object, as per this answer: https://stackoverflow.com/a/350374/1778169 The method is: private DateTime GetCacheUtcExpiryDateTime(string cacheKey) { object cacheEntry = Cache.GetType().GetMethod("Get", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(Cache, new object[] { cacheKey, 1 }); PropertyInfo utcExpiresProperty = cacheEntry.GetType().GetProperty("UtcExpires", BindingFlags.NonPublic | BindingFlags.Instance); DateTime

Java, method name via reflection for logging

折月煮酒 提交于 2019-12-24 15:19:07
问题 Working on a quick logging utility method. Normally I would use Aspects for this but working in the Android SDK where there isnt support for that. How would I go about grabbing a method name in a generic way using reflection? Also any performance hit in java with reflection ? 回答1: Something like this will probably work: Thread.currentThread().getStackTrace()[0].getMethodName() 来源: https://stackoverflow.com/questions/28948076/java-method-name-via-reflection-for-logging

Method.getAnnotations() method giving different outputs for java 1.7.0_79 and java 1.7.0_80 versions

早过忘川 提交于 2019-12-24 15:13:55
问题 I am getting different output for the same code while using java 1.7.0_79 and 1.7.0_80 versions for compilation and running. The test code snippet looks like this : interface Constraint<T> extends Serializable { T getValue(); } @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @interface NonZero { } public class ReflectionTest { @SuppressWarnings("serial") public static void main(String[] args) { Constraint<Integer> c = new Constraint<Integer>() { @Override @NonZero public

Compare two pojo and output difference to another pojo

一世执手 提交于 2019-12-24 15:04:02
问题 I have a pojo class as follows. public class Person { private String name; private Address address; public String getName() { ... } public Address getAddress() { ... } //Address bean public void setName() { ... } public void setAddress() { ... } } Get data from DB (json converted to above POJO) Show it to user User changes his Address Saving back to DB This is what currently happening. Now I am trying to make a delta of user made changes Vs database changes. (which is address alone) I need to

passing static reflection information to static generic methods

风流意气都作罢 提交于 2019-12-24 14:28:44
问题 EDIT: the class/method that i'm trying to run this inside is static and therefore i'm unable to pass this into the generic.Invoke I have a static Data Access Class that i use to automatically parse data from various sources. i was starting to re-factor it when i ran into a problem. Im tring to pass a Type to a Generic method via reflection, (the method then parses the type and returns the Type with a value) my code currently looks like Type type1 = typeof( T ); var item = (T)Activator

Java reflection: array of classes vs. array of parameter types

大城市里の小女人 提交于 2019-12-24 14:27:06
问题 I have an object m of class Method. I invoked m.getParameterTypes() to create an array params of the method's parameters. I also have an array arr of Objects. I wanted to check if the objects in arr are of the same types as the types in params (and in the same order). What I did was to create an array classes which includes the class of each object in arr. Then I tried to compare classes with params. Turns out they can never be equal because the classes in the classes array are in the format