reflection

Instantiating a constructor with parameters in an internal class with reflection

戏子无情 提交于 2019-12-27 23:36:51
问题 I have something along the lines of this: object[] parameter = new object[1]; parameter[0] = x; object instantiatedType = Activator.CreateInstance(typeToInstantiate, parameter); and internal class xxx : ICompare<Type> { private object[] x; # region Constructors internal xxx(object[] x) { this.x = x; } internal xxx() { } ... } And I get: threw exception: System.MissingMethodException: Constructor on type 'xxxx.xxx' not found.. Any ideas? 回答1: The issue is that Activator.CreateInstance(Type,

Using reflection to change static final File.separatorChar for unit testing?

末鹿安然 提交于 2019-12-27 17:37:29
问题 Specifically, I'm trying to create a unit test for a method which requires uses File.separatorChar to build paths on windows and unix. The code must run on both platforms, and yet I get errors with JUnit when I attempt to change this static final field. Anyone have any idea what's going on? Field field = java.io.File.class.getDeclaredField( "separatorChar" ); field.setAccessible(true); field.setChar(java.io.File.class,'/'); When I do this, I get IllegalAccessException: Can not set static

Using .Net, how can I determine if a type is a Numeric ValueType?

夙愿已清 提交于 2019-12-27 17:31:06
问题 But here's an example: Dim desiredType as Type if IsNumeric(desiredType) then ... EDIT: I only know the Type, not the Value as a string. Ok, so unfortunately I have to cycle through the TypeCode. But this is a nice way to do it: if ((desiredType.IsArray)) return 0; switch (Type.GetTypeCode(desiredType)) { case 3: case 6: case 7: case 9: case 11: case 13: case 14: case 15: return 1; } ;return 0; 回答1: A few years late here, but here's my solution (you can choose whether to include boolean).

INotifyPropertyChanged property name - hardcode vs reflection?

扶醉桌前 提交于 2019-12-27 17:01:30
问题 What is the best way to specify a property name when using INotifyPropertyChanged? Most examples hardcode the property name as an argument on the PropertyChanged Event. I was thinking about using MethodBase.GetCurrentMethod.Name.Substring(4) but am a little uneasy about the reflection overhead. 回答1: Don't forget one thing : PropertyChanged event is mainly consumed by components that will use reflection to get the value of the named property. The most obvious example is databinding. When you

INotifyPropertyChanged property name - hardcode vs reflection?

≯℡__Kan透↙ 提交于 2019-12-27 17:00:12
问题 What is the best way to specify a property name when using INotifyPropertyChanged? Most examples hardcode the property name as an argument on the PropertyChanged Event. I was thinking about using MethodBase.GetCurrentMethod.Name.Substring(4) but am a little uneasy about the reflection overhead. 回答1: Don't forget one thing : PropertyChanged event is mainly consumed by components that will use reflection to get the value of the named property. The most obvious example is databinding. When you

Can you use a string to instantiate a class?

点点圈 提交于 2019-12-27 17:00:06
问题 I'm using a builder pattern to seperate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named an ID (something like ID12345). These all inherit from the base builder class. In my script, I need to instantiate an instance for each class (about 50) every time this app runs. So, I'm trying to see if instead of doing something like this: ProcessDirector = ProcessDirector() ID12345 = ID12345() ID01234 = ID01234() ProcessDirector.construct(ID12345)

How to get the generic type at runtime?

£可爱£侵袭症+ 提交于 2019-12-27 11:45:27
问题 This is my code: The ExecutorImp extends AbstractExecutor which extract the same execute logics of its implementers(ExecutorImp is one case),when calling the execute() method of ExecutorImp, it will call the method in its supertype,but the supertype (the AbstractExcutor) should know another class binding to the implementer(in the example, it is the User class): import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; abstract class AbstractExecutor<E> { public void

GenericTypeResolver.resolveTypeArguments returns null

非 Y 不嫁゛ 提交于 2019-12-26 14:46:11
问题 I have the following code: List strings = new ArrayList<String>(); strings.add("string"); System.out.println(GenericTypeResolver.resolveTypeArguments(String.class, strings.getClass())); It prints null . I cannot understand this method signature. I want to check that strings is parameterized with String . How do you use resolveTypeArguments properly ? P.S. My example just dummy. P.S.2 I know that spring can do something like this with Convertor interface 回答1: This utility is made to look up

How might I count the number of int members defined for a Java class?

为君一笑 提交于 2019-12-25 19:34:13
问题 I have a class that stores a large number of int member variables, each defined as follows: public final static int int1 = int1value; public final static int int2 = int2value; ... public final static int int106 = int106value; There is a second class that needs to do a loop based on the number of int s in the first class. How would I go about adding a function to the first class to count these member variables? 回答1: It's just 106 "public final static int int1 = int1value" in the first class.

How might I count the number of int members defined for a Java class?

别等时光非礼了梦想. 提交于 2019-12-25 19:34:06
问题 I have a class that stores a large number of int member variables, each defined as follows: public final static int int1 = int1value; public final static int int2 = int2value; ... public final static int int106 = int106value; There is a second class that needs to do a loop based on the number of int s in the first class. How would I go about adding a function to the first class to count these member variables? 回答1: It's just 106 "public final static int int1 = int1value" in the first class.