How to iterate the List in Reflection

You just need to cast it: var collection = (List<Student>) studentPro.GetValue(studentObj,null); The value returned to you and stored in var is of type object. So you need to cast it to List<Student> first, before trying looping through it. RANT That is why I personally do not like var, it hides the type – unless in … Read more

Two Types not equal that should be

The same class / type loaded by different app domains [.NET] or class loaders [Java] will not compare equal and are not assignable to/from each other directly. You likely have two copies of the DLL containing that type – one loaded by the main program and one loaded by one of the Assembly.Load*(…) methods? Try … Read more

Get the class instance variables and print their values using reflection

You could do something like this: public void printFields(Object obj) throws Exception { Class<?> objClass = obj.getClass(); Field[] fields = objClass.getFields(); for(Field field : fields) { String name = field.getName(); Object value = field.get(obj); System.out.println(name + “: ” + value.toString()); } } This would only print the public fields, to print private fields use class.getDeclaredFields … Read more

C# Reflection and Getting Properties

Still not 100% sure of what you want, but this quick bit of code (untested) might get you on the right track (or at least help clarify what you want). void ReportValue(String propName, Object propValue); void ReadList<T>(List<T> list) { var props = typeof(T).GetProperties(); foreach(T item in list) { foreach(var prop in props) { ReportValue(prop.Name, prop.GetValue(item)); … Read more

How can I get the primitive name of a type in C#?

EDIT: I was half wrong in the answer below. Have a look at CSharpCodeProvider.GetTypeOutput. Sample code: using Microsoft.CSharp; using System; using System.CodeDom; class Test { static void Main() { var compiler = new CSharpCodeProvider(); // Just to prove a point… var type = new CodeTypeReference(typeof(Int32)); Console.WriteLine(compiler.GetTypeOutput(type)); // Prints int } } However, this doesn’t translate … Read more

Reflection Performance – Create Delegate (Properties C#)

This should work for you: static Action<object, object> BuildSetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), “o”); var value = Expression.Parameter(typeof(object)); Expression<Action<object, object>> expr = Expression.Lambda<Action<object, object>>( Expression.Call( Expression.Convert(obj, method.DeclaringType), method, Expression.Convert(value, method.GetParameters()[0].ParameterType)), obj, value); return expr.Compile(); } Usage: var accessor = BuildSetAccessor(typeof(TestClass).GetProperty(“MyProperty”).GetSetMethod()); var instance = new TestClass(); accessor(instance, “foo”); Console.WriteLine(instance.MyProperty); With TestClass: public class TestClass … Read more

Wrong number of arguments error when invoking a method

That will be all right. Object[] parameters = {new Object()}; // lets say this object array is null Class clas = Class.forName(“AClass”); Object anObject = clas.newInstance(); Object[] param = {parameters}; Method someMethod = clas.getDeclaredMethod(“someMethod”, parameters.getClass()); someMethod.invoke(anObject, param); Be careful about the second parameter of the invoke method. It’s Object[] itself, and the argument type of … Read more

How to use reflection to call a private method from outside a java class without getting IllegalAccessException?

use setAccessible(true) on your Method object before using its invoke method. import java.lang.reflect.*; class Dummy{ private void foo(){ System.out.println(“hello foo()”); } } class Test{ public static void main(String[] args) throws Exception { Dummy d = new Dummy(); Method m = Dummy.class.getDeclaredMethod(“foo”); //m.invoke(d);// throws java.lang.IllegalAccessException m.setAccessible(true);// Abracadabra m.invoke(d);// now its OK } } If someone is … Read more