How to get function parameter names/values dynamically?
How to get function parameter names/values dynamically?
How to get function parameter names/values dynamically?
How do I get the path of the assembly the code is in?
In order to access private fields, you need to get them from the class’s declared fields and then make them accessible: Field f = obj.getClass().getDeclaredField(“stuffIWant”); //NoSuchFieldException f.setAccessible(true); Hashtable iWantThis = (Hashtable) f.get(obj); //IllegalAccessException EDIT: as has been commented by aperkins, both accessing the field, setting it as accessible and retrieving the value can throw Exceptions, … Read more
What you need to do is have the preprocessor generate reflection data about the fields. This data can be stored as nested classes. First, to make it easier and cleaner to write it in the preprocessor we will use typed expression. A typed expression is just an expression that puts the type in parenthesis. So … Read more
Reflection; for an instance: obj.GetType().GetProperties(); for a type: typeof(Foo).GetProperties(); for example: class Foo { public int A {get;set;} public string B {get;set;} } … Foo foo = new Foo {A = 1, B = “abc”}; foreach(var prop in foo.GetType().GetProperties()) { Console.WriteLine(“{0}={1}”, prop.Name, prop.GetValue(foo, null)); } Following feedback… To get the value of static properties, pass … Read more
Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final, and actually modify a private static final field. Here’s an example: import java.lang.reflect.*; public class EverythingIsTrue { static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField … Read more
Due to the dynamic nature of class loaders, this is not possible. Class loaders are not required to tell the VM which classes it can provide, instead they are just handed requests for classes, and have to return a class or throw an exception. However, if you write your own class loaders, or examine the … Read more
Coding from the hip, it would be something like: java.lang.reflect.Method method; try { method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..); } catch (SecurityException e) { … } catch (NoSuchMethodException e) { … } The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give … Read more
As others mentioned, it’s only possible via reflection in certain circumstances. If you really need the type, this is the usual (type-safe) workaround pattern: public class GenericClass<T> { private final Class<T> type; public GenericClass(Class<T> type) { this.type = type; } public Class<T> getMyType() { return this.type; } }
public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetValue(src, null); } Of course, you will want to add validation and whatnot, but that is the gist of it.