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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:49:35+00:00 2026-05-25T21:49:35+00:00

I have a object that is created through deserialization of some XML. I used

  • 0

I have a object that is created through deserialization of some XML. I used Visual Studio’s tool to generate the XSD from the vendor mockup XML. Then using the XSD tool I got classes from them. I set the XSD tool so that it would make the generated classes be INotifyPropertyChanged.

Now I’m trying to display this object in a “tab” on my WPF application. I want a “dirty” indicator whenever someone makes a change. The issue is that this object, being generated classes from generated XSD is not the prettiest structure. The display for this object is does not mimic its data structure. I thought of creating display objects (not using MMVM ATM) and using those to bind my changes to and then persist those changes to the object as a data object. I would essentially just throw out my current display to do this since I’m now just adding a check for if there were edits.

My thought is to reflect across the object and set the PropertyChanged event for every property I come across (walking through the graph of objects and properties). My reflection fu is failing me and I’m also probably making some short-sighted mistakes.

Here is the code I’ve written thus far:

void SetupPropertyChanged(INotifyPropertyChanged component)
    {
        component.PropertyChanged += CAMConfig_PropertyChanged;

        Type componentType = component.GetType();

        foreach (PropertyInfo info in componentType.GetProperties())
        {
            Type[] types =
                info.PropertyType.FindInterfaces((a, b) => { return a.ToString() == b.ToString(); }, typeof(INotifyPropertyChanged));

            bool isINotify = types.Contains(typeof(INotifyPropertyChanged));


            if (isINotify)
                this.SetupPropertyChanged((INotifyPropertyChanged)info.GetValue(component, new object[] { }));
        }
    }

I think I’m running into Observable collection property type issues as its throwing an exception when I’m traversing my object. It also just hit me that I don’t know if this object structure would have circular references.

Can someone help me work out this code so that I can traverse the object graph. Right now I’m not too concerned about the possibility of circular references, but if a solution presents itself that prevents that situation it would be very, very helpful!

Based on Karel’s answer I’ve created this “helper” class:

    public static class NotifyPropertyChangedHelper
{
    public delegate void ChangeOccuredHandler(object sender);

    public static void SetupPropertyChanged(INotifyPropertyChanged component, ChangeOccuredHandler changedHandler)
    {
        SetupPropertyChanged(new List<object>(), component, changedHandler);
    }

    static void SetupPropertyChanged(IList<object> closed, INotifyPropertyChanged component, ChangeOccuredHandler changedHandler)
    {
        if (closed.Contains(component)) return; // event was already registered

        closed.Add(component); //adds the property that is to be processed

        //sets the property changed event if the property isn't a collection
        if (!(component is INotifyCollectionChanged))
            component.PropertyChanged += (sender, e) =>
                {
                    changedHandler(sender);
                };

        /*
         * If the component is an enumerable there are two steps. First check to see if it supports the INotifyCollectionChanged event.
         * If it supports it add and handler on to this object to support notification.  Next iterate through the collection of objects
         * to add hook up their PropertyChangedEvent.
         * 
         * If the component isn't a collection then iterate through its properties and attach the changed handler to the properties.
         */
        if (component is IEnumerable<object>)
        {
            if (component is INotifyCollectionChanged)
            {
                //((INotifyCollectionChanged)component).CollectionChanged += collectionHandler;
                ((INotifyCollectionChanged)component).CollectionChanged += (sender, e) =>
                    {
                        changedHandler(sender);
                    };
            }

            foreach (object obj in component as IEnumerable<object>)
            {
                if (obj is INotifyPropertyChanged)
                    SetupPropertyChanged(closed, (INotifyPropertyChanged)obj, changedHandler);
            }
        }
        else
        {
            foreach (PropertyInfo info in component.GetType().GetProperties())
            {
                var propertyValue = info.GetValue(component, new object[] { });
                var inpc = propertyValue as INotifyPropertyChanged;
                if (inpc == null) continue;
                SetupPropertyChanged(closed, inpc, changedHandler);
            }
        }
    }
}
  • 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-25T21:49:36+00:00Added an answer on May 25, 2026 at 9:49 pm

    Keep a list of properties (components) for which you’ve already registered the event and make your function recursive. And you can cast component to INotifyPropertyChanged directly.

    void SetupPropertyChanged(IList<object> closed, INotifyPropertyChanged component)  
    {
      if(closed.Contains(component)) return; // event was already registered
    
      closed.Add(component);
    
      component.PropertyChanged += CAMConfig_PropertyChanged;            
      foreach (PropertyInfo info in componentType.GetProperties())          
      {
        var propertyValue = info.GetValue(component, new object[] { });
        var inpc = propertyValue as INotifyPropertyChanged;
        if(inpc == null) continue;
        this.SetupPropertyChanged(closed, inpc);
      }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a object that I'd like to have accessible from wherever the request-object
I have a stdClass object created from json_decode that won't return the right number
I have an object that hasn't been created yet (so it has no ID),
I have an ExpenseType object that I have created with the following migration: class
i have created an IE band object (toolbar) that is working well. however, when
I have an NSArray of (Product) objects that are created by parsing an XML
I have created an IDbContext object that is provided to my IRepository implementations. The
I have created a BST full of WordInfo objects that have a vector to
I have a Rect object that I'd like to create and set its properties
I have the following code that creates a serverside object of the xmlhttp class.

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.