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());
查看全文
相关阅读:
jq 京东跳楼效果
*Sum of NestedInteger
**Minimum Window Substring & String类问题模板
**Word Ladder
*Longest Increasing Subsequence
*Kth Largest Element in an Array
*Permutations II
*Implement Stack using Queues
*Paint House II
*Paint Fence
原文地址:https://www.cnblogs.com/juqiang/p/45430.html
最新文章
线上部署一整套流程
[洛谷P2154] SDOI2009 虔诚的墓主人
[BZOJ3714] Kuglarz
[BZOJ4548] 小奇的糖果
[ARC098F] Donation
[CF815C] Karen and Supermarket
[CF1051F] Shortest Statement
[CF911F] Tree Destruction
[CF852D] Exploration plan
[CF434D Div1] Tree
热门文章
[CF859E] Desk Disorder
DOM parentNode
DOM children childNodes nodetype
无缝滚动
延时提示框 setTimeout
超炫酷数码时钟
三级联动菜单
top置顶
jq ajax
js 小实例 随机出现小飞机
Copyright © 2011-2022 走看看