C'è un modo per "sovrascrivere" un metodo con la riflessione?
-
13-12-2019 - |
Domanda
senza ereditare ma solo con la riflessione è possibile modificare dinamicamente il codice di un metodo in C #?
Qualcosa come:
nameSpaceA.Foo.method1 = aDelegate;
.
Non posso cambiare / modificare la classe Foo.
namespace nameSpaceA
{
class Foo
{
private void method1()
{
// ... some Code
}
}
}
.
Il mio obiettivo finale è cambiare dinamico il codice di:
public static IList<XPathNavigator> EnsureNodeSet(IList<XPathItem> listItems);
.
in system.xml.xsl.runtime.xslconvert.cs
Turn:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, string.Empty);
.
IN:
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, item.value);
. Soluzione
La prima parte di questa risposta è sbagliata, lo sto solo lasciando in modo che l'evoluzione nei commenti abbia senso. Si prega di consultare la modifica (s).
Non stai cercando la riflessione, ma le emissioni (che è il contrario).
In particolare, c'è un metodo che fa proprio quello che vuoi, fortunato a te!
Vedi typebuilder.definemethodoverride
Modifica:
Scrivere questa risposta, ho appena ricordato che Re-mix ti permette di farlo anche di farlo. È più difficile però.
Re-mix è un framework che "simula" mixins in C #. Nel suo aspetto fondamentale, puoi pensarci come interfacce con implementazioni predefinite. Se vai oltre, diventa molto più di quello.
Modifica 2: Ecco un esempio di utilizzo per il riorcamento (implementazione inotifypropertychanged su una classe che non lo supporta e non ha idea di 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;
}
}
.
E il test:
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
.
Si prega di notare che:
[assembly: Mix(typeof(INPCTester), typeof(INotifyPropertyChangedMixin))]
.
e
[Extends(typeof(INPCTester))] //commented out in my example
.
e
[ImplementsINPC] //commented out in my example
.
ha lo stesso effetto identico. Si tratta di dove desideri definire che un particolare mixin viene applicato a una particolare classe.
Esempio 2: Scontoride uguali e GetsHashCode
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<>))
{
}
}
.
Quell'esempio è la mia implementazione del laboratorio pratico dato con rimorso. Puoi trovare ulteriori informazioni lì.