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
;
}
}
查看全文
相关阅读:
Linux ps 查看进程
Linux free命令
Linux sar命令
php 上传文件
sql 计算周围公里语句
mysql sum 和 count 函数 合并使用
php函数 ceil floor round和 intval
linux sort 命令
Sicily 2711. 模板与STL 解题报告
堆排序
原文地址:https://www.cnblogs.com/engine1984/p/863581.html
最新文章
(转)linux 命令访问url: curl http://www.baidu.com/index.html
(转)CentOS7下解决ifconfig command not found的办法
(转)Hyper-v 安装CentOS 7 (其他虚拟机一样参考)
(转)使用windows server2008 创建 Hyper-V虚拟机
(转)dial tcp 10.96.0.1:443: getsockopt: no route to host --- kubernetes(k8s)DNS 服务反复重启
(转)Linux将命令添加到PATH中
(转)k8s集群部署二:flannel网络
(转)基于TLS证书手动部署kubernetes集群(下)
(转)基于TLS证书手动部署kubernetes集群(上)
MongoDB(2):入门
热门文章
MongoDB(1):常用操作命令大全
2015-2016前端知识体系(转)
javascript oo实现(转)
input实时监听(input oninput propertychange onpropertychange)
iframe
getComputedStyle(and currentStyle)
js如何将纯数字字符串转换为long型
Eclipse快捷键大全(转载)
linux 抓包工具tcpdump和tshark
linux netstat 查看网络连接状况
Copyright © 2011-2022 走看看