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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:54:03+00:00 2026-05-12T12:54:03+00:00

I’m running into an unusual problem in my unit tests. The class I’m testing

  • 0

I’m running into an unusual problem in my unit tests. The class I’m testing creates a dependency property dynamically at runtime and the type of that dependency property can vary depending on the circumstances. While writing my unit tests, I need to create the dependency property with different types and that leads to errors because you can’t redefine an existing dependency property.

So is there any way to either un-register a dependency property or to change the type of an existing dependency property?

Thanks!


OverrideMetadata() only lets you change a very few things like default value so it isn’t helpful. The AppDomain approach is a good idea and might work but seems more complicated than I really wanted to delve into for the sake of unit testing.

I never did find a way to unregister a dependency property so I punted and carefully reorganized my unit tests to avoid the issue. I’m getting a bit less test coverage, but since this problem would never occur in a real application and only during unit testing I can live with it.

Thanks for the help!

  • 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-12T12:54:03+00:00Added an answer on May 12, 2026 at 12:54 pm

    I had similar issue just yesterday when trying to test my own DependencyProperty creating class. I came across this question, and noticed there was no real solution to unregister dependency properties. So I did some digging using Red Gate .NET Reflector to see what I could come up with.

    Looking at the DependencyProperty.Register overloads, they all seemed to point to DependencyProperty.RegisterCommon. That method has two portions:

    First to check if the property is already registered

    FromNameKey key = new FromNameKey(name, ownerType);
    lock (Synchronized)
    {
      if (PropertyFromName.Contains(key))
      {
        throw new ArgumentException(SR.Get("PropertyAlreadyRegistered", 
          new object[] { name, ownerType.Name }));
      }
    }
    

    Second, Registering the DependencyProperty

    DependencyProperty dp = 
      new DependencyProperty(name, propertyType, ownerType, 
        defaultMetadata, validateValueCallback);
    
    defaultMetadata.Seal(dp, null);
    //...Yada yada...
    lock (Synchronized)
    {
      PropertyFromName[key] = dp;
    }
    

    Both pieces center around DependencyProperty.PropertyFromName, a HashTable. I also noticed the DependencyProperty.RegisteredPropertyList, an ItemStructList<DependencyProperty> but have not seen where it is used. However, for safety, I figured I’d try to remove from that as well if possible.

    So I wound up with the following code that allowed me to “unregister” a dependency property.

    private void RemoveDependency(DependencyProperty prop)
    {
      var registeredPropertyField = typeof(DependencyProperty).
        GetField("RegisteredPropertyList", BindingFlags.NonPublic | BindingFlags.Static);
      object list = registeredPropertyField.GetValue(null);
      var genericMeth = list.GetType().GetMethod("Remove");
      try
      {
        genericMeth.Invoke(list, new[] { prop });
      }
      catch (TargetInvocationException)
      {
        Console.WriteLine("Does not exist in list");
      }
    
      var propertyFromNameField = typeof(DependencyProperty).
        GetField("PropertyFromName", BindingFlags.NonPublic | BindingFlags.Static);
      var propertyFromName = (Hashtable)propertyFromNameField.GetValue(null);
    
      object keyToRemove = null;
      foreach (DictionaryEntry item in propertyFromName)
      {
        if (item.Value == prop)
          keyToRemove = item.Key;
      }
      if (keyToRemove != null)
      propertyFromName.Remove(keyToRemove);
    }
    

    It worked well enough for me to run my tests without getting an “AlreadyRegistered” exception. However, I strongly recommend that you do not use this in any sort of production code. There is likely a reason that MSFT chose not to have a formal way to unregister a dependency property, and attempting to go against it is just asking for trouble.

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

Sidebar

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With erc come several modules. Customise erc-modules so it contains… May 12, 2026 at 3:33 pm
  • Editorial Team
    Editorial Team added an answer In my tests, changing the ItemTemplate (TaskListTemplate) did not have… May 12, 2026 at 3:33 pm
  • Editorial Team
    Editorial Team added an answer For long template errors, related to stl you can use… May 12, 2026 at 3:33 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.