Link to home
Start Free TrialLog in
Avatar of programmerist 1983
programmerist 1983Flag for Türkiye

asked on

why i need Activator CreateInstance?

Why i need Activator CreateInstance? i don't need to use create new instance via Activator createInstance can you explain why i need Activator. can you give some advise when i need to use 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, calcType.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; }
    }
}

Open in new window

i can do that without 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();

        }
    }
}

Open in new window

why i need create Instance? Can you show me?
Avatar of saragani
saragani

Hi, in this case you don't need the Activator...
You should better have this:

public void Save<TModel>(object[] Vals) where TModel : new()


This way you can also have value types (like int) as the TModel.
ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial