zoukankan
html css js c++ java
c# 反射总结
Code
http:
//
blog.csdn.net/xiaolei1982/archive/2008/04/15/2294364.aspx
首先我们建立一个类库,将它生成为HelloWorld.dll,
using
System;
namespace
Webtest
{
public
interface
interface1
{
int
add();
}
public
class
ReflectTest : interface1
{
public
string
Write;
private
string
Writec;
public
string
Writea
{
get
{
return
this
.Write;}
set
{
this
.Write
=
value; }
}
public
string
Writeb
{
get
{
return
this
.Writec; }
set
{
this
.Writec
=
value; }
}
public
ReflectTest()
{
this
.Write
=
"
Write
"
;
this
.Writec
=
"
Writec
"
;
}
public
ReflectTest(
string
str1,
string
str2)
{
this
.Write
=
str1;
this
.Writec
=
str2;
}
public
string
WriteString(
string
s,
int
b)
{
return
"
欢迎你,
"
+
s
+
"
--
"
+
b;
}
public
static
string
WriteName(
string
s)
{
return
"
欢迎你:
"
+
s;
}
public
string
WriteNoParam()
{
return
"
你使用的是无参数的方法!
"
;
}
private
string
WritePrivate()
{
return
"
私有类型的方法!
"
;
}
public
int
add()
{
return
5
;
}
}
}
然后,建立再建立一个项目引入该HelloWorld.dll,
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Reflection;
namespace
Refltest
{
class
Program
{
delegate
string
TestDelegate(
string
value,
int
value1);
static
void
printf()
{
Console.WriteLine(
"
******************************************************
"
);
}
static
void
Main()
{
//
Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果
Assembly t
=
Assembly.Load(
"
HelloWorld
"
);
//
调用程序集
//
**********************************************************************
printf();
foreach
(Type aaa
in
t.GetTypes())
{
Console.WriteLine(
"
dll下的类:
"
+
aaa.Name);
//
显示该dll下所有的类
}
//
**********************************************************************
printf();
Module[] modules
=
t.GetModules();
//
??????
foreach
(Module module
in
modules)
{
Console.WriteLine(
"
module name:
"
+
module.Name);
//
显示模块的名字本例为"HelloWorld.dll"
}
//
**********************************************************************
printf();
Type a
=
typeof
(Webtest.ReflectTest);
//
得到具体的类的类型,和下面一个效果
Type a_a
=
t.GetType(
"
Webtest.ReflectTest
"
);
//
Console.WriteLine(a.Name);
//
输出类名
Console.WriteLine(a_a.Name);
//
**********************************************************************
printf();
string
[] bb
=
{
"
aaaa
"
,
"
bbbbb
"
}
;
object
obj
=
Activator.CreateInstance(a,bb);
//
创建该类的实例,后面的bb为有参构造函数的参数,a为类型,有默认参数
object
_obj
=
t.CreateInstance(
"
Webtest.ReflectTest
"
);
//
与上面方法相同,无默认参数
Console.WriteLine(((Webtest.ReflectTest)obj).Writea);
Console.WriteLine(((Webtest.ReflectTest)_obj).Writea);
//
**********************************************************************
printf();
MethodInfo[] miArr
=
a.GetMethods();
//
显示公共方法
foreach
(MethodInfo mi0
in
miArr)
{
Console.WriteLine(
"
method:
"
+
mi0.Name);
//
显示所有的共有方法
}
//
**********************************************************************
printf();
MethodInfo mi
=
a.GetMethod(
"
WriteString
"
);
//
显示具体的方法,获取具体的方法
object
[] aa
=
{
"
使用的是带有参数的非静态方法
"
,
2
}
;
string
s
=
(
string
)mi.Invoke(obj,aa);
//
带参数方法的调用,obj是类的实例,aa是参数
Console.WriteLine(
"
s=
"
+
s);
MethodInfo mi1
=
a.GetMethod(
"
WriteName
"
);
String[] aa1
=
{
"
使用的是静态方法
"
}
;
string
s1
=
(
string
)mi1.Invoke(
null
, aa1);
//
静态方法的调用,不用实例
Console.WriteLine(
"
s1=
"
+
s1);
MethodInfo mi2
=
a.GetMethod(
"
WriteNoParam
"
);
string
s2
=
(
string
)mi2.Invoke(obj,
null
);
//
不带参数的方法调用
Console.WriteLine(
"
s2=
"
+
s2);
MethodInfo mi3
=
a.GetMethod(
"
WritePrivate
"
,BindingFlags.Instance
|
BindingFlags.NonPublic);
string
s3
=
(
string
)mi3.Invoke(obj,
null
);
//
私有类型方法调用
Console.WriteLine(
"
s3=
"
+
s3);
//
**********************************************************************
printf();
PropertyInfo[] piArr
=
a.GetProperties(BindingFlags.Instance
|
BindingFlags.NonPublic
|
BindingFlags.Public);
foreach
(PropertyInfo pi
in
piArr)
{
Console.WriteLine(
"
属性:
"
+
pi.Name);
//
显示所有的属性
}
//
**********************************************************************
printf();
PropertyInfo pi1
=
a.GetProperty(
"
Writea
"
);
//
pi1.SetValue(obj, "Writea", null);
Console.WriteLine(pi1.GetValue(obj,
null
));
PropertyInfo pi2
=
a.GetProperty(
"
Writeb
"
, BindingFlags.Instance
|
BindingFlags.NonPublic
|
BindingFlags.Public);
pi2.SetValue(obj,
"
Writeb
"
,
null
);
//
Console.Write(pi2.GetValue(obj, null));
FieldInfo fi1
=
a.GetField(
"
Write
"
);
Console.WriteLine(fi1.GetValue(obj));
//
**********************************************************************
printf();
ConstructorInfo[] ci1
=
a.GetConstructors();
foreach
(ConstructorInfo ci
in
ci1)
{
Console.WriteLine(
"
构造函数:
"
+
ci.ToString());
//
获得构造函数的形式
}
ConstructorInfo asCI
=
a.GetConstructor(
new
Type[]
{
typeof
(
string
),
typeof
(
string
) }
);
Console.WriteLine(
"
特定的构造函数:
"
+
asCI.ToString());
//
**********************************************************************
printf();
Webtest.interface1 obj1
=
(Webtest.interface1)t.CreateInstance(
"
Webtest.ReflectTest
"
);
Webtest.ReflectTest obj2
=
(Webtest.ReflectTest)t.CreateInstance(
"
Webtest.ReflectTest
"
);
Console.WriteLine(
"
Factory_interface:
"
+
obj1.add());
//
典型的工厂模式
Console.WriteLine(
"
Factory_Class:
"
+
obj2.add());
//
**********************************************************************
printf();
foreach
(Type tt
in
t.GetTypes())
{
if
(tt.GetInterface(
"
interface1
"
)
!=
null
)
//
判断是否继承接口interface1
{
Webtest.interface1 obj3
=
(Webtest.interface1)Activator.CreateInstance(a);
//
a 类的类型,实例对象
Console.WriteLine(obj3.add());
//
???
}
}
//
**********************************************************************
printf();
TestDelegate method
=
(TestDelegate)Delegate.CreateDelegate(
typeof
(TestDelegate), obj,
"
WriteString
"
);
//
动态创建委托的简单例子
Console.WriteLine(
"
委托:
"
+
method(
"
str1
"
,
2
));
//
**********************************************************************
printf();
ConstructorInfo asCI1
=
a.GetConstructor(
new
Type[
0
]);
Webtest.ReflectTest obj5
=
(Webtest.ReflectTest)asCI1.Invoke(
null
);
//
通过无参构造函数实例化的方法
Console.Write(obj5.Writea);
ConstructorInfo asCI2
=
a.GetConstructor(
new
Type[]
{
typeof
(
string
),
typeof
(
string
) }
);
//
通过有参构造函数实例化的方法
Webtest.ReflectTest obj6
=
(Webtest.ReflectTest)asCI2.Invoke(bb);
Console.Write(obj6.Writea);
//
**********************************************************************
Console.Read();
}
}
}
查看全文
相关阅读:
老罗android开发视频教程学习完了
微软云工具
Andriod视频http://pan.baidu.com/share/link?shareid=7300&uk=3339495714
微软图片编辑工具
Smartdraw世界上最流行的商业绘图软件
arcgis安装问题SDK开始安装不了
光学字符识别OCR
Android实现OCR扫描识别数字图片之图片扫描识别
android实现图片识别的几种方法
开源的asp.net工作流程引擎。 http://ccflow.org
原文地址:https://www.cnblogs.com/binlyzhuo/p/1500191.html
最新文章
[剑指offer] 54. 字符流中第一个不重复的字符
[剑指offer] 53. 表达数值的字符串
[剑指offer] 52. 正则表达式匹配
[剑指offer] 51. 构建乘积数组
iOS进阶之路——理解 Xcode 编译系统
三十岁辞职以后——怎么找回自己人生的方向
市场缺的不是iOS开发,缺的是iOS开发大牛
(三)ansible playbook
(二)ansible 使用
(一)ansible 安装配置
热门文章
轻应用
.net开源工作流引擎ccflow
解决SQL Server管理器无法连接远程数据库Error: 1326错误
SQL 2005远程连接是出错(provider: SQL 网络接口, error: 28
java工作流bpm开发ERP实例
workflow4.0学习资料
workflow4.0持久化
微软Azure Services Bus中的工作流
微软视频学习
android导入项目出错处理
Copyright © 2011-2022 走看看