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());
查看全文
相关阅读:
bzoj1336: [Balkan2002]Alien最小圆覆盖
bzoj3564: [SHOI2014]信号增幅仪
[HDU5353]
[codeforce1072D]
[dp001]逛公园
树上问题泛做
[BZOJ2599]race
[CEOI2019]MAGIC TREE
[BZOJ2836]魔法树
QTREE3
原文地址:https://www.cnblogs.com/juqiang/p/45430.html
最新文章
最近的日子,很惬意!
我!
vecto容器中一些没有注意到的地方
最近关于Qt学习的一点碎碎念
Qt的QSettings类和.ini文件读写
Qt带参数的信号和槽
C++中vecotr表示二维数组并自己实现一个Grid类
64. Minimum Path Sum
63. Unique Paths II
VS2017报错:未提供初始值设定项
热门文章
62. Unique Paths
动态规划
CF #368 div2
bzoj4318: OSU!&&CF235BLet's Play Osu!
bzoj2337: [HNOI2011]XOR和路径
bzoj1417: Pku3156 Interconnect
bzoj2219: 数论之神
bzoj2052: Pku1777 Vivian
老oj1965:polygon半平面交
bzoj2732: [HNOI2012]射箭
Copyright © 2011-2022 走看看