zoukankan
html css js c++ java
反射
public
class
Love
{
public
int
field1;
private
string
_name;
public
Love()
{
}
public
string
Name
{
get
{
return
_name;
}
set
{
_name
=
value;
}
}
public
int
GetInt(
int
a)
{
return
a;
}
public
void
Display(
string
str)
{
System.Windows.Forms.MessageBox.Show(str);
}
}
测试代码:
private
void
test()
{
Love love
=
new
Love();
Type type
=
love.GetType();
type.InvokeMember(
"
Display
"
, BindingFlags.InvokeMethod
|
BindingFlags.Public
|
BindingFlags.Instance,
null
, love,
new
object
[]
{
"
aldfjdlf
"
}
);
//
调用有返回值的方法
int
i
=
(
int
)type.InvokeMember(
"
GetInt
"
, BindingFlags.InvokeMethod
|
BindingFlags.Public
|
BindingFlags.Instance,
null
, love,
new
object
[]
{
1
}
);
MessageBox.Show(i.ToString());
//
设置属性值
type.InvokeMember(
"
Name
"
, BindingFlags.SetProperty,
null
, love,
new
string
[]
{
"
abc
"
}
);
//
获取属性值
string
str
=
(
string
)type.InvokeMember(
"
Name
"
, BindingFlags.GetProperty,
null
, love,
null
);
MessageBox.Show(str);
//
设置字段值
type.InvokeMember(
"
field1
"
, BindingFlags.SetField,
null
, love,
new
object
[]
{
444
}
);
//
获取公有字段值
int
f
=
(
int
)type.InvokeMember(
"
field1
"
, BindingFlags.GetField,
null
, love,
null
);
MessageBox.Show(f.ToString());
//
获取私有字段值
string
name
=
(
string
)type.InvokeMember(
"
_name
"
, BindingFlags.GetField
|
BindingFlags.NonPublic
|
BindingFlags.Instance,
null
, love,
null
);
MessageBox.Show(name);
}
查看全文
相关阅读:
食物
连在一起的幻想乡
【XSY3209】RGB Sequence
【Luogu4389】付公主的背包
【BZOJ4555】【TJOI2016】【HEOI2016】求和
【BZOJ3456】城市规划
【BZOJ2693】jzptab & 【BZOJ2154】Crash的数字表格
【Learning】左偏树
小Z的袜子
【BZOJ3625】【CF438E】小朋友和二叉树
原文地址:https://www.cnblogs.com/gxh973121/p/454784.html
最新文章
enumerate()函数的用法
如何用python进行多项式拟合
如何获取python可执行程序的文件安装路径
关于个人技术博客的说明
Perl获取匹配行之后的后几行2
perl获取匹配行的后几行
perl返回哈希和的键和值
perl中my变量和local变量之间的区别
perl中的私有变量
香港、澳门、珠江
热门文章
兰州、西宁、青海湖、贵德
越南芽庄
西安、华山
理想早餐与现实的差别
桂林
福建武夷山
厦门
济南大明湖、趵突泉
吉首德夯
NOIP2018爆炸记
Copyright © 2011-2022 走看看