zoukankan
html css js c++ java
C#反射实例讲解
创建用于反射使用的DLL
新建一个C#类库项目,拷贝源代码如下,编译生成DLL(假如DLL的文件名是TestReflect.dll)
using
System;
namespace
Webtest
{
/**/
/**/
/**/
///
<summary>
///
ReflectTest 的摘要说明。
///
</summary>
public
class
ReflectTest
{
public
ReflectTest()
{}
public
string
WriteString(
string
s)
{
return
"
欢迎您,
"
+
s;
}
/**/
/**/
/**/
///
<summary>
///
dsajkjflasjdfalksdjfaskfd
///
</summary>
///
<param name="s"></param>
///
<returns></returns>
public
static
string
WriteName(
string
s)
{
return
"
欢迎您光临,
"
+
s;
}
public
string
WriteNoPara()
{
return
"
您使用的是无参数方法
"
;
}
}
}
2 应用于反射的例子
在ASPNET页面中加入以下函数:
public
void
test1()
{
System.Reflection.Assembly ass;
Type type ;
object
obj;
try
{
ass
=
System.Reflection.Assembly.LoadFile(
@"
d:\TestReflect.dll
"
);
type
=
ass.GetType(
"
Webtest.ReflectTest
"
);
//
必须使用名称空间+类名称
System.Reflection.MethodInfo method
=
type.GetMethod(
"
WriteString
"
);
//
方法的名称
obj
=
ass.CreateInstance(
"
Webtest.ReflectTest
"
);
//
必须使用名称空间+类名称
string
s
=
(
string
)method.Invoke(obj,
new
string
[]
{
"
jianglijun
"
}
);
//
实例方法的调用
Response.Write(s
+
"
<br>
"
);
method
=
type.GetMethod(
"
WriteName
"
);
//
方法的名称
s
=
(
string
)method.Invoke(
null
,
new
string
[]
{
"
jianglijun
"
}
);
//
静态方法的调用
Response.Write(s
+
"
<br>
"
);
method
=
type.GetMethod(
"
WriteNoPara
"
);
//
无参数的实例方法
s
=
(
string
)method.Invoke(obj,
null
);
Response.Write(s
+
"
<br>
"
);
method
=
null
;
}
catch
(Exception ex)
{
Response.Write(ex
+
"
<br>
"
);
}
finally
{
ass
=
null
;
type
=
null
;
obj
=
null
;
}
}
查看全文
相关阅读:
Bufferedreader和BufferedWriter(带缓存的输入输出)
FileReader和FileWriter
Map接口的类实现
Map接口
Set集合
List接口的实现类
获取文本框或密码框中的内容
ADTS (zz)
初级美语 L003:My Family 解析
初级美语 L001:Self Introduction 解析
原文地址:https://www.cnblogs.com/engine1984/p/863581.html
最新文章
linux 下创建虚拟环境 python , linux彻底删除nginx
django自带权限机制
Django 2.0 新款URL配置详解
网络爬虫之scrapy框架详解,scrapy框架设置代理
爬虫之Xpath详解
scrapy中间件的简介
scrapy执行流程
O
G
J
热门文章
P
A
E
D
B
A
E
定义泛型类
数据输入输出流
获取程序地址
Copyright © 2011-2022 走看看