internal class Employee
{
public string No { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public string Post { get; set; }
}
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee();
empAge = 25;
empPost = "人工智能工程师";
empNo = "A001";
empName = "hky_bd2010";
SystemReflectionPropertyInfo[] pro = empGetType()GetProperties();
foreach (SystemReflectionPropertyInfo item in pro)
{
ConsoleWriteLine(itemName+"="+itemGetValue(emp));
}
ConsoleReadKey(true);
}
}
Java代码
package ;
import java lang reflect Field;
import java lang reflect InvocationTargetException;
import java lang reflect Method;
public class TestBean {
private int age;
public static void main(String []args) throws InstantiationException IllegalAccessException SecurityException NoSuchFieldException{
try {
Class<> class =Class forName( TestBean );
Object tObject=class newInstance();
Field field=class getDeclaredField( age );
field setAccessible(true); //设置私有属性范围
field set(tObject );
System out print(field get(tObject));
try {
Method method=class getMethod( setAge int class);
method invoke(tObject );
Method getMethod=class getMethod( getAge );
System out println(getMethod invoke(tObject));
} catch (NoSuchMethodException e) {
// TODO Auto generated catch block
e printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto generated catch block
e printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto generated catch block
e printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto generated catch block
e printStackTrace();
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
this age = age;
}
lishixinzhi/Article/program/Java/hx/201311/26324
以上就是关于在 .NET 下, 如何通过反射获取类的全部 Field 的名称和值全部的内容,包括:在 .NET 下, 如何通过反射获取类的全部 Field 的名称和值、Java反射设置私有属性和获取属性、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)