C# 为什么我需要使用Activator CreateInstance?

C# 为什么我需要使用Activator CreateInstance?,c#,.net,asp.net,visual-studio,C#,.net,Asp.net,Visual Studio,我不需要通过Activator createInstance使用创建新实例,为什么我需要它?在哪些情况下我需要使用Activator.CreateInstance() 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 运用系统反思; 命名空间App.CreateInstance { 班级计划 { 静态void Main(字符串[]参数) { 新建MyCustomerManager().Save(新对象[]{ 1. “xxx”,

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

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
运用系统反思;
命名空间App.CreateInstance
{
班级计划
{
静态void Main(字符串[]参数)
{
新建MyCustomerManager().Save(新对象[]{
1.
“xxx”,
“yyy”});
}
}
公共类MyCustomerManager
{
公共作废保存(对象[]VAL)
{
类型calcType=typeof(TModel);
对象实例=Activator.CreateInstance(calcType);
PropertyInfo[]ColumnNames=instance.GetType()
.GetProperties();
for(int i=0;i
我可以在没有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();

        }
    }
}
使用系统反射;
命名空间App.reflectionGeneric4
{
班级计划
{
静态void Main(字符串[]参数)
{
object[]Vals=新对象[]{1,“xxx”,“yyyy”};
新建MyCustomerManager().Save(VAL);
}
}
//模型
公共类MyCustomer
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
}
公共类MyCustomerManager
{
公共作废保存(对象[]VAL)
其中TModel:class,new()
{
var实例=新的TModel();
类型calcType=instance.GetType();
PropertyInfo[]ColumnNames=calcType.GetProperties();
for(int i=0;i
场景:

  • 您使用的不是泛型,而是基于
    类型的代码
  • 您使用的是泛型,但构造函数接受参数-
    new()
    仅限于无参数构造函数
  • 有一个无参数构造函数,但它不可访问(如果您要求,IIRC Activator将使用私有构造函数)
但是是的,在您的情况下,
new()
约束是理想的。

场景:

  • 您使用的不是泛型,而是基于
    类型的代码
  • 您使用的是泛型,但构造函数接受参数-
    new()
    仅限于无参数构造函数
  • 有一个无参数构造函数,但它不可访问(如果您要求,IIRC Activator将使用私有构造函数)

但是是的,在您的例子中,
new()
约束是理想的。

@Marck:+1对于解释,我需要更深入的解释:(@programmerist-你还不明白什么?Marc列出了你必须使用Activator.CreateInstance的情况,这正是你所要求的。在大多数其他情况下,最好直接调用构造函数,就像在你的第二个代码中使用
new()
约束一样。可能是“更深层次的”这个问题可能会得到您需要的回答?@programmerist很乐意添加更多,但您需要告诉我您不清楚的地方。但作为一个例子,在protobuf net中,我在编译时不知道类型,它使用基于
类型的代码作为主API;因此它不能使用
new()
-它必须改用
激活器
。@Marck:+1解释我需要更深入的解释:(@programmerist-你还不明白什么?Marc列出了你必须使用Activator.CreateInstance的情况,这正是你所要求的。在大多数其他情况下,最好直接调用构造函数,就像在你的第二个代码中使用
new()
约束一样。可能是“更深层次的”这个问题可能会得到您需要的回答?@programmerist很乐意添加更多,但您需要告诉我您不清楚的地方。但作为一个例子,在protobuf net中,我在编译时不知道类型,它使用基于
类型的代码作为主API;因此它不能使用
new()
-它必须使用
激活器
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();

        }
    }
}