reflection

Java: Cast String to primitive type dynamically

时间秒杀一切 提交于 2019-12-24 12:01:41
问题 I want to invoke a method by reflection in java. I have on my hand the Method instance of the method I want to invoke (so I can get the types of its parameters), in addition, I have the values of these parameters as Strings. I have an assumption that all the parameters MUST be primitives. for example, if I want to invoke the following method: public static double calc(int a, double b, String op){...} I have the parameter as a String Array: String[]: {"25", "34.45", "add"} So, how can I

Determining a Generic Type at Runtime in Non-Generic Class

你离开我真会死。 提交于 2019-12-24 11:49:11
问题 I've gotten myself in a pickle, and could use the help of a guru... I have a Journal that records entries for different types: Journal(Of ParentT) - Parent could be Customer , Address , other classes The constructor of the Journal requires knowledge of the Type parameter: Public Sub New(Parent as ParentT) In my consuming form, I take a Journal in the constructor: Public Sub DisplayForm(Journal as object) At this point, I cannot determine what type the Journal is for. I have looked at using

Does MonoDroid support java.lang.reflect package methods?

只愿长相守 提交于 2019-12-24 11:28:45
问题 I need to use methods from java.lang.reflect in a MonoDroid solution, but I can't find this namespace anywhere. Is there a way to access these? 回答1: The java.lang.reflect package is not bound in Mono for Android. This bug has some more details about that. If you really need to work around that limitation, you could write that part of your application in Java and then call it through JNI. Xamarin has a good guide on using JNI up here. 来源: https://stackoverflow.com/questions/11372981/does

Check using reflection if property is IEnumerable only of reference types but not string or value types

梦想与她 提交于 2019-12-24 11:00:27
问题 Hello I need to check using reflection whether property is IEnumerable type but not IEnumerable of string and value types, but only IEnumerable of non-string reference types. Right now I have that part of code: private bool IsEnumerable(PropertyInfo propertyInfo) { return propertyInfo.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)) && propertyInfo.PropertyType != typeof(string); } If property is IEnumerable<MyCustomType> this is ok, but if it is IEnumerable<string> my method should

Accessing a C++ non-member function from C# via reflection

本小妞迷上赌 提交于 2019-12-24 10:59:10
问题 I need to gain some run-time information about a C++ program, which is kinda difficult due to C++ not offering some sophisticated reflection mechanism. Now, my approach is to compile the C++ code using /clr and to reflect over the resulting assembly from C# (simply because I like that language more than C++). While this is all turning out more or less fine, I'm now stuck at a point where I need to actually run the program by calling its main method. Which is kind of frustrating considering

Mapping C structure to an XML element

旧城冷巷雨未停 提交于 2019-12-24 10:58:59
问题 Suppose I have a structure in C or C++, such as: struct ConfigurableElement { int ID; char* strName; long prop1; long prop2; ... }; I would like to load/save it to/from the following XML element: <ConfigurableElement ID="1" strName="namedElem" prop1="2" prop2="3" ... /> Such a mapping can be trivially done in Java/C# or any other language with run-time reflection for the matter. Can it be done in any non-tedious way in C++ with macros/template trickery? Bonus points for handling nested

Get a static property from class in actionscript

旧巷老猫 提交于 2019-12-24 10:49:15
问题 I have this class package somePackage { public class SomeClass { public static const FOO: SomeClass = new SomeClass("0"); public static const BAR: SomeClass = new SomeClass("1"); } } I want to be able to get those static property given it's name. Example: public static function getProperty(propertyName: String): SomeClass { //don't know what goes here } var property1:SomeClass = SomeClass.getProperty("FOO"); // property1 == SomeClass.FOO var property2:SomeClass = SomeClass.getProperty("BAR");

Can a field's transient property/flag be set through reflection in java?

元气小坏坏 提交于 2019-12-24 10:48:16
问题 Is there a simple way to specify if a field should be transient or not in Java with reflection, similar to how a field's accessibility flag can be set with setAccessible()? 回答1: Reflection itself cannot alter code. Java Agents should allow you to rewrite the class as it is loaded. You can use reflection to alter serialPersistentFields if it exists (unlikely), before the serialisation mechanism caches the class data. You could use reflection in a highly version specific way to alter the data

Show all elements in a protocol buffer message

荒凉一梦 提交于 2019-12-24 10:26:51
问题 How can I show all elements in a protocol buffer message? Do I need to use reflection or convert the message into an XML message and then show it? Ideally some generic code that will work for any message. Lars 回答1: A protobuf message is internally ambiguous unless you have the .proto schema (or can infer a schema) available, as (for example) a "string" wire-type could represent: a utf-8 string a BLOB a sub-message a packed array Similar ambiguity exists for all wire-types (except perhaps

Can you invoke a method via reflection on behalf of another class

此生再无相见时 提交于 2019-12-24 10:06:34
问题 I have a client.class that uses Util.class to invoke a method on target.class. The invocation is forced by calling the setAccessible(true) to the method of target.class. Question: Is there a way to have this Util.class determine if the client.class has access to this particular method of target.class? I want to skip having to use the method setAccessible(true). client.class has access to the method I am invoking in target.class, but Util.class does not have access because the method is