通过Class对象获取对象的方式是通过class.newInstance()方式获取,通过调用默认构造参数实例化一个对象. /** * Created by hunt on 2017/6/27. * 测试的实体类 * @Data 编译后会自动生成set.get.无惨构造.equals.canEqual.hashCode.toString方法 */ @Data public class Person { private String name; private int age; } /** * C
import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class BeanChangeUtil<T> { public static <T> void main(String[] args) { User u1 = new User("1", true, "a"); User u
Service1.java package reflection; public class Service1 { public void doService1(){ System.out.println("业务方法1"); } } Service2.java package reflection; public class Service2 { public void doService2(){ System.out.println("业务方法1"); } } s
获取所有字段的值: public void PrintProperties(Object obj) { Type type = obj.GetType(); foreach( PropertyInfo p in type.GetProperties()) { Console.Write(p.GetValue()); } }
Field[] field = behavior.getClass().getDeclaredFields(); for (int i = 0; i < field.length; i++) { String name = field[i].getName(); name = name.substring(0, 1).toUpperCase() + name.substring(1);//找到首位改成大写形式 String type = field[i].getGenericType().toS
public static string GetObjectPropertyValue<T>(T t, string propertyname){ Type type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return string.Empty; object o = property.GetValue(t, null); if (o == n