reflection

How to provide conversion when using Reflection.SetValue?

余生颓废 提交于 2019-12-29 07:57:28
问题 I have a class that pretends to be an int, so it has overloaded the various operators; public class MyId { int value; public virtual int Value { get { return this.value; } set { this.value = value; } } public MyId(int value) { this.value = value; } public static implicit operator MyId(int rhs) { return new MyId(rhs); } public static implicit operator int(MyId rhs) { return rhs.Value; } } However, when I use code like PropertyInfo.SetValue(myObj, 13, null) OR MyId myId = 13; int x = Convert

DynamicMethod with generic type parameters

霸气de小男生 提交于 2019-12-29 07:40:06
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

DynamicMethod with generic type parameters

风格不统一 提交于 2019-12-29 07:40:05
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

Android: how to code depending on the version of the API?

三世轮回 提交于 2019-12-29 06:49:30
问题 In Android I get the version of the SDK easily ( Build.VERSION.SDK ) but I need to use LabeledIntent only if the platform is newer than 1.6 ( >Build.VERSION_CODES.DONUT ) I suppose that Reflection is necessary (I have read this link but it is not clear for a class or to me). This is the code but it gives me an exception because in my Android 1.6, the compiler verifies if the package exists even if the condition is not applied: Intent theIntent=....; if(Integer.parseInt(Build.VERSION.SDK) >

checking whether a package is existent or not

亡梦爱人 提交于 2019-12-29 06:48:40
问题 How can i check whether a package like javax.servlet.* exists or not in my installation of java? 回答1: Java can only tell you if it can load a class. It can't tell you if a package exists or not because packages aren't loaded, only classes. The only way would be by trying to load a class from that package. e.g., For javax.servlet.* you could do: try { Class.forName("javax.servlet.Filter"); return true; } catch(Exception e) { return false; } 回答2: Check if package is present as a resource: //

How to list all properties exposed by a Java class and its ancestors in Eclipse?

你。 提交于 2019-12-29 06:47:12
问题 Given a single Java class I'd like to be able to list all properties that are exposed in all ancestors and recursively traverse all their exposed properties (i.e. public or with getters/setters) in the same way. Easier to explain with a simple example: public class BaseClass1 { private int intProperty; // has getter and setter (not shown) } public class SubClass1 extends BaseClass1 { private int privateSoNotListed; public SubClass2 subClass2Property; } public class BaseClass2 { public String

Java Reflection - Object is not an instance of declaring class

感情迁移 提交于 2019-12-29 06:34:05
问题 This question is being asked everywhere on Google but I'm still having trouble with it. Here is what I'm trying to do. So like my title states, I'm getting an 'object is not an instance of declaring class' error. Any ideas? Thanks! Main.java Class<?> base = Class.forName("server.functions.TestFunction"); Method serverMethod = base.getMethod("execute", HashMap.class); serverMethod.invoke(base, new HashMap<String, String>()); TestFunction.java package server.functions; import java.util.HashMap;

Get Method Name Using Lambda Expression

有些话、适合烂在心里 提交于 2019-12-29 04:40:13
问题 I'm trying to get the name of a method on a type using a lambda expression. I'm using Windows Identity Foundation and need to define access policies with the type name with namespace as a resource and the method name as the action. Here is an example. This is the type I would be getting the type name and method name from: namespace My.OrderEntry { public class Order { public void AddItem(string itemNumber, int quantity) {} } } This is how I would like to define the access policy through a DSL

How do I create and access a new instance of an Anonymous Class passed as a parameter in C#?

守給你的承諾、 提交于 2019-12-29 04:02:26
问题 I have created a function that takes a SQL command and produces output that can then be used to fill a List of class instances. The code works great. I've included a slightly simplified version without exception handling here just for reference - skip this code if you want to jump right the problem. If you have suggestions here, though, I'm all ears. public List<T> ReturnList<T>() where T : new() { List<T> fdList = new List<T>(); myCommand.CommandText = QueryString; SqlDataReader nwReader =

Accessing a Collection Through Reflection

陌路散爱 提交于 2019-12-29 03:34:09
问题 Is there a way to iterate (through foreach preferably) over a collection using reflection? I'm iterating over the properties in an object using reflection, and when the program gets to a type that is a collection, I'd like it to iterate over the contents of the collection and be able to access the objects in the collection. At the moment I have an attribute set on all of my properties, with an IsCollection flag set to true on the properties that are collections. My code checks for this flag