2011-04-25 156 views
1

我不需要使用通过Activator createInstance创建新实例,那么为什么我需要它呢?在哪些情况下我需要使用Activator.CreateInstance()?为什么我需要使用Activator CreateInstance?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace App.CreateInstance 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      new MyCustomerManager().Save <MyCustomer>(new object[] { 
        1, 
        "xxx", 
        "yyyy" }); 
      } 
    } 

    public class MyCustomerManager 
    { 
     public void Save<TModel>(object[] Vals) 
     { 
      Type calcType = typeof(TModel); 
      object instance = Activator.CreateInstance(calcType); 
      PropertyInfo[] ColumnNames = instance.GetType() 
               .GetProperties(); 

      for (int i = 0; i < ColumnNames.Length; i++) 
      { 
       calcType.GetProperty(ColumnNames[i].Name, 
             BindingFlags.Instance 
            | BindingFlags.Public ) 
       .SetValue(instance, Vals[i], null); 
      } 

      string result = ""; 
      for (int i = 0; i < ColumnNames.Length; i++) 
      { 
       result += String.Format("{0}:{1}", 
        ColumnNames[i].Name, c 
        alcType.GetProperty(ColumnNames[i].Name, 
              BindingFlags.Instance 
             | BindingFlags.Public ) 
          .GetValue(instance, null).ToString()); 
      } 
      Console.WriteLine(result); 
      Console.ReadKey(); 
     } 
    } 

    // Model 
    public class MyCustomer 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string SurName { get; set; } 
    } 
} 

我可以做,没有Activator.CreateInstance:

using System.Reflection; 

namespace App.ReflectionToGeneric4 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      object[] Vals = new object[] { 1, "xxx","yyyy" }; 
      new MyCustomerManager().Save<MyCustomer>(Vals); 
     } 
    } 

    // Model 
    public class MyCustomer 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string SurName { get; set; } 
    } 

    public class MyCustomerManager 
    { 
     public void Save<TModel>(object[] Vals) 
           where TModel : class, new() 
     { 
      var instance = new TModel(); 
      Type calcType = instance.GetType(); 
      PropertyInfo[] ColumnNames = calcType.GetProperties(); 

      for (int i = 0; i < ColumnNames.Length; i++) 
      { 
        calcType.GetProperty(ColumnNames[i].Name, 
              BindingFlags.Instance 
             | BindingFlags.Public ) 
          .SetValue(instance, Vals[i], null); 
      } 
      string result = ""; 
      for (int i = 0; i < ColumnNames.Length; i++) 
      { 
       result += String.Format("{0}:{1}", 
        ColumnNames[i].Name, 
        calcType.GetProperty(ColumnNames[i].Name, 
              BindingFlags.Instance 
             | BindingFlags.Public) 
          .GetValue(instance, null).ToString()); 
      } 
      Console.WriteLine(result); 
      Console.ReadKey(); 

     } 
    } 
} 

回答

12

方案:

  • 没有使用仿制药,而只是基于Type代码
  • 您使用泛型,但构造函数需要参数 - new()仅限于无参数构造函数
  • 有参数的构造函数,但它是不可访问(如果你问IIRC激活将使用私有的构造)

但是,是的,你的情况new()约束是理想的。

+1

@Marck:+1解释 – 2011-04-25 11:56:50

+0

我需要更深入的解释:( – programmerist 2011-04-25 12:45:38

+0

@programmerist - 你不明白什么?Marc列举了你必须使用Activator.CreateInstance的情况,这就是你所问的。大多数情况下,最好是直接调用构造函数,就像第二个代码中使用'new()'约束。 – Justin 2011-04-25 13:04:50

相关问题