reflection

ClassNotFoundException while loading a class from file with URLClassLoader

社会主义新天地 提交于 2020-01-05 02:51:27
问题 I am using the following code to create an instance of OdbcIniTarget class which implements the Target interface. The OdbcIniTarget class already exists in the application but is located in a different package and now should be loaded from the target directory which is not in the class path. ... // directory below is resolved to /D:/Workspace/<myproject>/targets/ private File targetsLocation = new File("targets"); ... private void loadTargets() { URL url = null; try { url = targetsLocation

ClassNotFoundException while loading a class from file with URLClassLoader

十年热恋 提交于 2020-01-05 02:50:27
问题 I am using the following code to create an instance of OdbcIniTarget class which implements the Target interface. The OdbcIniTarget class already exists in the application but is located in a different package and now should be loaded from the target directory which is not in the class path. ... // directory below is resolved to /D:/Workspace/<myproject>/targets/ private File targetsLocation = new File("targets"); ... private void loadTargets() { URL url = null; try { url = targetsLocation

What will be the runtime signature?

隐身守侯 提交于 2020-01-04 14:21:24
问题 I have a bit of a problem understanding Java's type erasure when it comes to bounded types. Consider this: class Event {} // From the API class FooEvent extends Event {} abstract class Foo<EventType extends Event> { public abstract <E extends EventType> void onEventCaught(E event); } class Bar extends Foo<FooEvent> { @Override public void onEventCaught(FooEvent event) { } } Apparently this compiles without problems. The question that I ask myself is, for which parameter-types is Bar

How do I bind an Anonymous action as new event handler using reflection

自作多情 提交于 2020-01-04 13:38:02
问题 I am writing a number of unit tests for Windows Forms, and so far have been able to figure out how to set private controls' properties and invoke their methods, using Reflection. But I am stuck on how to associate an in-line lambda to attach itself to an event occurring on one of the controls, in this case the DataSourceChanged event of DataGridView. public static void ObserveGrid(this Form form, string controlName, Action action) { var controls = form.Controls.Find(controlName, true); if

getting an Int/short/byte structure byte representation with C#

戏子无情 提交于 2020-01-04 13:35:20
问题 Given a FieldInfo object and an object, I need to get the actual bytes representation of the field. I know that the field is either int,Int32,uint,short etc. How can I get the actual byte representation? BinaryFormatter.Serialize won't help, since it'll give me more information than I need (it also records type name etc.). The Marshal class does not seem to have facilities to use bytes array (but maybe I'm missing something). Thanks 回答1: You may also try code like the following if what you

C# VS 2005: How to get a class's public member list during the runtime?

孤者浪人 提交于 2020-01-04 13:14:05
问题 I'm trying to get a class memeber variable list at run time. I know this probably using typeof and reflections. but can't find an example. Please someone shed light for me. Here is pseudo code example: Class Test01 { public string str01; public string str02; public int myint01; } I want something like this (pseudo code): Test01 tt = new Test01(); foreach(variable v in tt.PublicVariableList) { debug.print v.name; debug.print v.type; } Please help me figure out how to do this in C# VS2005

Using Reflection to call a method on a field

て烟熏妆下的殇ゞ 提交于 2020-01-04 09:16:14
问题 Okay been working on this too long and can't get it to work. I have a list of Thread types that can be different classes i.e. WriteFileData(extends Thread). I want to for loop through that list and do a call to add to queue a byte array. I currently have this in a Broker class // consumers is filled with different Thread types all having a queue // variable of type LinkedBlockingQueue ArrayList<Thread> consumers = new ArrayList<Thread>(); synchronized void insert(final byte[] send) throws

How to queue and call actual methods (rather than immediately eval) in java?

﹥>﹥吖頭↗ 提交于 2020-01-04 07:55:40
问题 There are a list of tasks that are time sensitive (but "time" in this case is arbitrary to what another program tells me - it's more like "ticks" rather than time). However, I do NOT want said methods to evaluate immediately. I want one to execute after the other finished. I'm using a linked list for my queue, but I'm not really sure how/if I can access the actual methods in a class without evaluating them immediate. The code would look something like... LinkedList<Method> l = new LinkedList

Dynamically editing/creating classes in Java Android

喜欢而已 提交于 2020-01-04 06:19:21
问题 I am looking for a way to dynamically define classes and instantiate them in Android, at runtime. From my understanding, this is already done in Android, I just need some help figuring it out. I can a similiar result in Javascript and PHP. I know it can be done in Java using something like ASM, BCEL or CGlib. However, I do not know enough about any of these to understand if they will work on Android. Or, of they will work, what are the implications? If, hypothetically, all three will work in

Enumerate the fields of a subclass in Scala

北城以北 提交于 2020-01-04 06:03:56
问题 Simple question about reflection in Scala : How can I write a method fields that returns all the fields of a certain type, even in subclasses? Is that even the right way to do it? How would you do it if it is not? Example : class Top { def fields = ... } class A extends Top { val f1 = Field(...) val f2 = Field(...) } (new A).fields // List(f1, f2) 回答1: So, in my trials and errors, I've found genuine run-time type reflection to be difficult, because of Scala's trick of embedding type