reflection

How to cast an object to a Type extracted at runtime

不想你离开。 提交于 2020-01-12 07:27:09
问题 I am using reflection to get an object's type, or for this issue an object that has instance properties type, at runtime and then I need to change an existing variable's type into that newly found type. Is this possible? For example, the following code does not work in the line indicated within: Public Sub DoSomething(x As T, y As T, exp As String) 'This is a instance property on the object of a different type 'i.e. 'T.AnotherType' We need to reflect to find out what type of object

Detect access modifier type on a property using Reflection

天涯浪子 提交于 2020-01-12 06:42:15
问题 I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection. However I need to find out if the property is public or protected. eg: public string Name{get;set;} protected int Age{get;set;} The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this? 回答1: Since properties are just syntactic sugar over a pair of get / set methods, there's no such thing as

Detect access modifier type on a property using Reflection

孤人 提交于 2020-01-12 06:40:06
问题 I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection. However I need to find out if the property is public or protected. eg: public string Name{get;set;} protected int Age{get;set;} The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this? 回答1: Since properties are just syntactic sugar over a pair of get / set methods, there's no such thing as

How to serialize ANY Object into a URI?

烈酒焚心 提交于 2020-01-12 04:44:25
问题 My basic question: is there anything built that already does this automatically (doesn't have to be part of a popular library/package)? The main things I'm working with are Spring (MVC) and Jackson2. I understand there are a few manual ways to do this: Create a method in each class that serializes its specific properties into property=value& form (kind of stinks because it's a bunch of logic duplication, I feel). Create a function that accepts an object, and uses reflection to dynamically

How to run all methods with a given annotation?

大城市里の小女人 提交于 2020-01-12 04:04:06
问题 This is what I want to happen: public class MainClass { public static void main(String args[]) { run @mod(); // run all methods annotated with @mod annotation } } The annotation declaration: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface mod { String name() default ""; } The methods to be called: public class Good { @mod (name = "me1") public void calledcs(){ System.out.println("called"); } @mod (name = "me2") public void calledcs2(){ System.out.println(

Using reflection to call an ASP.NET web service

倾然丶 夕夏残阳落幕 提交于 2020-01-12 02:30:41
问题 Say I have an ASMX web service, MyService. The service has a method, MyMethod. I could execute MyMethod on the server side as follows: MyService service = new MyService(); service.MyMethod(); I need to do similar, with service and method not known until runtime. I'm assuming that reflection is the way to go about that. Unfortunately, I'm having a hard time making it work. When I execute this code: Type.GetType("MyService", true); It throws this error: Could not load type 'MyService' from

How to create a Python class decorator that is able to wrap instance, class and static methods?

回眸只為那壹抹淺笑 提交于 2020-01-11 19:42:06
问题 I'd like to create a Python class decorator (*) that would be able to seamlessly wrap all method types the class might have: instance, class and static. This is the code I have for now, with the parts that break it commented: def wrapItUp(method): def wrapped(*args, **kwargs): print "This method call was wrapped!" return method(*args, **kwargs) return wrapped dundersICareAbout = ["__init__", "__str__", "__repr__"]#, "__new__"] def doICareAboutThisOne(cls, methodName): return (callable(getattr

C#.NET - Type.GetType(“System.Windows.Forms.Form”) returns null

倖福魔咒の 提交于 2020-01-11 13:25:25
问题 I have a snippet of code in my application ( which references System.Windows.Forms ) which attempts to resolve type information for a Form class like so: Type tForm = Type.GetType("System.Windows.Forms.Form"); dynamic instance = Activator.CreateInstance(tForm); but Activator.CreateInstance fails because tForm is null. How do I solve this? EDIT: Types MUST be resolvable at runtime! 回答1: You need to use assembly-qualified name of the type Type tForm = Type.GetType("System.Windows.Forms.Form,

C#.NET - Type.GetType(“System.Windows.Forms.Form”) returns null

喜你入骨 提交于 2020-01-11 13:25:12
问题 I have a snippet of code in my application ( which references System.Windows.Forms ) which attempts to resolve type information for a Form class like so: Type tForm = Type.GetType("System.Windows.Forms.Form"); dynamic instance = Activator.CreateInstance(tForm); but Activator.CreateInstance fails because tForm is null. How do I solve this? EDIT: Types MUST be resolvable at runtime! 回答1: You need to use assembly-qualified name of the type Type tForm = Type.GetType("System.Windows.Forms.Form,

How to test whether the value of a Java field gotten by reflection is null?

半城伤御伤魂 提交于 2020-01-11 12:21:32
问题 I have Field f = this.getClass().getFields()[0]; I need to know if f 's value in this is null or not. There are many methods like getInt() and getDouble() , but I have not found a method like Object getData() or isNull() . Is there such a method? 回答1: field.get(target) returns Object . So you can check if (field.get(this) == null) {..} If the field is primitive, it will get wrapped. int -> Integer , char -> Character , etc. 回答2: You need to get the field from the object then check if it is