摘要:本文主要向大家介绍了C#编程之C#反射--------创建对象实例,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C#反射--------创建对象实例,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
C#反射--------创建对象实例
C#创建对象实例共有三种方法
(1)通过System.Reflection.Assmbly里方法获得实例,主要根据类的Fullname和构造函数的参数创建实例,包括了私有和公有的,很强大
public object CreateInstance(string typeName);//使用区分大小写的方式找到指定的类型名称,FullName, public object CreateInstance(string typeName, bool ignoreCase);//使用指定的类型名称,FullName,是否区分大小写由参数决定
//使用多种参数,binder为Null,不知道什么东西,args为构造函数的参数,culture和activation可以为null,不知道什么东西 public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);//创建实例的例子:var obj = Assembly.LoadFrom(@"D:\应用软件\VS2017\C#\WindowsFormsApp16\ClassLibrary1\bin\Debug\ClassLibrary1.dll").CreateInstance(参数)
(2)采用System.Activator里静态方法CreateInstance获得对应的实例
public static object CreateInstance(Type type);//根据公有的无参的构造函数创建对象实例public static object CreateInstance(Type type, bool nonPublic);//如果公共或非公共默认构造函数都可以匹配,则为 true;如果只有公共默认构造函数可以匹配,则为 false。public static object CreateInstance(Type type, object[] args, object[] activationAttributes);//args为构造函数的参数public static object CreateInstance(Type type, params object[] args);public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture);public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);public static ObjectHandle CreateInstance(string assemblyName, string typeName);public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttributes);
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号