本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射。

 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的更多相关文章

  1. 关于Assembly.CreateInstance()与Activator.CreateInstance()方法

    于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...

  2. C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  3. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  4. (转) C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  5. C#中Activator.CreateInstance()方法用法分析

    本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...

  6. Activator.CreateInstance 方法 (Type) 的用法

    转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...

  7. cSharp:use Activator.CreateInstance with an Interface?

    ///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...

  8. 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 中,执行该操作的另一种方法 ...

  9. 注意Activator.CreateInstance两个重载方法的性能

    今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...

随机推荐

  1. Python数据结构之列表、元组及字典

    一位大牛Niklaus Wirth曾有一本书,名为<Algorithms+Data Structures=Programs>,翻译过来也就是算法+数据结构=程序.而本文就是介绍一下Pyth ...

  2. Leetcode 493.翻转对

    翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数量. 示例 ...

  3. Unity 碰撞检测

    武器与怪物的碰撞 目前来说有三种思路,其实前两种算变种了: 1.动画关键帧回调 + 范围检测.http://blog.csdn.net/u013700908/article/details/52888 ...

  4. location.replace()和location.href=进行跳转的区别

    location.href 通常被用来跳转到指定页面地址;location.replace 方法则可以实现用新的文档替换当前文档;location.replace 方法不会在 history 对象中生 ...

  5. 【bzoj2096】[Poi2010]Pilots 双指针法+STL-set

    题目描述 Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是不行的,于是他 ...

  6. [ZJOI2015][bzoj3924] 幻想乡战略游戏 [动态点分治]

    唉:-(动态点分治的思想真是复杂...... 先码住,再做几道题再来填坑 PS:接下来的Code因为用了倍增lca所以TLE一部分,但是懒得改成RMQ了...... Code: #include< ...

  7. git filter-branch应用

    1.修改author和committer git filter-branch --commit-filter ' export GIT_AUTHOR_EMAIL=me@example.com; exp ...

  8. ZJOI2017D1

    假装我还活着. 去温州前沉迷各种奇怪的动画片..嗯补了不少高达.. 到温州以后继续看片..嗯ZG还是挺不错的..然后接着就FA♂现我什么都不会写..有点尴尬.. 因为宾馆离温州中学比较远就完全没去听课 ...

  9. 使用libcurl提示 LNK2001的错误

    vs使用libcurl(static library),link时报错: error LNK2001: unresolved external symbol __imp__curl_easy_perf ...

  10. Python 操作 Mysql 模块

    一.Python 操作 Mysql 模块的安装 linux: yum install MySQL-python window: http://files.cnblogs.com/files/wupei ...