用Activator.CreateInstance代替new实现类的实例化
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例.
这在工厂模式中是非常有用的
这样,可以使程序有更高的扩展性,例如,,下面的例子
如果现在有一个类,专门用来计算交通工具的速度,不同的交通工具计算方法是不一样的,但是到底有那些交通工具是未知的或者是可变的,这种情况下,我们可能觉得要在添加交通工具的时候,需要修改用来计算速度的那个类,
但如果用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实现类的实例化的更多相关文章
- 用Activator.CreateInstance代替new实现类的实例化(转)
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...
- C# Activator.CreateInstance 动态创建类的实例(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# Activator.CreateInstance 动态创建类的实例(一)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- 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#在类工厂中 ...
随机推荐
- 使用 Linux Mint 作为主要操作系统的一些个人常用软件
本篇文章讲一下一些 Linux 上的应用,多数为日常生活娱乐用的软件,同时也会讲一点开发工具,对于有兴趣继续研究 Linux 的可以参考一下. 目录 软件的安装方式 1.Software Manage ...
- FMX开源控件
FMX开源控件 这是群友谢顿做的控件,必须赞一个! https://github.com/zhaoyipeng/FMXComponents 这是loki的: https://sourceforge.n ...
- cmake log
20:28:54: 为项目RoboticArmProject_CarTerminal_V20190530执行步骤 ...20:28:54: 正在启动 "/usr/bin/make" ...
- BigDecimal的toString和toPlainString区别
创建一个BigDecimal的数,分别调用toPlainString和toString方法 import java.math.BigDecimal; public class Test05 { pub ...
- React Native Expected a component class,got [object Object]解决
报错原因: 组件大小写错误. 解决方式: 修改组件名称即可. 这篇博客介绍了大部分RN的错误原因和解决方法: http://blog.csdn.net/chichengjunma/article/de ...
- Hadoop 部署之 Hive (五)
目录 一.Hive 简介 1.什么是 Hive 2.为什么使用 Hive 3.Hive 的特点 4.Hive 的架构 二.Hive 安装 1.MySQL 安装(datanode01) 2.MySQL ...
- eclipse运行mapreduce的wordcount
1,eclipse安装hadoop插件 插件下载地址:链接: https://pan.baidu.com/s/1U4_6kLFNiKeLsGfO7ahXew 提取码: as9e 下载hadoop-ec ...
- idea配置svn,maven,jdk和一些基础设置
1.idea配置svn 1.1 首先下载svn,百度云链接:链接:https://pan.baidu.com/s/1PvSBuHcHMrrBHfnOfVRC9Q 提取码:hs7l 1.2 开始安装 这 ...
- thinkphp5相关
THINKPHP5代码风格规范(基于PSR): https://www.jianshu.com/p/e53d26407e68
- 对于大于等于3的整数n,在区间【n,3/2 * n】中一定存在一个素数
对于大于3的整数n,在区间[n,3/2 * n]中一定存在一个素数