zoukankan
html css js c++ java
两种动态创建对象的方法
1、如果要创建的对象,在一个assembly中,那么方法可以有很多。如这种方式:
System.Reflection.Assembly ass
=
System.Reflection.Assembly.LoadFrom(
@"
D:Programsvs.netInterfaceCominDebugCom.dll
"
);
Type t
=
ass.GetType(
"
Com.Class1
"
);
string
[] implCtorSig
=
{
"
System.String
"
}
;
Type[] ctorSigArr
=
Type.GetTypeArray(implCtorSig);
System.Reflection.ConstructorInfo ctorInfo
=
t.GetConstructor(ctorSigArr);
object
[] ctorArgs
=
{
"
1
"
}
;
object
obj
=
ctorInfo.Invoke(ctorArgs);
Com.IDict idict
=
(Com.IDict)obj;
2、如果对象在另一个assembly中,那么上述代码最后一行,会抛出一个invalid cast异常。而用下面的方式,是可以的:
System.Reflection.Assembly ass
=
System.Reflection.Assembly.LoadFrom(
@"
D:Programsvs.netInterfaceCominDebugCom.dll
"
);
Type t
=
ass.GetType(
"
Com.Class1
"
);
object
[] ctorArgs
=
{
"
1
"
}
;
System.Runtime.Remoting.ObjectHandle oh
=
Activator.CreateInstance(ass.FullName,t.FullName,
true
,System.Reflection.BindingFlags.CreateInstance,
null
,ctorArgs,System.Globalization.CultureInfo.CurrentCulture,
new
Object[]
{}
,
null
);
Com.IDict dict
=
(Com.IDict)(oh.Unwrap());
查看全文
相关阅读:
FastAPI项目实战: 个人博客项目的API
Jmeter分布式执行,java.rmi.UnmarshalException: xxxAbstractSimpleThreadGroup错误
[转]JMeter分布式的坑
Docker菜鸟教程-硬敲系列
VMware EXIS 安装
2020简单总结
07.1 迭代器、生成器
locust 的 ‘1’ 版本时代变化
移动端专项测试-内存泄漏
乘风破浪的不止姐姐,还有我们的测试工程师!
原文地址:https://www.cnblogs.com/juqiang/p/45430.html
最新文章
jmeter压测报错
线程(一)
基础(3)
基础(2)
基础
git 常用命令
redis常用命令
APScheduler定时框架
Pytest+allture+appium实战APP自动化测试
pytest
热门文章
appium元素定位方式
Appium简介及环境搭建
appium自动化测试元素定位工具
adb命令
APP测试概述
apiAutoTest: 接口自动化测试的数据清洗(备份/恢复)处理方案
apiAutoTest-更新2020/11/23
Python接口自动化测试工具(Pytest+Allure+jsonpath+xlrd+excel、支持Restful接口规范)
Jmeter任在运行,线程数上不去
Python (paramiko) 连接Linux服务器
Copyright © 2011-2022 走看看