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
;
}
}
查看全文
相关阅读:
WAS日常维护中的重启时机——总结
利用Shell生成Zabbix监控的数字报表
Zabbix version upgrade (1.8.3 to 1.8.12)
xeyes命令
centos系统调节屏幕亮度
centos7 安装kchmviewer 软件
ftp使用FileZilla工具传输文件
搭建vsftpd服务并实现本地用户访问
centos中创建服务和关闭防火墙的基本命令
阿里云vsftpd登录失败:530 Permission Denied.
原文地址:https://www.cnblogs.com/engine1984/p/863581.html
最新文章
给IDEA设置单独的JDK
asfdasdf test
使用flume-ng聚合双活Nginx日志
xenserver添加静态路由
redmine常见问题
Opennms 问题整理
OpenERP 使用与开发笔记(一)
KEEPALIVED 双机自动切换部署备忘
将博客搬至51CTO
检测服务器环境是否支持SHA-2加密的证书
热门文章
Configure mutiple IBM HTTP Server / Other Apache based WEB server on 1 physical server (Section 3)
Configure mutiple IBM HTTP Server / Other Apache based WEB server on 1 physical server (Section 2)
Configure mutiple IBM HTTP Server / Other Apache based WEB server on 1 physical server (Section 1)
Housekeep IHS logs without restarting process
保证服务器高可用性-灾难恢复(2)APP服务器的配置流程
保证服务器高可用性-灾难恢复(1)WEB服务器的配置流程
设置IIS服务器监听特定IP的特定端口(IIS与IHS或Apache共存的解决方案)
Linux系统添加虚拟IP(Suse linux为例)
如何确定Hadoop守护进程分别会在哪台机器上运行
Hadoop集群配置心得(低配置集群+自动同步配置)
Copyright © 2011-2022 走看看