java反射给字段赋值就是给实体类的set赋值,怎么做? 亲,以下是我写的例子,你可以参考:import java.lang.reflect.Field;import java.util.Arrays;import static java.lang.System.out;enum Tweedle { DEE,DUM }public class Book {public long chapters=0;public String[]characters={\"Alice\",\"White Rabbit\"};public Tweedle twin=Tweedle.DEE;public static void main(String.args){Book book=new Book();String fmt=\"%6S:%-12s=s%n;try {Class?c=book.getClass();Field chap=c.getDeclaredField(\"chapters\");out.format(fmt,\"before\",\"chapters\",book.chapters);chap.setLong(book,12);out.format(fmt,\"after\",\"chapters\",chap.getLong(book));Field chars=c.getDeclaredField(\"characters\");out.format(fmt,\"before\",\"characters\",Arrays.asList(book.characters));String[]newChars={\"Queen\",\"King\"};chars.set(book,newChars);out.format(fmt,\"after\",\"characters\",Arrays.asList(book.characters));Field t=c.getDeclaredField(\"twin\");out.format(fmt,\"before\",\"twin\",book.twin);t.set(book,Tweedle.DUM);out.format(fmt,\"after\",\"twin\",t.get(book));production code should handle these C#如何通过属性名称反射出属性本身? C c=new C();Type t=typeof(C);var v1=t.GetField(\"n\").GetValue(c);获取字段值,你的类C.n是字段,不是属性var v2=t.GetProperty(\"n\").GetValue(c,null);获取属性值两个参数的GetValue向下兼容.net framework 2.0if(typeof(C).GetField(\"n\").GetValue(c).Equals(1))懂JAVA反射的高手请进!!! List
#string#反射机制