当前位置: 首页 > 编程语言 > C#

有没有办法用反射“覆盖”一个方法?分享

时间:2023-04-11 11:48:18 C#

有没有办法用反射“覆盖”一个方法?是否可以在不继承但仅使用反射的情况下动态更改C#中方法的代码?类似于:nameSpaceA.Foo.method1=aDelegate;我无法更改/编辑Foo类。namespacenameSpaceA{classFoo{privatevoidmethod1(){//...一些代码}}}我的最终目标是更改动态代码:publicstaticIListEnsureNodeSet(IListlistItems);在System.Xml.Xsl.Runtime.XslConvert.cs中转:if(!item.IsNode)thrownewXslTransformException(Res.XPath_NodeSetExpected,string.Empty);输入:if(!item.IsNode)thrownewXslTransformException(Res.XPath_NodeSetExpected,item.value);这个答案的一部分部分是错误的,我只是离开它,以便评论中的演变有意义。见编辑。您不是在寻找反射,而是在寻找发射(这是另一种方式)。特别是,有一种方法可以做你想做的事,幸运的是你!请参阅TypeBuilder.DefineMethodOverride编辑:写这个答案,我只记得remix也允许你这样做。但这更难。Remix是一个在C#中“模拟”mixins的框架。在其基本方面,您可以将其视为具有默认实现的接口。如果再进一步,那就远不止这些了。编辑2:这是一个重新混合的示例(在不支持它并且不知道混合的类上实现INotifyPropertyChanged)。使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用Remotion.Mixins;使用System.ComponentModel;使用混合测试;[程序集:Mix(typeof(INPCTester),typeof(INotifyPropertyChangedMixin))]namespaceMixinTest{//[Remotion.Mixins.CompleteInterface(typeof(INPCTester))]publicinterfaceICustomINPC:INotifyPropertyChanged{voidRaisePropertyChanged(stringprop);}//[扩展(typeof(INPCTester))]publicclassINotifyPropertyChangedMixin:Mixin,ICustomINPC{publicvoidRaisePropertyChanged(stringprop){PropertyChangedEventHandlerhandler=this.PropertyChanged;if(handler!=null){handler(this,newPropertyChangedEventArgs(prop));}}}publicclassImplementsINPCAttribute:UsesAttribute{publicImplementsINPCAttribute():base(typeof(INotifyPropertyChangedMixin)){}}//[ImplementsINPC]publicclassINPCTester{privatestringm_Name;公开icstringName{get{returnm_Name;}set{if(m_Name!=value){m_Name=value;((ICustomINPC)this).RaisePropertyChanged("名称");}}}}公共类INPCTestWithoutMixin:ICustomINPC{privatestringm_Name;publicstringName{get{returnm_Name;}set{if(m_Name!=value){m_Name=value;this.RaisePropertyChanged("姓名");}}}publicvoidRaisePropertyChanged(stringprop){PropertyChangedEventHandlerhandler=this.PropertyChanged;if(handler!=null){handler(this,newPropertyChangedEventArgs(prop));}}公共事件PropertyChangedEventHandlerPropertyChanged;}}而测试:staticvoidINPCImplementation(){Console.WriteLine("INPCimplementationandusage");varinpc=ObjectFactory.Create(ParamList.Empty);Console.WriteLine("生成的对象可转换为INPC:"+(inpcisINotifyPropertyChanged));((INotifyPropertyChanged)inpc).PropertyChanged+=inpc_PropertyChanged;inpc.Name="新名称!";((INotifyPropertyChanged)inpc).PropertyChanged-=inpc_PropertyChanged;控制台.WriteLine();}staticvoidinpc_PropertyChanged(objectsender,PropertyChangedEventArgse){Console.WriteLine("Hello,world!Property'sname:"+e.PropertyName);}//OUTPUT://INPC实现和使用//生成的对象可转换为INPC:True//Hello,world!属性名称:名称注意:[assembly:Mix(typeof(INPCTester),typeof(INotifyPropertyChangedMixin))]和[Extends(typeof(INPCTester))]//在我的示例中注释掉并且[ImplementsINPC]//在我的示例中注释掉具有完全相同的效果这是您要在何处定义特定mixin应用于特定类的位置的问题。示例2:覆盖Equals和GetHashCode.Equals(Tother){if(other==null)returnfalse;如果(Target.GetType()!=other.GetType())返回false;for(inti=0;i)this).Equals(otherasT);}[OverrideTarget]publicnewintGetHashCode(){inti=0;foreach(FieldInfofinm_TargetFields)i^=f.GetValue(Target).GetHashCode();返回我;}}publicclassEquatableByValuesAttribute:UsesAttribute{publicEquatableByValuesAttribute():base(typeof(EquatableByValuesMixin)){}}这个例子是我对混音动手实验的实现。您可以在那里找到更多信息。以上是C#学习教程:有没有办法用反射“覆盖”方法?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