Assembly.CreateInstance和Activator.CreateInstance
本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射。
namespace Factory
{
public abstract class Factory
{
public abstract Human CreateHuman(string humanType);
}
public class HumanFactory : Factory
{
public static readonly Assembly assembly = typeof(Human).Assembly; public override Human CreateHuman(string humanType)
{
Type[] ss = assembly.GetTypes();
string type = string.Concat("Factory.", humanType);
Human human = assembly.CreateInstance(type) as Human;
return human;
}
}
}
本来想用传进的humanType字符串来实例化一个类的,但是因为humanType只是类的名字(Name,不是FullName),所以总是创建实例不成功,得出的一直都是human=null的结果。
后来才发现在创建的过程中应该是在程序集中寻找类型的FullName来匹配的,也就是Namespace.ClassName,本例中就是Factory.humanType的。
PS:Activator.CreateInstance(type)也可以创建实例的,只不过参数是一个类型,本例中可以写为
Human human1 = Activator.CreateInstance(Type.GetType(type)) as Human;
至于两者的区别,除了Activator.CreateInstance是静态的,assembly.CreateInstance不是,其他还不清楚,有待进一步研究。
不过,Assembly类中的CreateInstance方法是调用了Activator.CreateInstance方法的,囧。
Assembly.CreateInstance和Activator.CreateInstance的更多相关文章
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
- C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- (转) C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- C#中Activator.CreateInstance()方法用法分析
本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- cSharp:use Activator.CreateInstance with an Interface?
///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
EF Core使用SQL调用返回其他类型的查询 假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...
- 注意Activator.CreateInstance两个重载方法的性能
今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...
随机推荐
- CTF 两道web整数溢出题目(猫咪银行和ltshop)
①猫咪银行: (2018中科大hackgame) 一开始给十个CTB,而flag需要20个CTB,我们需要理财赚够20个. 理财是只能买入TDSU才可以获得收益.我们先上来直接把CTB全部换成TDSU ...
- 安装的 Python 版本太多互相干扰?pyenv 建议了解一下。
写在之前 我们都知道现在的 Python 有 Python2 和 Python3,但是由于各种乱七八糟的原因导致这俩哥们要长期共存,荣辱与共,尴尬的是这哥俩的差异还比较大,在很多时候我们可能要同时用到 ...
- Detect Vertical&Horizontal Segments By OpenCV
Detect Vertical&Horizontal Segments By OpenCV,and Save the data to csv. Steps: Using adaptiveThr ...
- (转载)CentOS 6.5使用aliyun镜像来源
(原地址:http://www.linuxidc.com/Linux/2014-09/106675.htm) 当我们把CentOS 6.5安装好以后,可以使用这个脚本来使用国内的阿里云镜像源 #!/b ...
- Ansible实战之Nginx代理Tomcat主机架构
author:JevonWei 版权声明:原创作品 实验架构:一台nginx主机为后端两台tomcat主机的代理,并使用Ansible主机配置 实验环境 Nginx 172.16.252.82 Tom ...
- 【bzoj1367】[Baltic2004]sequence 可并堆
题目描述 输入 输出 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 题解 可并堆,黄源河<左偏树的特点及其应用>Page 13例题原题 #include & ...
- 【APIO 练习题】Lock Puzzle
题意 你有一个长度为 $n$ 的字符串,你需要经过若干次操作将其变成目标串 $n'$. 一次操作:选择串 $n$ 的一个后缀,将其翻转,并放到串 $n$ 的最前面. 请你输出任意一种方案.当然,你达到 ...
- swarm集群数据管理
1.volume [root@manager ~]# docker service create --mount type=volume,src=vol1,dst=/usr/local/nginx/h ...
- 适配IPhone X的技巧
#define TabbarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49) // ...
- SPOJ HIGH Highways
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...