通过反射,创建类的实例三

这次我们通过构造函数的方式处理

using System;
using System.Reflection;

namespace App
{
    public class TypeHelper
    {
        public static object CreateObject<T>(params object[] args) where T : Interface.IObjcet
        {
            try
            {
                Type myType = typeof(T);

                int lenght = 0;
                if (args != null)
                {
                    lenght = args.Length;
                }

                Type[] types = new Type[lenght];
                for (int i = 0; i < args.Length; i++)
                {
                    types[i] = args[i].GetType();
                }

                object[] param = new object[lenght];
                for (int i = 0; i < args.Length; i++)
                {
                    param[i] = args[i];
                }

                object obj = null;

                // Get the constructor that takes an integer as a parameter.
                ConstructorInfo constructorInfoObj = myType.GetConstructor(types);

                //ConstructorInfo constructorInfoObj = myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, types, null);
                //ConstructorInfo constructorInfoObj = myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null,CallingConventions.HasThis, types, null);

                //CustomBinder customBinder = new CustomBinder();
                //ConstructorInfo constructorInfoObj = myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, customBinder, CallingConventions.HasThis, types, null);

                if (constructorInfoObj != null)
                {
                    Console.WriteLine("The constructor of PlugPut that takes an integer as a parameter is: " + constructorInfoObj.ToString());
                    //调用指定参数的构造函数
                    obj = constructorInfoObj.Invoke(param);
                }
                else
                {
                    Console.WriteLine("The constructor of PlugPut that takes an integer as a parameter is not available.");
                    obj = Activator.CreateInstance(myType, null);
                }
                return obj;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("");

        Test1();

        Console.ReadLine();
    }

    static void Test1()
    {
        Console.Write("Put plus value is:");
        string strPlus = Console.ReadLine();

        //无参构造函数
        IObjcet obj = (IObjcet)TypeHelper.CreateObject<NetStar.Biz.PlugPut>();
        obj.Put();
        obj.Put(strPlus);

        //定义构造函数所需参数
        object[] param = new object[1];
        param[0] = strPlus;

        //带参数的构造函数
        obj = (IObjcet)TypeHelper.CreateObject<NetStar.Biz.PlugPut>(param);
        obj.Put();
        obj.Put(strPlus);
    }

    ......



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值