有没有办法“覆盖"?一种带反射的方法? [英] Is there a way to "override" a method with reflection?
问题描述
不继承而只用反射是否可以在C#中动态更改方法的代码?
Without inherit but only with reflection is it possible to dynamically change the code of a method in C#?
类似:
nameSpaceA.Foo.method1 = aDelegate;
我无法更改/编辑 The Foo 类.
namespace nameSpaceA
{
class Foo
{
private void method1()
{
// ... some Code
}
}
}
我的最终目标是动态更改以下代码:
My final objective is to change dynamicaly the code of :
public static IList<XPathNavigator> EnsureNodeSet(IList<XPathItem> listItems);
在 System.Xml.Xsl.Runtime.XslConvert.cs 中
In System.Xml.Xsl.Runtime.XslConvert.cs
转向:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, string.Empty);
进入:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, item.value);
推荐答案
这个答案的第一部分是错误的,我只留下它以便评论中的演变有意义.请参阅编辑.
您不是在寻找反射,而是在寻找发射(正好相反).
You're not looking for reflection, but emission (which is the other way around).
特别是,有一种方法可以满足您的需求,您很幸运!
In particular, there's a method that does just what you want, lucky you!
参见 TypeBuilder.DefineMethodOverride
写这个答案时,我只记得 re-mix 也允许您这样做.不过比较难.
Writing this answer, I just remembered that re-mix allows you to do this too. It's way harder though.
Re-mix 是一个在 C# 中模拟"mixin 的框架.在其基本方面,您可以将其视为具有默认实现的接口.如果你走得更远,它会变得更多.
Re-mix is a framework that "simulates" mixins in C#. In its basic aspect, you can think of it as interfaces with default implementations. If you go further, it becomes much more than that.
编辑 2: 这是一个用于重新混合的示例(在不支持它的类上实现 INotifyPropertyChanged,并且不知道 mixins).
EDIT 2: Here is an example of use for re-mix (implementing INotifyPropertyChanged on a class that doesn't support it, and has no idea of mixins).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Remotion.Mixins;
using System.ComponentModel;
using MixinTest;
[assembly: Mix(typeof(INPCTester), typeof(INotifyPropertyChangedMixin))]
namespace MixinTest
{
//[Remotion.Mixins.CompleteInterface(typeof(INPCTester))]
public interface ICustomINPC : INotifyPropertyChanged
{
void RaisePropertyChanged(string prop);
}
//[Extends(typeof(INPCTester))]
public class INotifyPropertyChangedMixin : Mixin<object>, ICustomINPC
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string prop)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
}
public class ImplementsINPCAttribute : UsesAttribute
{
public ImplementsINPCAttribute()
: base(typeof(INotifyPropertyChangedMixin))
{
}
}
//[ImplementsINPC]
public class INPCTester
{
private string m_Name;
public string Name
{
get { return m_Name; }
set
{
if (m_Name != value)
{
m_Name = value;
((ICustomINPC)this).RaisePropertyChanged("Name");
}
}
}
}
public class INPCTestWithoutMixin : ICustomINPC
{
private string m_Name;
public string Name
{
get { return m_Name; }
set
{
if (m_Name != value)
{
m_Name = value;
this.RaisePropertyChanged("Name");
}
}
}
public void RaisePropertyChanged(string prop)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
和测试:
static void INPCImplementation()
{
Console.WriteLine("INPC implementation and usage");
var inpc = ObjectFactory.Create<INPCTester>(ParamList.Empty);
Console.WriteLine("The resulting object is castable as INPC: " + (inpc is INotifyPropertyChanged));
((INotifyPropertyChanged)inpc).PropertyChanged += inpc_PropertyChanged;
inpc.Name = "New name!";
((INotifyPropertyChanged)inpc).PropertyChanged -= inpc_PropertyChanged;
Console.WriteLine();
}
static void inpc_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("Hello, world! Property's name: " + e.PropertyName);
}
//OUTPUT:
//INPC implementation and usage
//The resulting object is castable as INPC: True
//Hello, world! Property's name: Name
请注意:
[assembly: Mix(typeof(INPCTester), typeof(INotifyPropertyChangedMixin))]
和
[Extends(typeof(INPCTester))] //commented out in my example
和
[ImplementsINPC] //commented out in my example
具有完全相同的效果.您希望在何处定义将特定 mixin 应用于特定类的问题.
Have the exact same effect. It is a matter of where you wish to define that a particular mixin is applied to a particular class.
示例 2:覆盖 Equals 和 GetHashCode
public class EquatableByValuesMixin<[BindToTargetType]T> : Mixin<T>, IEquatable<T> where T : class
{
private static readonly FieldInfo[] m_TargetFields = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
bool IEquatable<T>.Equals(T other)
{
if (other == null)
return false;
if (Target.GetType() != other.GetType())
return false;
for (int i = 0; i < m_TargetFields.Length; i++)
{
object thisFieldValue = m_TargetFields[i].GetValue(Target);
object otherFieldValue = m_TargetFields[i].GetValue(other);
if (!Equals(thisFieldValue, otherFieldValue))
return false;
}
return true;
}
[OverrideTarget]
public new bool Equals(object other)
{
return ((IEquatable<T>)this).Equals(other as T);
}
[OverrideTarget]
public new int GetHashCode()
{
int i = 0;
foreach (FieldInfo f in m_TargetFields)
i ^= f.GetValue(Target).GetHashCode();
return i;
}
}
public class EquatableByValuesAttribute : UsesAttribute
{
public EquatableByValuesAttribute()
: base(typeof(EquatableByValuesMixin<>))
{
}
}
那个例子是我对 re-mix 给出的动手实验的实现.您可以在那里找到更多信息.
That example is my implementation of the hands-on lab given with re-mix. You can find more information there.
这篇关于有没有办法“覆盖"?一种带反射的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!