激活剂以及我们如何使用它 [英] activator and how we can use it
本文介绍了激活剂以及我们如何使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
激活类的确切用途是什么?
what is the exact use of activator class???
Type ClassType = Type.GetType(ClassName + ", TMS.API, Version=1.0.0.0, Culture=neutral, Custom=null");
try
{
// Open connection
OpenConnection();
// Create the command
//Execute the SP and return Reader
dataReaderToFill = ExecuteReaderForXML(spName, baseObject.GetXML());
int ColumnLength = dataReaderToFill.FieldCount;
while (dataReaderToFill.Read())
{
Object ClassObject = Activator.CreateInstance(ClassType);
...
推荐答案
Quote:
包含在本地或远程创建对象类型的方法,或获取对现有远程对象的引用。
Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects.
System.Activator [ ^ ]
在上面的例子中,它是用于根据类的类型创建类。类的类型是在第一行中动态创建的。
System.Activator[^]
In the case above, its being used to create a class based on the type of the class. The type of the class is created dynamically in the first line.
Activator是.Net中内置的反射内容的一部分,因此适用于动态实例化类。 />
如果您使用的是工厂模式,Activator是您最好的朋友。
我经常使用它进程处理程序工厂(不要将它与HTTP处理程序混淆,它们是不同的)。我构建了一个抽象基类和一些使用该基类的进程,调用它的抽象方法来完成某些事情。然后我将子类的完全限定类型名称放入数据库或其他配置中,并从中进行实例化。
通常情况如下:
Activator is part of the reflection stuff built into .Net, and as such, is good for dynamically instantiating a class.
If you're using the factory pattern, Activator is your best friend.
I frequently use it for process-handler factories (don't confuse this with HTTP handlers, they're different). I build an abstract base class and some processes that use that base class, calling its abstract methods to get something done. Then I put the fully-qualified type name of the child classes into a database or other configuration, and instantiate from that.
Typically it goes something like this:
// do something to get type name here, for now, just pretend it's stored in childClassName
Type t = Type.GetType(childClassName);
FooBaseClass fbc = (FooBaseClass)Activator.CreateInstance(t);
// fbc is ready to use as an instance of a child class of FooBaseClass
这是配置驱动工厂的核心。
That's the core of a configuration-driven factory.
这篇关于激活剂以及我们如何使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文