reflection

AppDomain.CurrentDomain.GetAssemblies fails with ReflectionTypeLoadException

南笙酒味 提交于 2020-01-02 03:28:07
问题 During unittesting I have run into a problem with the following code that asks for all the loaded assemblies: var res = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) .ToList(); this code fails with a ReflectionTypeLoadException which has inner exceptions of the pattern Could not load type Microsoft.Xml.Serialization.GeneratedAssembly.FOO where FOO are some specific classes also coded by us. The problem arises when running unittests prior to the above which creates XML

Parsing Objects from String in Java

最后都变了- 提交于 2020-01-02 03:11:15
问题 I am trying to write a general method to parse objects from strings. To be clear, I have the following not-so-elegant implementation: public static Object parseObjectFromString(String s, Class class) throws Exception { String className = class.getSimpleName(); if(className.equals("Integer")) { return Integer.parseInt(s); } else if(className.equals("Float")) { return Float.parseFloat(s); } else if ... } Is there a better way to implement this? 回答1: Your method can have a single line of code:

Why doesn't Type.GetFields() return backing fields in a base class?

孤街醉人 提交于 2020-01-02 03:00:49
问题 In C#, if you use Type.GetFields() with a type representing a derived class, it will return a) all explicitly declared fields in the derived class, b) all backing fields of automatic properties in the derived class and c) all explicitly declared fields in the base class. Why are the d) backing fields of automatic properties in the base class missing? Example: public class Base { public int Foo { get; set; } } public class Derived : Base { public int Bar { get; set; } } class Program { static

Access to private method using reflection in C#/Silverlight applications

蹲街弑〆低调 提交于 2020-01-02 02:34:54
问题 My code invokes method using reflection: scoringType.InvokeMember("scoringClient_ScorePostsCompleted", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, scoringInstance, new object[] { sArg, eArg }); where scoringInstance is an instance of a ModelView class. The method is private, but I use BindingFlags.NonPublic, so, i should be able to access it, but I cannot - I get MethodAccessException exception: "Attempt by method ... to access method ... failed." Google

Java Reflection get the Instance from a Field

匆匆过客 提交于 2020-01-02 02:28:14
问题 is there any way to get the Instance from a Field? Here's a sample code: public class Apple { // ... a bunch of stuffs.. } public class Person { @MyAnnotation(value=123) private Apple apple; } public class AppleList { public add(Apple apple) { //... } } public class Main { public static void main(String args[]) { Person person = new Person(); Field field = person.getClass().getDeclaredField("apple"); // Do some random stuffs with the annotation ... AppleList appleList = new AppleList(); //

Expression tree for String.IndexOf method

旧巷老猫 提交于 2020-01-02 02:28:08
问题 How should I construct Expression tree for string.IndexOf("substring", StringComparison.OrdinalIgnoreCase) ? I can get it working without the second argument: StringComparison.OrdinalIgnoreCase . These are my attempts so far: var methodCall = typeof (string).GetMethod("IndexOf", new[] {typeof (string)}); Expression[] parms = new Expression[]{right, Expression.Constant("StringComparison.OrdinalIgnoreCase", typeof (Enum))}; var exp = Expression.Call(left, methodCall, parms); return exp; Also

.NET Framework: Get Type from TypeInfo

左心房为你撑大大i 提交于 2020-01-02 02:23:46
问题 The new reflection API introduces the TypeInfo class: https://msdn.microsoft.com/en-us/library/system.reflection.typeinfo(v=vs.110).aspx I can get a TypeInfo instance of a Type (say, a Car ) by writing TypeInfo typeInfo = typeof(Car).GetTypeInfo(); Now, what if I just have a TypeInfo instance, how do I get the Type its refering to? Can I just write Type type = typeInfo.GetType(); Or will this return a type that is equal to typeof(TypeInfo) ? 回答1: If you call typeInfo.GetType() , you will

Can a transient field in a class be obtained using reflection

我的未来我决定 提交于 2020-01-02 02:01:11
问题 Can a transient field in a class be obtained using reflection? (using getDeclaredField(..) ) 回答1: Yes , It is a normal field. You can check whether it is transient by: Modifier.isTransient(field.getModifiers()); transient : A keyword in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient

TYPE_USE annotations get lost when type is nested, generic interface [duplicate]

余生长醉 提交于 2020-01-02 01:17:44
问题 This question already has answers here : Why annotation on generic type argument is not visible for nested type? (2 answers) Closed 2 years ago . It appears that TYPE_USE annotations cannot be accessed through reflection when the annotated type is a nested, generic interface. Please observe the following example: import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang

How can I check a class has no arguments constructor

≡放荡痞女 提交于 2020-01-02 01:12:21
问题 Object obj = new Object(); try { obj.getClass().getConstructor(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { dosomething(); e.printStackTrace(); } I don't want check like this, because it throw a Exception. Is there another way? 回答1: You can get all Constructor s and check their number of parameters, stopping when you find one that has 0. private boolean hasParameterlessPublicConstructor(Class<?> clazz) { for (Constructor<?> constructor : clazz