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 ...
随机推荐
- 【Pow(x,n)】
题目: Implement pow(x, n). 代码: class Solution { public: double myPow(double x, int n) { double ret = S ...
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
思路: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- JMeter非GUI界面运行
JMeter是一款可以用于做接口可以用于作压力性能的应用程序,该程序是纯Java语音开发,所有对环境支持都比较好. JMeter可以运行模式有两种,一种是UI图形,另一种是命令模式运行也就是非GUI模 ...
- Wordpress 数据库查询错误 Call to a member function get_results() on null
在插件中的一个文件使用如下代码,无法查询 <body> <?php global $wpdb; $sql = ""; $sql = "SELECT * ...
- java 二叉树递归遍历算法
//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...
- File IO(NIO.2):读、写并创建文件
简介 本页讨论读,写,创建和打开文件的细节.有各种各样的文件I / O方法可供选择.为了帮助理解API,下图以复杂性排列文件I / O方法 在图的最左侧是实用程序方法readAllBytes,read ...
- BootStrap导入及其使用
BootStrap主要是一个CSS框架,用于页面布局 <!DOCTYPE html> <html lang="en"> <head> <m ...
- post方式的数据抓取
public static String sendPost(String url, String param) { PrintWriter out = null; Buff ...
- linux系统——etc下的group 文件
etc/group 文件 用户组的所有信息都存放在/etc/group文件中 将用户分组是Linux系统中对用户进行管理及控制访问权限的一种手段.每个用户都属于某个用户组:一个组中可以有多个用户,一个 ...
- Codeforces Round #330 (Div. 2) B 容斥原理
B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...