有关反射的一些理解
来源:互联网 发布:大数据运维管理 编辑:程序博客网 时间:2024/04/28 01:13
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
namespace xmlTojson
{
public partial class DynamicForm : Form
{
public DynamicForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.CheckFileExists = true;
dlg.Filter = "程序集|*.exe|程序集|*.dll|所有文件|*.*";
//dlg.Filter = "程序集|*.exe;*.dll|所有文件|*.*";
if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
//Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
string friendlyname = AppDomain.CurrentDomain.FriendlyName;
//friendlyname = Application.StartupPath;
AppDomainSetup setss = new AppDomainSetup();
setss.ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
setss.ShadowCopyFiles = "true";
setss.ShadowCopyDirectories = setss.ApplicationBase;
AppDomain appdomain = AppDomain.CreateDomain("aaa", null, setss);
appdomain.ExecuteAssembly(@"Bin\DCSoft.WinFormDemo.exe");
File.Delete(@"Bin\DCSoft.WinFormDemo.exe");
Assembly assembly = Assembly.LoadFrom(dlg.FileName);
if (assembly != null)
{
Type typeClass = null;
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.Name == "类名")
{
typeClass = type;
break;
}
}
#region 获取类型中的构造函数
//获取类型中的构造函数
ConstructorInfo[] Constructors = typeClass.GetConstructors();
//无参构造函数 初始化
object sss = Constructors[0].Invoke(new object[] { });
////有参构造函数 初始化
////object sss1 = Constructors[0].Invoke(new object[] { 1, 2, 2 });
////另外一种方式 初始化
//object sss2 = assembly.CreateInstance("namespace.class");
////另外一种方式---最佳方式 初始化
//object sss3 = Activator.CreateInstance(typeClass, new object[] {1,2,3 });
#endregion
//获取方法的自定义特性
Attribute assemblyattribute = Attribute.GetCustomAttribute(typeClass, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(typeClass, typeof(xmlTojson.MyTestProperty), true);
#region 获取类型中的方法
//获取类型中的方法
MethodInfo[] methods = typeClass.GetMethods(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public);
foreach (MethodInfo method in methods)
{
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(method, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(xmlTojson.MyTestProperty), true);
//方法的调用
method.Invoke(sss, new object[] { 1, 4 });
}
#endregion
#region 获取类型中的属性
//获取类型中的属性
PropertyInfo[] properties = typeClass.GetProperties();
foreach (PropertyInfo property in properties)
{
//property.SetValue()
Attribute[] propertyattribute = (Attribute[])property.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(property, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
}
#endregion
#region 获取类型中的字段
//获取类型中的字段
FieldInfo[] fields = typeClass.GetFields();
foreach (FieldInfo field in fields)
{
Attribute[] propertyattribute = (Attribute[])field.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(field, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
field.SetValue("字段名","值");
}
#endregion
#region 获取类型中的事件
//获取类型中的事件
EventInfo[] events = typeClass.GetEvents();
foreach (EventInfo @event in events)
{
Attribute[] propertyattribute = (Attribute[])@event.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(@event, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
//给事件新增程序的处理方法
@event.AddEventHandler(new Control(), Delegate.CreateDelegate(@event.EventHandlerType, typeClass.GetMethod("方法名")));
}
#endregion
}
this.richTextBox1.Text = dlg.FileName;
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
namespace xmlTojson
{
public partial class DynamicForm : Form
{
public DynamicForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.CheckFileExists = true;
dlg.Filter = "程序集|*.exe|程序集|*.dll|所有文件|*.*";
//dlg.Filter = "程序集|*.exe;*.dll|所有文件|*.*";
if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
//Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
string friendlyname = AppDomain.CurrentDomain.FriendlyName;
//friendlyname = Application.StartupPath;
AppDomainSetup setss = new AppDomainSetup();
setss.ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
setss.ShadowCopyFiles = "true";
setss.ShadowCopyDirectories = setss.ApplicationBase;
AppDomain appdomain = AppDomain.CreateDomain("aaa", null, setss);
appdomain.ExecuteAssembly(@"Bin\DCSoft.WinFormDemo.exe");
File.Delete(@"Bin\DCSoft.WinFormDemo.exe");
Assembly assembly = Assembly.LoadFrom(dlg.FileName);
if (assembly != null)
{
Type typeClass = null;
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.Name == "类名")
{
typeClass = type;
break;
}
}
#region 获取类型中的构造函数
//获取类型中的构造函数
ConstructorInfo[] Constructors = typeClass.GetConstructors();
//无参构造函数 初始化
object sss = Constructors[0].Invoke(new object[] { });
////有参构造函数 初始化
////object sss1 = Constructors[0].Invoke(new object[] { 1, 2, 2 });
////另外一种方式 初始化
//object sss2 = assembly.CreateInstance("namespace.class");
////另外一种方式---最佳方式 初始化
//object sss3 = Activator.CreateInstance(typeClass, new object[] {1,2,3 });
#endregion
//获取方法的自定义特性
Attribute assemblyattribute = Attribute.GetCustomAttribute(typeClass, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(typeClass, typeof(xmlTojson.MyTestProperty), true);
#region 获取类型中的方法
//获取类型中的方法
MethodInfo[] methods = typeClass.GetMethods(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public);
foreach (MethodInfo method in methods)
{
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(method, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(xmlTojson.MyTestProperty), true);
//方法的调用
method.Invoke(sss, new object[] { 1, 4 });
}
#endregion
#region 获取类型中的属性
//获取类型中的属性
PropertyInfo[] properties = typeClass.GetProperties();
foreach (PropertyInfo property in properties)
{
//property.SetValue()
Attribute[] propertyattribute = (Attribute[])property.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(property, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
}
#endregion
#region 获取类型中的字段
//获取类型中的字段
FieldInfo[] fields = typeClass.GetFields();
foreach (FieldInfo field in fields)
{
Attribute[] propertyattribute = (Attribute[])field.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(field, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
field.SetValue("字段名","值");
}
#endregion
#region 获取类型中的事件
//获取类型中的事件
EventInfo[] events = typeClass.GetEvents();
foreach (EventInfo @event in events)
{
Attribute[] propertyattribute = (Attribute[])@event.GetCustomAttributes(true);
//获取方法的自定义特性
Attribute attribute = Attribute.GetCustomAttribute(@event, typeof(xmlTojson.MyTestProperty), true);
//Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
//给事件新增程序的处理方法
@event.AddEventHandler(new Control(), Delegate.CreateDelegate(@event.EventHandlerType, typeClass.GetMethod("方法名")));
}
#endregion
}
this.richTextBox1.Text = dlg.FileName;
}
}
}
}
}
0 0
- 有关反射的一些理解
- 有关catalog的一些理解
- 有关json的一些理解
- 有关傅里叶的一些理解
- 反射技术的一些理解
- 关于反射的一些理解
- 有关OOM KILLER的一些理解
- 有关动态规划的一些理解
- linux中有关环境变量的一些理解
- 关于java反射的一些理解
- 对java反射机制的一些理解
- 有关Qt事件的接收与忽略的一些理解
- 有关socket网络编程中一些概念的理解
- 有关BCD码,ASCII码的一些理解
- 有关PHP函数:array_diff()与array_diff_assoc()的一些理解点
- 有关网络协议融合、存储关键知识点的一些理解
- 对于java反射的一些学习理解和认知
- 关于 回调 、 反射 和异步的一些理解---初级知识
- 日志记录—项目日志处理方案
- postgresql分表及数据迁移
- 小白的github个人博客稳搭绕坑详解
- CSS备忘录
- 做Android中遇到的问题,集锦
- 有关反射的一些理解
- Linux内核的整体架构
- setContentView究竟干了什么?(API21)
- 懒加载的形式创建一个按钮
- React-Native开发(二)-ListView
- 程序猿装B指南
- 为按钮添加监听事件
- Linux系统/etc/sysconfig目录下无iptables文件
- 数据可视化分析工具大集合