C#在类工厂中动态创建类的实例,所使用的方法为:

1. Activator.CreateInstance (Type)

2. Activator.CreateInstance (Type, Object[])

 

两种方法区别仅为:创建无参数的构造方法和创建有参数的构造函数。

//Activator.CreateInstance(Type)

object result = null;
Type typeofControl =null;

typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl);

//Activator.CreateInstance(Type,Object[])

object result = null;
Type typeofControl =null;

typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);

但是在动态创建时,可能会动态使用到外部应用的DLL中类的实例,则此时需要进行反编译操作,使用Reflection命名控件下的Assembly类。

//先使用Assembly类载入DLL,再根据类的全路径获取类

object result = null;
Type typeofControl = null;
Assembly tempAssembly;

tempAssembly = Assembly.LoadFrom(vDllName);
typeofControl = tempAssembly.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);

参考处处:http://blog.csdn.net/jaydawson/article/details/5539438

--------------------------------------------------

参考二:

--------------------------------------------------

Activator.CreateInstance 方法 (Type) 

使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

C# 
public static Object CreateInstance (Type type)

Activator.CreateInstance 泛型方法 ()

注意:此方法在 .NET Framework 2.0 版中是新增的。

创建类型的一个实例,该类型由指定的泛型类型参数指定。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

C# 
public static T CreateInstance<T> ()

Activator.CreateInstance 
(Type)实例
输入一个类的名称为参数,返回一个相应的类的实例。
这在工厂模式中是非常有用的,这样,可以使程序有更高的扩展性


/*
 * Created by SharpDevelop.
 * User: 19004991
 * Date: 2009-8-6
 * Time: 9:10
 */
using System; namespace ActivatorCreateInstance
{
    public interface IObject
    {
        void printName();
    }
    
    public class ClassExam : IObject
    {
        private string name="default name";
        
        public ClassExam()
        {
        }
        
        public ClassExam(string name)
        {
            this.name = name;
        }
        
        public void printName()
        {
            Console .WriteLine (this.name );
        }
    }
    
    public class Program
    {
        public Program()
        {
        }
        
        public static void Main(string[] args)
        {
            // TODO: Implement Functionality Here
            
            IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType ("ActivatorCreateInstance.ClassExam"));
            
            obj1.printName();
            
            IObject obj2=(IObject)Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam"),new string[]{"seted new name"});
            
            obj2.printName();
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

网上代码System.Type.GetType内为"ActivatorCreateInstance.ClassExam,ActivatorExample"编译失败

C# Activator.CreateInstance()的更多相关文章

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

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

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

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

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

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

  4. Activator.CreateInstance 反射实例化对象

    public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...

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

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

  6. 用Activator.CreateInstance代替new实现类的实例化(转)

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

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

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

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

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

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

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

随机推荐

  1. Django自身的CBV列表

    慢慢就有感觉了.... 这个是可以快速开发很多东东,不过,类视图要求的积累还是有一些的.. ~~~~~~~~~~~~~ CBVs在功能上的可扩展性, 牺牲的是简单性, 一个CBV最多的时候拥有8个im ...

  2. 【HDU1538】A Puzzle for Pirates(经典的海盗问题)

    [题目] Description A bunch of pirates have gotten their hands on a hoard of gold pieces and wish to di ...

  3. TCP/IP小纪

    链 路 层 主 要 有 三 个 目 的 :( 1 )为 I P 模 块 发 送 和 接收 I P 数 据 报 ; ( 2 )为 A R P 模块发送 A R P 请 求 和 接 收 A R P 应 答 ...

  4. HDU-1225 Football Score

    http://acm.hdu.edu.cn/showproblem.php?pid=1225 一道超级简单的题,就因为我忘记写return,就wa好久,拜托我自己细心一点. 学习的地方:不过怎么查找字 ...

  5. javascript的几个问题

    基础 1. 可选的分号 只有在缺少了分号就无法正确解析代码的时候,javascript,才会在一行的最后自动添加; a = 3 //自动填充 b = 4; var a a = 3 console.lo ...

  6. 层层递进Struts1(七)详解DispatchAction

    通过前面几篇博客,不知道大家有没有发现这个问题,虽然现在可以灵活控制跳转了,但是Action的数量还是比较多,如何既能保证跳转灵活,还能减少Action的数量?这就是我们这篇博客所说的Dispatch ...

  7. Oracle RAC中的投票算法

    RAC集群中有三台机器,A,B,C A,B,C都会有3票,假设这是A的心跳线出现问题,整个RAC集群就划分为两个paritition, 一个是只有A的partition,一个是B,C组成的partit ...

  8. CSS3新特性(阴影、动画、渐变、变形、伪元素等)

    CSS3与页面布局学习总结(六)--CSS3新特性(阴影.动画.渐变.变形.伪元素等)   目录 一.阴影 1.1.文字阴影 1.2.盒子阴影 二.背景 2.1.背景图像尺寸 2.2.背景图像显示的原 ...

  9. java poi操作excel 添加 锁定单元格保护

    Excel的book保护是很常用的,主要是不想让别人修改Excel的时候用.这样能够避免恶意随便修改数据,提高数据的可信度. 下面介绍JAVA POI来实现设置book保护: 使用HSSFSheet类 ...

  10. 关于封装unity3d的dll时候的进一步总结

    在进行新项目的开发或者接手.后期维护其他一些项目时,经常性的,我们会遇到以下情况: 1.使用新的插件替换已有的bug比较多或者不在维护的插件: 2.因策划需求或美术资源的变更,而不得不更换游戏中的游戏 ...