激活器的用途.用example创建实例

本文关键字:创建 实例 example 激活 | 更新日期: 2023-09-27 18:10:06

谁能详细解释一下Activator.CreateInstance()的用途?

激活器的用途.用example创建实例

假设您有一个名为MyFancyObject的类,如下所示:

class MyFancyObject
{
 public int A { get;set;}
}

它让你转动:

String ClassName = "MyFancyObject";

MyFancyObject obj;
使用

obj = (MyFancyObject)Activator.CreateInstance("MyAssembly", ClassName))

,然后可以执行如下操作:

obj.A = 100;

这就是它的目的。它还有许多其他的重载,比如在字符串中提供Type而不是类名。为什么你会有这样的问题是另一回事。这里有一些人需要它:

  • Createinstance() -我这样做对吗?
  • c# Using Activator调用CreateInstance
  • 在设计时不知道类名的情况下创建对象

我可以给你一个例子,为什么要使用这样的东西。想象一款你想要在XML文件中存储关卡和敌人的游戏。当你解析这个文件时,你可能会有这样一个元素:

<Enemy X="10" Y="100" Type="MyGame.OrcGuard"/>

你现在可以做的是,动态地创建在关卡文件中找到的对象。

foreach(XmlNode node in doc)
   var enemy = Activator.CreateInstance(null, node.Attributes["Type"]);

对于构建动态环境非常有用。当然,它也可以用于插件或插件场景和更多。

我的好朋友MSDN可以给你解释一下,用一个例子

如果链接或内容将来发生变化,下面是代码:

using System;
class DynamicInstanceList
{
    private static string instanceSpec = "System.EventArgs;System.Random;" +
        "System.Exception;System.Object;System.Version";
    public static void Main()
    {
        string[] instances = instanceSpec.Split(';');
        Array instlist = Array.CreateInstance(typeof(object), instances.Length);
        object item;
        for (int i = 0; i < instances.Length; i++)
        {
            // create the object from the specification string
            Console.WriteLine("Creating instance of: {0}", instances[i]);
            item = Activator.CreateInstance(Type.GetType(instances[i]));
            instlist.SetValue(item, i);
        }
        Console.WriteLine("'nObjects and their default values:'n");
        foreach (object o in instlist)
        {
            Console.WriteLine("Type:     {0}'nValue:    {1}'nHashCode: {2}'n",
                o.GetType().FullName, o.ToString(), o.GetHashCode());
        }
    }
}
// This program will display output similar to the following: 
// 
// Creating instance of: System.EventArgs 
// Creating instance of: System.Random 
// Creating instance of: System.Exception 
// Creating instance of: System.Object 
// Creating instance of: System.Version 
// 
// Objects and their default values: 
// 
// Type:     System.EventArgs 
// Value:    System.EventArgs 
// HashCode: 46104728 
// 
// Type:     System.Random 
// Value:    System.Random 
// HashCode: 12289376 
// 
// Type:     System.Exception 
// Value:    System.Exception: Exception of type 'System.Exception' was thrown. 
// HashCode: 55530882 
// 
// Type:     System.Object 
// Value:    System.Object 
// HashCode: 30015890 
// 
// Type:     System.Version 
// Value:    0.0 
// HashCode: 1048575

你也可以这样做-

var handle = Activator.CreateInstance("AssemblyName", 
                "Full name of the class including the namespace and class name");
var obj = handle.Unwrap();

下面是一个很好的例子:例如,您有一组logger,并且允许用户通过配置文件指定在运行时使用的类型。

:

string rawLoggerType = configurationService.GetLoggerType();
Type loggerType = Type.GetType(rawLoggerType);
ILogger logger = Activator.CreateInstance(loggerType.GetType()) as ILogger;

或者另一种情况是当你有一个共同的实体工厂,它创建实体,也负责从数据库接收数据的实体初始化:

(伪代码)
public TEntity CreateEntityFromDataRow<TEntity>(DataRow row)
 where TEntity : IDbEntity, class
{
   MethodInfo methodInfo = typeof(T).GetMethod("BuildFromDataRow");
   TEntity instance = Activator.CreateInstance(typeof(TEntity)) as TEntity;
   return methodInfo.Invoke(instance, new object[] { row } ) as TEntity;
}

Activator.CreateInstance方法使用与指定参数最匹配的构造函数创建指定类型的实例。

例如,假设您有一个字符串形式的类型名,并且您希望使用该字符串来创建该类型的实例。您可以使用Activator.CreateInstance:

string objTypeName = "Foo";
Foo foo = (Foo)Activator.CreateInstance(Type.GetType(objTypeName));

这是一篇MSDN文章,详细解释了它的应用:

http://msdn.microsoft.com/en-us/library/wccyzw83.aspx

在deepee1和this的基础上,这里是如何接受字符串中的类名,然后使用LINQ对数据库进行读写。我使用"dynamic"而不是deepe1的强制转换,因为它允许我分配属性,这允许我们动态地选择和操作任何我们想要的表。

Type tableType = Assembly.GetExecutingAssembly().GetType("NameSpace.TableName");
ITable itable = dbcontext.GetTable(tableType);
//prints contents of the table
foreach (object y in itable) {
    string value = (string)y.GetType().GetProperty("ColumnName").GetValue(y, null);
    Console.WriteLine(value);
}
//inserting into a table
dynamic tableClass = Activator.CreateInstance(tableType);
//Alternative to using tableType, using Tony's tips
dynamic tableClass = Activator.CreateInstance(null, "NameSpace.TableName").Unwrap();
tableClass.Word = userParameter;
itable.InsertOnSubmit(tableClass);
dbcontext.SubmitChanges();
//sql equivalent
dbcontext.ExecuteCommand("INSERT INTO [TableNme]([ColumnName]) VALUES ({0})", userParameter);

加上反射,我找到了Activator。CreateInstance对于将存储过程结果映射到自定义类非常有帮助,如下面的答案所述。

如果你已经知道了这个类,为什么还要使用它呢?为什么不按传统的方式上课,就像你以前那样呢?这与通常的做法相比没有什么优势。是否有一种方法可以获取文本并对其进行操作:

label1.txt = "Pizza" 
Magic(label1.txt) p = new Magic(lablel1.txt)(arg1, arg2, arg3);
p.method1();
p.method2();

如果我已经知道它是披萨,那就没有优势:

p = (Pizza)somefancyjunk("Pizza"); over
Pizza p = new Pizza();