zoukankan
html css js c++ java
利用反射动态调用类成员[原创]
1
.首先建立一个类
class
PropertyClass
{
public
PropertyClass()
{
}
public
PropertyClass(
ref
Int32 x)
{
x
*=
5
;
}
public
int
fieldValue
=
0
;
private
string
name;
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
public
int
GetInt(
int
value)
{
return
value;
}
public
void
Display(
string
str)
{
Console.WriteLine(str);
}
public
override
String ToString()
{
return
fieldValue.ToString();
}
}
2
.用反射的方式调用这个PropertyClass类中的方法
public
static
void
instanceLove()
{
PropertyClass propertyClass
=
new
PropertyClass();
Type type
=
propertyClass.GetType();
//
Type type = typeof(propertyClass);效果和上面语句一样,只是写法不同。
object
obj
=
type.InvokeMember(
null
, BindingFlags.DeclaredOnly
|
BindingFlags.Public
|
BindingFlags.NonPublic
|
BindingFlags.Instance
|
BindingFlags.CreateInstance,
null
,
null
,
new
object
[]
{ }
);
//
调用没有返回值的方法
//
运行之后的结果[Hello World]
type.InvokeMember(
"
Display
"
, BindingFlags.InvokeMethod
|
BindingFlags.Public
|
BindingFlags.Instance,
null
, obj,
new
object
[]
{
"
Hello World
"
}
);
//
调用有返回值的方法
//
运行之后的结果[1982]
int
i
=
(
int
)type.InvokeMember(
"
GetInt
"
, BindingFlags.InvokeMethod
|
BindingFlags.Public
|
BindingFlags.Instance,
null
, obj,
new
object
[]
{
1982
}
);
//
设置属性值
//
此时的Name 's Value为[My Name is Brian Lei]
type.InvokeMember(
"
Name
"
, BindingFlags.SetProperty,
null
, obj,
new
object
[]
{
"
My Name is Brian Lei
"
}
);
//
获取属性值
//
运行之后的结果[My Name is Brian Lei]
string
str
=
(
string
)type.InvokeMember(
"
Name
"
, BindingFlags.GetProperty,
null
, obj,
null
);
//
设置字段值
type.InvokeMember(
"
fieldValue
"
, BindingFlags.SetField,
null
, obj,
new
object
[]
{
813
}
);
//
获取字段值
//
运行之后的结果[813]
int
f
=
(
int
)type.InvokeMember(
"
fieldValue
"
, BindingFlags.GetField,
null
, obj,
null
);
//
调用override方法
//
运行之后的结果[813]
String s
=
(String)type.InvokeMember(
"
ToString
"
, BindingFlags.DeclaredOnly
|
BindingFlags.Public
|
BindingFlags.NonPublic
|
BindingFlags.Instance
|
BindingFlags.InvokeMethod,
null
, obj,
null
);
//
调用构造方法
//
运行之后的结果[8*5=40]
Object objs
=
type.InvokeMember(
null
,BindingFlags.DeclaredOnly
|
BindingFlags.Public
|
BindingFlags.NonPublic
|
BindingFlags.Instance
|
BindingFlags.CreateInstance,
null
,
null
,
new
Object[]
{
8
}
);
}
3
.写个Main函数测试一下
class
EntryPoint
{
static
void
Main()
{
Test.instanceLove();
}
}
查看全文
相关阅读:
YTU 1002: Home Work
C++拷贝构造函数(深拷贝,浅拷贝)
深入探讨this指针
C++中关于strtok()函数的用法
STL笔记之【map之移除元素】
进程结束后,进程的所有内存都将被释放,包括堆上的内存泄露的内存。
数组指针和指针数组的区别
sizeof()用法汇总
文件描述符和文件指针的区别
字符集、字符编码、XML中的中文编码
原文地址:https://www.cnblogs.com/RuiLei/p/674833.html
最新文章
认识<hr>标签,添加水平横线
使用C++的string实现高精度加法运算
1025. PAT Ranking (25)
判断无向图是否有环路的方法 -并查集 -BFS
1021. Deepest Root (25) -并查集判树 -BFS求深度
百度地图API的学习
1022. Digital Library (30) -map -字符串处理
1020. Tree Traversals (25) -BFS
利用Dijkstra算法实现记录每个结点的所有最短路径
CSS House
热门文章
1017. Queueing at Bank (25)
YTU 1099: Minesweeper
YTU 1020: I think it
YTU 1009: University
YTU 1008: 童年生活二三事
YTU 1007: Redraiment猜想
YTU 1006: Hero In Maze
YTU 1005: 渊子赛马
YTU 1004: 1、2、3、4、5...
YTU 1003: Redraiment的遭遇
Copyright © 2011-2022 走看看