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 7704973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:47:42+00:00 2026-05-31T23:47:42+00:00

I’m having difficulty trying to sum up my question in form of a google

  • 0

I’m having difficulty trying to sum up my question in form of a google query (maybe my foo is off):

I’m attempting to give an interface or class some functionality by not editing the type definition itself, but rather by dressing up the object with an interface. The simple example I’m attempting is to attach a Attribute, called [Notify] which has a property change event defined to, ideally, give the interface/class use of the event. I’ve been unsuccessful with this so I’m opening up to any device possible (if any) to achieve the equivalent:

[Notify]
public interface IPerson
{
    string Name { get; set; }
}

    [AttributeUsage(AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
    public class Notify: Attribute
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

    }

Using an IPerson:

IPerson person = PersonFactory.create();
person.PropertyChanged += SomeDelegateHere // the center peice

Of course if you copied this code, person won’t have PropertyChanged. With or without attributes, is there a way to accomplish given the IPerson more functionality without changing the definition (i.e not adding an interface or base class). I get that attributes are changing too, but I’m comfortable with the meta data approach.

Thanks in advance for any light that can be shed.

  • 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-31T23:47:42+00:00Added an answer on May 31, 2026 at 11:47 pm

    What you are looking is hooking on such events as method invocations and attaching some actions to that events. This is not default feature of C#, but it can be done via such AOP framework as PostSharp. PostSharp adds this functionality by changing code during build time (it does not use Reflection).

    Here is what you need to implement with PostSharp:

    [Serializable]
    [IntroduceInterface(typeof(INotifyPropertyChanged), OverrideAction = InterfaceOverrideAction.Ignore)]
    [MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.Strict )]
    public class NotifyAttribute : InstanceLevelAspect, INotifyPropertyChanged
    {        
        [ImportMember("OnPropertyChanged", IsRequired = false)] 
        public Action<string> OnPropertyChangedMethod;    
    
        [IntroduceMember(Visibility = Visibility.Family, IsVirtual = true, OverrideAction = MemberOverrideAction.Ignore)]
        public void OnPropertyChanged( string propertyName )
        {
            if (PropertyChanged != null )            
                PropertyChanged(Instance, new PropertyChangedEventArgs(propertyName));            
        }
    
        [IntroduceMember(OverrideAction = MemberOverrideAction.Ignore)]
        public event PropertyChangedEventHandler PropertyChanged;
    
        [OnLocationSetValueAdvice]
        [MulticastPointcut(Targets = MulticastTargets.Property,  Attributes = MulticastAttributes.Instance)]
        public void OnPropertySet( LocationInterceptionArgs args )
        {            
            if (args.Value == args.GetCurrentValue()) return;
    
            args.ProceedSetValue();
            this.OnPropertyChangedMethod.Invoke( args.Location.Name );            
        }
    }
    

    Implementation details you can find here.

    Usage is very simple:

    [Notify]
    public class Person
    {
        public string Name { get; set; }
    }
    

    Keep in mind, that actual code will be injected into assembly during compilation. Attributes cannot add any functionality. They are just meta data. In this case meta data used by PostSharp to generate code and inject it into assembly.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.