Activator.CreateInstance 方法 (Type) 的用法

摘要:
创建由指定的泛型类型参数指定的类型的实例。

转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html

Activator.CreateInstance 方法 (Type) 的用法
 
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)实例
输入一个类的名称为参数,返回一个相应的类的实例。
这在工厂模式中是非常有用的,这样,可以使程序有更高的扩展性 
复制代码
Activator.CreateInstance 方法 (Type) 的用法第2张/*
Activator.CreateInstance 方法 (Type) 的用法第3张 * Created by SharpDevelop.
Activator.CreateInstance 方法 (Type) 的用法第3张 * User: 19004991
Activator.CreateInstance 方法 (Type) 的用法第3张 * Date: 2009-8-6
Activator.CreateInstance 方法 (Type) 的用法第3张 * Time: 9:10
Activator.CreateInstance 方法 (Type) 的用法第7张 */
Activator.CreateInstance 方法 (Type) 的用法第8张using System;
Activator.CreateInstance 方法 (Type) 的用法第8张
Activator.CreateInstance 方法 (Type) 的用法第8张namespace ActivatorCreateInstance
Activator.CreateInstance 方法 (Type) 的用法第2张{
Activator.CreateInstance 方法 (Type) 的用法第3张    public interface IObject
Activator.CreateInstance 方法 (Type) 的用法第13张    {
Activator.CreateInstance 方法 (Type) 的用法第3张        void printName();
Activator.CreateInstance 方法 (Type) 的用法第15张    }
Activator.CreateInstance 方法 (Type) 的用法第3张    
Activator.CreateInstance 方法 (Type) 的用法第3张    public class ClassExam : IObject
Activator.CreateInstance 方法 (Type) 的用法第13张    {
Activator.CreateInstance 方法 (Type) 的用法第3张        private string name="default name";
Activator.CreateInstance 方法 (Type) 的用法第3张        
Activator.CreateInstance 方法 (Type) 的用法第3张        public ClassExam()
Activator.CreateInstance 方法 (Type) 的用法第13张        {
Activator.CreateInstance 方法 (Type) 的用法第15张        }
Activator.CreateInstance 方法 (Type) 的用法第3张        
Activator.CreateInstance 方法 (Type) 的用法第3张        public ClassExam(string name)
Activator.CreateInstance 方法 (Type) 的用法第13张        {
Activator.CreateInstance 方法 (Type) 的用法第3张            this.name = name;
Activator.CreateInstance 方法 (Type) 的用法第15张        }
Activator.CreateInstance 方法 (Type) 的用法第3张        
Activator.CreateInstance 方法 (Type) 的用法第3张        public void printName()
Activator.CreateInstance 方法 (Type) 的用法第13张        {
Activator.CreateInstance 方法 (Type) 的用法第3张            Console .WriteLine (this.name );
Activator.CreateInstance 方法 (Type) 的用法第15张        }
Activator.CreateInstance 方法 (Type) 的用法第15张    }
Activator.CreateInstance 方法 (Type) 的用法第3张    
Activator.CreateInstance 方法 (Type) 的用法第3张    public class Program
Activator.CreateInstance 方法 (Type) 的用法第13张    {
Activator.CreateInstance 方法 (Type) 的用法第3张        public Program()
Activator.CreateInstance 方法 (Type) 的用法第13张        {
Activator.CreateInstance 方法 (Type) 的用法第15张        }
Activator.CreateInstance 方法 (Type) 的用法第3张        
Activator.CreateInstance 方法 (Type) 的用法第3张        public static void Main(string[] args)
Activator.CreateInstance 方法 (Type) 的用法第13张        {
Activator.CreateInstance 方法 (Type) 的用法第3张            // TODO: Implement Functionality Here
Activator.CreateInstance 方法 (Type) 的用法第3张            
Activator.CreateInstance 方法 (Type) 的用法第3张            IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType ("ActivatorCreateInstance.ClassExam"));
Activator.CreateInstance 方法 (Type) 的用法第3张            
Activator.CreateInstance 方法 (Type) 的用法第3张            obj1.printName();
Activator.CreateInstance 方法 (Type) 的用法第3张            
Activator.CreateInstance 方法 (Type) 的用法第13张            IObject obj2=(IObject)Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam"),new string[]{"seted new name"});
Activator.CreateInstance 方法 (Type) 的用法第3张            
Activator.CreateInstance 方法 (Type) 的用法第3张            obj2.printName();
Activator.CreateInstance 方法 (Type) 的用法第3张            
Activator.CreateInstance 方法 (Type) 的用法第3张            Console.Write("Press any key to continue . . . ");
Activator.CreateInstance 方法 (Type) 的用法第3张            Console.ReadKey(true);
Activator.CreateInstance 方法 (Type) 的用法第15张        }
Activator.CreateInstance 方法 (Type) 的用法第15张    }
Activator.CreateInstance 方法 (Type) 的用法第7张}
复制代码

