Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html
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"编译失败
Activator.CreateInstance 方法 (Type) 的用法的更多相关文章
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
- C#中Activator.CreateInstance()方法用法分析
本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...
- 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#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
EF Core使用SQL调用返回其他类型的查询 假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...
- 注意Activator.CreateInstance两个重载方法的性能
今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...
- cSharp:use Activator.CreateInstance with an Interface?
///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
- Assembly.CreateInstance和Activator.CreateInstance
本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射. namespace Factory { public abstract class Factory { public abs ...
随机推荐
- BZOJ3218: a + b Problem
题解: 先做60分... 考虑最小割,连边容量为需要付出的代价.不妨设在s割为黑色,t割为白色. (s,i,b[i])(i,t,w[i]) 关于奇怪,因为不是按份数来的.所以我们这样建图: (i,i+ ...
- dict 字典
Python 学习笔记[dict的操作方法] Python中dict详解
- Github 终于开始认真考虑开源项目许可证了
如今GitHub已成为全球最流行的开源项目托管平台,但也有质疑声音——“Github中的大多数项目并不算是开源项目”.这是因为Github中大多数项目并没有明确声明所使用的许可证. 根据版权法规定,如 ...
- POJ 1401 Factorial
题意:求一个数的阶乘最后边有几个0. 解法:如果有0说明这个数含有2和5这两个因子,对于一个阶乘来说因子2的数量一定比5的数量多,所以只要算有几个5就可以了,依次算5的个数,25的个数,125的个数… ...
- c# 进行AE开发时,如何在地图上定位出一个点
一.文本形式的气泡提示框 由于本人是初学,所以具体的含义尚未弄清楚,直接给出代码吧!
- Grep 命令 用法大全
查找x文件 find / -name "x*" -ls 查找文件中x所在的行数 grep -n "x" -r *find . -name "*.jav ...
- CGAffineTransformScale
[UIViewbeginAnimations:nilcontext:nil]; [UIViewsetAnimationDuration:0.5]; _imgView.transform = CGAff ...
- The Automated Testing Handbook 自动化测试手册简介
Learn what works, what doesn't and why. The Automated Testing Handbook is a practical blueprint for ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)
def get_selected_list_labels(self, locator): """Returns the visible labels of selecte ...
- 用duilib制作仿QQ2013动态背景登录器
转载请说明原出处,谢谢~~ 在上一篇博客里,我修复了CActiveXUI控件的bug,从而可以使用flash动画来制作程序的背景,这篇博客说明一下应该怎么使用CActiveXUI控件创建透明无窗体的背 ...