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

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

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

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

但如果用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. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  2. Spring AOP Interceptor transaction is not working

    Problem The Spring AOP transaction is not working in following interceptors? <bean id="testA ...

  3. thymeleaf中的模板布局

    一.包括模板片段: 1:定义和引用片段,我们经常会想要包含在模板片段来自其他模板.常见的用途是页脚.标题.菜单…; 为了做到这一点,Thymeleaf需要我们定义包含可用的片段,我们可以通过使用th: ...

  4. HDU1874畅通工程续(floyd||dijkstra)

    看了看floyd和dijkstra,然后就找了两个练习来捉 #include<iostream> #include<stdio.h> #include<string.h& ...

  5. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  6. CodeForces 706B Interesting drink (二分查找)

    题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...

  7. mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息

    /** 在Mysql下执行: delete from my.stu where id not in( select min(id) id from my.stu group by code) ; 用途 ...

  8. [c++]this指针理解

    #include <iostream> using namespace std; /** * this 指针理解 */ class A{ int i; public: void hello ...

  9. struts2的action的知识点和利用action向页面注入值的操作

    1.      Action的顺序,会先搜索指定名字下的包的action,如果找不到会去搜索默认路径下的包下的action. 2.      如果没有给action设置值,那么action会有一些默认 ...

  10. my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables

    FROM http://sqlstudies.com/2013/01/20/my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tab ...