网上代码System.Type.GetType内为"ActivatorCreateInstance.ClassExam,ActivatorExample"编译失败
 
  • 相关阅读:
    [skill][c][ld][gcc] 明确指定gcc在链接时明确使用静态库
    [administrative][lvm] lvm 分区修改
    [administrative] windows 下制作USB启动盘的工具
    [administrative][archlinux][netctl][wpa_supplicant] 查看WIFI链接信息
    [skill][makefile] makefile 常用内容记录
    [security] security engine things
    [administrative][CentOS] 新装系统时如何正确精准的选择基础环境和软件包
    [filesystem][archlinux][disk encryption][btrfs] btrfs
    [cipher][archlinux][disk encryption][btrfs] 磁盘分区加密 + btrfs
    [administrative][archlinux][clonezilla][disk cloning] 一块 windows 10 硬盘的备份
  • 原文地址:https://www.cnblogs.com/chengjun/p/4555224.html
  • 免责声明:文章转载自《Activator.CreateInstance 方法 (Type) 的用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

    上篇蓝牙的L2CAP协议ElasticSearch7.4.2安装、使用以及与SpringBoot的整合下篇

    宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

    相关文章

    Java 简单的excel读写操作

    描述:简单的对excel的第一个Sheet表的读写操作 依赖包:apache.poi/poi-3.14、apache.poi/poi-ooxml-3.14 package excel; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel....

    java 类变量初始化顺序

    假定有一个类定义如下: package com.zhang; public final class Girl { // static代码块1 private static String sex = "female"; // 成员方法代码块1 private String name = "anonymous";...

    缺陷管理工具JIRA破解版及其安装方法

    JIRA是一个优秀的问题(or bugs,task,improvement,new feature )跟踪及管理软件。    它由Atlassian开发,采用J2EE技术.它正被广泛的开源软件组织,以及全球著名的软件公司使用,它堪称是J2EE的Bugzilla。   JIRA提供了全面的功能,界面十分友好,可用性以及可扩展性方面都十分出色,如果购买商业版许...

    system 系统调用、gcc编译过程

    system 库函数的功能是执行操作系统的命令或者运行指定的程序 #include <stdio.h> #include <stdlib.h>//引入库 int main() { //system("notepad");//打开记事本 system("dir");//查看目录   system("ipconfig")...

    explorer.exe命令行

    explorer.exe命令行 说明: 当进程中没有explorer.exe时,运行explorer.exe即启动外壳程序;当外壳程序已经启动时,运行explorer.exe将会打开“我的文档"。 参数 /n 为默认选择内容打开一个新的单窗格窗口。默认选择内容通常是安装windows的驱动器的根目录 /e 使用默认视图启动windows资源管理器 /roo...

    20169205实验一 Java开发环境的熟悉(Linux+IDEA)

    20169205实验一 Java开发环境的熟悉(Linux+IDEA) 实验内容及步骤 使用JDK编译、运行简单的Java程序 打开windows下的cmd → 输入cd Code命令进入Code目录 → 输入md 20169205建立以自己学号命名的文件夹 → 输入cd 20169205进入文件夹 → 输入md exp1建立“实验一”文件夹 → 输入cd...