Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:43:11+00:00 2026-05-31T05:43:11+00:00

Without inherit but only with reflection is it possible to dynamically change the code

  • 0

Without inherit but only with reflection is it possible to dynamically change the code of a method in C#?

something like :

nameSpaceA.Foo.method1 = aDelegate;

I cannot change/edit The Foo Class.

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);

In System.Xml.Xsl.Runtime.XslConvert.cs

to turn :

if (!item.IsNode)
    throw new XslTransformException(Res.XPath_NodeSetExpected, string.Empty); 

into :

if (!item.IsNode)
    throw new XslTransformException(Res.XPath_NodeSetExpected, item.value); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:43:12+00:00Added an answer on May 31, 2026 at 5:43 am

    The first part of this answer is wrong, I’m only leaving it so that the evolution in the comments makes sense. Please see the EDIT(s).

    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!

    See TypeBuilder.DefineMethodOverride

    EDIT:
    Writing this answer, I just remembered that re-mix allows you to do this too. It’s way harder though.

    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.

    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;
        }
    }
    

    And the 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
    

    Please note that:

    [assembly: Mix(typeof(INPCTester), typeof(INotifyPropertyChangedMixin))]
    

    and

    [Extends(typeof(INPCTester))] //commented out in my example
    

    and

    [ImplementsINPC] //commented out in my example
    

    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.

    Example 2: overriding Equals and 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<>))
            {
    
            }
        }
    

    That example is my implementation of the hands-on lab given with re-mix. You can find more information there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a Silverlight custom control using C# only, without any
Without getting a degree in information retrieval, I'd like to know if there exists
Without spending a long time reviewing the boost source code, could someone give me
Without having the full module path of a Django model, is it possible to
I have a custom WinForms control (inherits from control, i.e. without user interface jsut
Without the use of any external library, what is the simplest way to fetch
Without calculating them , I mean?
Without using Javascript, is there a way to make a CSS property toggle on
Without using a WebBrowser control, how could I execute a JavaScript function, that is
Without routing, HttpContext.Current.Session is there so I know that the StateServer is working. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.