CodeDom提供了对事件的支持,以及我们可以用反射机制对CodeDom生成的事件进行注册,以及调用。本节程序很简单,先贴上我将用CodeDom生成的代码:
- namespace CodeDomDemo3 { using System; public class CodeDomDemo3 { public event System.EventHandler MyEvent; protected virtual void OnHandle(System.EventArgs e) { if ((this.MyEvent != null)) { this.MyEvent(this, e); } } public void CallEvent() { this.OnHandle(EventArgs.Empty); } } }
代码很简单哦,主要是看CodeDom的事件定义机制和反射调用CodeDom编译的类型方法事件。
CodeDom代码为:
- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.CodeDom; namespace CodeDomDemo1 { public class CodeDomDemo3 { public CodeNamespace CreateCodeNamespcae() { CodeNamespace nspcae = new CodeNamespace("CodeDomDemo3"); nspcae.Imports.Add(new CodeNamespaceImport("System")); nspcae.Types.Add(CrateClassType()); return nspcae; } public CodeTypeDeclaration CrateClassType() { //field event CodeMemberEvent myevent = new CodeMemberEvent(); myevent.Attributes = MemberAttributes.Public; myevent.Type = new CodeTypeReference(typeof(EventHandler)); myevent.Name = "MyEvent"; // event method OnHandle; CodeMemberMethod method = new CodeMemberMethod(); method.Name = "OnHandle"; method.Attributes = MemberAttributes.Family; method.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(EventArgs)), "e")); method.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeEventReferenceExpression(
- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.CodeDom; namespace CodeDomDemo1 { public class CodeDomDemo3 { public CodeNamespace CreateCodeNamespcae() { CodeNamespace nspcae = new CodeNamespace("CodeDomDemo3"); nspcae.Imports.Add(new CodeNamespaceImport("System")); nspcae.Types.Add(CrateClassType()); return nspcae; } public CodeTypeDeclaration CrateClassType() { //field event CodeMemberEvent myevent = new CodeMemberEvent(); myevent.Attributes = MemberAttributes.Public; myevent.Type = new CodeTypeReference(typeof(EventHandler)); myevent.Name = "MyEvent"; // event method OnHandle; CodeMemberMethod method = new CodeMemberMethod(); method.Name = "OnHandle"; method.Attributes = MemberAttributes.Family; method.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(EventArgs)), "e")); method.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeEventReferenceExpression(
- new CodeThisReferenceExpression(),"MyEvent"), CodeBinaryOperatorType.IdentityInequality,new CodePrimitiveExpression(null)), new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(
- {new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("EventArgs"),"Empty") })); CodeTypeDeclaration myclass = new CodeTypeDeclaration("CodeDomDemo3"); myclass.Attributes = MemberAttributes.Public; myclass.Members.AddRange(new CodeTypeMember[] { myevent, method, callEvent }); return myclass; } } }
public CodeDelegateInvokeExpression( targetObject,//事件的引用 [] parameters//参数 )
2:事件的添加(+=):
public CodeAttachEventStatement( targetObject,//目标对象 string eventName,//事件名 listener//监听者 )
3:事件的移除(-=):
public CodeRemoveEventStatement( targetObject,//目标对象 string eventName,//事件名 listener//监听者 )
利用反射调用事件代码:
- 4567 Type t = result.CompiledAssembly.GetType("CodeDomDemo3.CodeDomDemo3");//获取类型 object obj= Activator.CreateInstance(t);//创建实例 t.GetEvent("MyEvent").AddEventHandler(obj, new EventHandler(pro.CallEvent));//添加事件 t.GetMethod("CallEvent").Invoke(obj, null);//触发事件
本示例代码下载:CodeDomDemo3
本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/835607,如需转载请自行联系原作者