using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Kernel.Interface
{
public interface IObjcet
{
void Put(); void Put(string plus);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kernel.Interface; namespace Kernel.SimpleLibrary
{
public class PlugPut : IObjcet
{ private string plugName = "my plugName value is default!"; public string PlugName
{
get { return plugName; }
set { plugName = value; }
} public PlugPut() { } public PlugPut(string plusName)
{
this.PlugName = plusName;
} public void Put()
{
Console.WriteLine("Default plug value is:" + plugName);
} public void Put(string plus)
{
Console.WriteLine("Put plus value is:" + plus);
}
}
}
using Kernel.DriverLibrary;
using Kernel.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Kernel.TypeLibrary
{
public class TypeHelper
{
public static object CreateObject<T>(params object[] args) where T : IObjcet
{
try
{
Type myType = typeof(T); int lenght = ;
if (args != null)
{
lenght = args.Length;
} Type[] types = new Type[lenght];
for (int i = ; i < args.Length; i++)
{
types[i] = args[i].GetType();
} object[] param = new object[lenght];
for (int i = ; 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());
//Console.WriteLine(constructorInfoObj.ToString()); //调用指定参数的构造函数
obj = constructorInfoObj.Invoke(param);
}
else
{
Console.WriteLine("The constructor of PlugPut that takes an integer as a parameter is not available."); //myType is System.Type.GetType("Kernel.SimpleLibrary.PlugPut,Kernel.SimpleLibrary")
//Activator.CreateInstance(System.Type.GetType("Kernel.SimpleLibrary.PlugPut,Kernel.SimpleLibrary"), null);
obj = Activator.CreateInstance(myType, null);
}
return obj;
}
catch (Exception)
{
throw;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Kernel.DriverLibrary
{
public class CustomBinder : Binder
{
public override MethodBase BindToMethod(
BindingFlags bindingAttr,
MethodBase[] match,
ref object[] args,
ParameterModifier[] modifiers,
CultureInfo culture,
string[] names,
out object state)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
// Arguments are not being reordered.
state = null;
// Find a parameter match and return the first method with
// parameters that match the request.
foreach (MethodBase mb in match)
{
ParameterInfo[] parameters = mb.GetParameters(); if (ParametersMatch(parameters, args))
{
return mb;
}
}
return null;
} public override FieldInfo BindToField(BindingFlags bindingAttr,
FieldInfo[] match, object value, CultureInfo culture)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
foreach (FieldInfo fi in match)
{
if (fi.GetType() == value.GetType())
{
return fi;
}
}
return null;
} public override MethodBase SelectMethod(
BindingFlags bindingAttr,
MethodBase[] match,
Type[] types,
ParameterModifier[] modifiers)
{
if (match == null)
{
throw new ArgumentNullException("match");
} // Find a parameter match and return the first method with
// parameters that match the request.
foreach (MethodBase mb in match)
{
ParameterInfo[] parameters = mb.GetParameters();
if (ParametersMatch(parameters, types))
{
return mb;
}
} return null;
} public override PropertyInfo SelectProperty(
BindingFlags bindingAttr,
PropertyInfo[] match,
Type returnType,
Type[] indexes,
ParameterModifier[] modifiers)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
foreach (PropertyInfo pi in match)
{
if (pi.GetType() == returnType &&
ParametersMatch(pi.GetIndexParameters(), indexes))
{
return pi;
}
}
return null;
} public override object ChangeType(
object value,
Type myChangeType,
CultureInfo culture)
{
try
{
object newType;
newType = Convert.ChangeType(value, myChangeType);
return newType;
}
// Throw an InvalidCastException if the conversion cannot
// be done by the Convert.ChangeType method.
catch (InvalidCastException)
{
return null;
}
} public override void ReorderArgumentArray(ref object[] args,
object state)
{
// No operation is needed here because BindToMethod does not
// reorder the args array. The most common implementation
// of this method is shown below. // ((BinderState)state).args.CopyTo(args, 0);
} // Returns true only if the type of each object in a matches
// the type of each corresponding object in b.
private bool ParametersMatch(ParameterInfo[] a, object[] b)
{
if (a.Length != b.Length)
{
return false;
}
for (int i = ; i < a.Length; i++)
{
if (a[i].ParameterType != b[i].GetType())
{
return false;
}
}
return true;
} // Returns true only if the type of each object in a matches
// the type of each corresponding entry in b.
private bool ParametersMatch(ParameterInfo[] a, Type[] b)
{
if (a.Length != b.Length)
{
return false;
}
for (int i = ; i < a.Length; i++)
{
if (a[i].ParameterType != b[i])
{
return false;
}
}
return true;
}
}
}
using Kernel.DriverLibrary;
using Kernel.Interface;
using Kernel.SimpleLibrary;
using Kernel.TypeLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Text;
using System.Threading.Tasks; namespace Kernel.App
{
class Program
{
static void Main(string[] args)
{
#region
Console.Write("Put plus value is:");
string strPlus = Console.ReadLine(); //无参构造函数
IObjcet obj = (IObjcet)TypeHelper.CreateObject<PlugPut>();
obj.Put();
obj.Put(strPlus); //定义构造函数所需参数
object[] param = new object[];
param[] = strPlus; //带参数的构造函数
obj = (IObjcet)TypeHelper.CreateObject<PlugPut>(param);
obj.Put();
obj.Put(strPlus); #endregion Console.ReadLine();
}
}
}

C# Type.GetConstructor() 根据构造函数参数获取实例对象(一)的更多相关文章

  1. SpringBoot 基于web应用开发(请求参数获取,静态资源,webjars)

    SpringBoot 基于web应用开发 一.Lombok使用 1.导入依赖库 <dependency>    <groupId>org.projectlombok</g ...

  2. hiero.ui获取实例名的方法

    在hiero.ui中经常会通过hiero.ui.windowManager().windows()来获取当前QMainWindow中的QWidget子窗口,而这些子窗口是以实例对象的方式返回的,如果想 ...

  3. JS高级---实例对象和构造函数之间的关系

    实例对象和构造函数之间的关系:   1. 实例对象是通过构造函数来创建的---创建的过程叫实例化   2. 如何判断对象是不是这个数据类型?    1) 通过构造器的方式 实例对象.构造器==构造函数 ...

  4. JS中new的自定义实现创建实例对象

    我们都知道在JS中通常通过对象字面量和new关键字来创建对象,那么今天我就来给大家讲讲new是怎么创建实例对象的:首先创建一个构造函数: function Person(name,age){ this ...

  5. 对ES6中类class以及实例对象、原型对象、原型链之间关系的详细总结

    1. 类 ES6 中新增加了类的概念,可以使用 class 关键字声明一个类,之后用这个类来实例化对象.即类的用途:实例化对象. // 创建一个Person类 class Person { } // ...

  6. 理解Python中的类对象、实例对象、属性、方法

    class Animal(object): # 类对象 age = 0 # 公有类属性 __like = None # 私有类属性 def __init__(self): # 魔法方法 self.na ...

  7. promise核心技术 1 实例对象/函数对象

    一个程序员要在看到代码的语法同时判断数据类型 知道语法是基础  基础才能延伸功能 //一行代码 a()[0]() // a() 首先推断出a是一个函数 //a()[0] 判断a函数的返回值是一个数组 ...

  8. JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇

    前两篇文章分表讲述了 后置处理器 - 正则表达式提取器概述及简单实例.多参数获取,相应博文敬请参阅 简单实例.多参数获取. 此文主要讲述如何引用正则表达式提取器获取的数据信息.其实,正则表达式提取器获 ...

  9. Java反射机制(获取Class对象的三种方式+获取Class中的构造函数进行对象的初始化+获取反射类的字段+获取反射类的一般方法)

    反射技术其实就是动态加载一个指定的类,并获取该类中的所有内容.而且将字节码文件封装成对象,并将字节码文件中的内容都封装成对象,这样便于操作这些成员,简单来说:反射技术可以对一个类进行解剖,反射大大增强 ...

随机推荐

  1. 【贪心】Gym - 100507H - Pair: normal and paranormal

    每次取相邻的两个可以射击的从序列中删除,重复n次. 可以看作括号序列的匹配. #include<cstdio> #include<vector> using namespace ...

  2. C#SerialPort实现串口控制继电器

    最近做了一个小系统,麻雀虽小五脏俱全呀,用到各种线程控制,串口控制等技术.其中串口控制最麻烦,因为继电器的响应很快,根据不同的转接口,返回的数据质量是不一样的,所以不能直接wirte,然后马上read ...

  3. ajax变量作用域的问题

    需求:使得一个变量在ajax内外都有作用域 方法:设置为同步请求 async:false

  4. Debian安装VirtualBox增强工具

    切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...

  5. win10 关键错误开始菜单和cortana无法工作 的问题

    win10点击 '此电脑' 反键选择 管理 -服务和应用程序-服务 找到 User Manager 双击点击启动就行了 把 UserManager服务设为自动启动解决了

  6. JSON和XML的区别

  7. ajax asynx:false

    默认设置下,所有请求均为异步请求.如果需要发送同步请求,请将此选项设置为 false.注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行 (默认: true) 默认设置下,所有请求均为 ...

  8. mysql left join,right join,inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...

  9. Webpack安装和配置

    一.安装和配置webpack 1.全局安装webpack 这样就安装好了webpack,可以再全局通过webpack -v来查看是否安装成功. 2.先创建项目目录结构,根目录是mywebpack.进入 ...

  10. gensim加载word2vec训练结果(bin文件)并进行相似度实验

    # -*- coding: utf-8 -*- import gensim # 导入模型 model = gensim.models.KeyedVectors.load_word2vec_format ...