一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例.

这在工厂模式中是非常有用的

这样,可以使程序有更高的扩展性,例如,,下面的例子

如果现在有一个类,专门用来计算交通工具的速度,不同的交通工具计算方法是不一样的,但是到底有那些交通工具是未知的或者是可变的,这种情况下,我们可能觉得要在添加交通工具的时候,需要修改用来计算速度的那个类,

但如果用Activator .CreateInstance创建实例,通过接口技术,则只要向程序集添加一个交通工具类,而不需要修改任何其它代码..实现了高扩展性.,

示例如下:

//接口:比如,在上例中,可以是交通工具必需实现的接口

using System;

namespace ActivatorCreateInstance
{
 /// <summary>
 /// IObjcet 的摘要说明。
 /// </summary>
 public interface IObjcet
 {
  void printName();
 }
}

//实现接口的类:比如在上例中,是交通工具类,这种类可以在扩展的时候添加,其它类的代码不用修改

using System;

namespace ActivatorCreateInstance
{
 /// <summary>
 /// ClassExam 的摘要说明。
 /// </summary>
 public class ClassExam:IObjcet
 {
  private string name="default name";
  public ClassExam()
  {
   
  }

public ClassExam(string name)
  {
   this.name =name;
  }

public void printName()
  {
   Console .WriteLine (this.ToString ()+"'s name is:");
  Console .WriteLine (this.name );

}
  }
}

//程序入口:

namespace ActivatorCreateInstance
{
 /// <summary>
 /// main 的摘要说明。
 /// </summary>
 public class main
 {
  public main()
  {
   
  }

public static void Main()
  {

//用传递参数来得到一个类的实例

//用Activator .CreateInstance创建函数实例,默认的不带参数的构造函数
   IObjcet obj=(IObjcet)Activator .CreateInstance(System.Type .GetType ("ActivatorCreateInstance.ClassExam,ActivatorExample" ),null);
   //System.Type .GetType  命名空间.类名,程序集
   obj.printName();

//调用ClassExam类的另一个带参数构造函数
   IObjcet obj2=(IObjcet)System.Activator .CreateInstance (System.Type .GetType ("ActivatorCreateInstance.ClassExam,ActivatorExample" ),new string []{"seted new name"});
   obj2.printName ();
  }
 }
}

用Activator.CreateInstance代替new实现类的实例化(转)的更多相关文章

  1. 用Activator.CreateInstance代替new实现类的实例化

    一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...

  2. C# Activator.CreateInstance 动态创建类的实例(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. C# Activator.CreateInstance 动态创建类的实例(一)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

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

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

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

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

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

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

  7. C# Activator.CreateInstance()

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

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

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

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

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

随机推荐

  1. ocp 1Z0-042 61-120题解析

    61. View the Exhibit.Which statement regarding the dept and emp tables is true?A) When you delete a ...

  2. Spring EL method invocation example

    In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...

  3. uva 796 Critical Links(无向图求桥)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. CSS 酷站

    http://mikkelbang.com/#!videos

  5. HDU 1068 Girls and Boys (二分图最大独立集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合 ...

  6. oracle对序列的操作

    select t.*, t.rowid from tbl_type t order by t.id desc Select SEQ_TBL_TYPE_ID.NextVal From Dual; ; ; ...

  7. VHDL TestBench基础(转)

    TestBench的主要目标是: 实例化DUT-Design Under Test 为DUT产生激励波形 产生参考输出,并将DUT的输出与参考输出进行比较 提供测试通过或失败的指示 TestBench ...

  8. thinkPHP模板的输出和模型的使用

    a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出 想分配变量可以使用assign方法 c.修改左右定界符 休要修改配置文件中的配置项 'TMPL_L_DELIM' ...

  9. 在Mac OS X 10.9上安装nginx

    1. 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Downlo ...

  10. Intel大坑之中的一个:丢失的SSE2 128bit/64bit 位移指令,马航MH370??

    缘由 近期在写一些字符串函数的优化,兴趣使然.但是写的过程中,想要实现 SSE2 128 bit / 64 bit 的按 bit 逻辑位移.遇到了一个大坑,且听我娓娓道来. 我并不想用什么马航370来 ...