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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:50:34+00:00 2026-05-23T01:50:34+00:00

I am trying attempting to refactor the following code: public static void SaveSplitbar(RadSplitBar splitBar)

  • 0

I am trying attempting to refactor the following code:

public static void SaveSplitbar(RadSplitBar splitBar)
{
    Logger.InfoFormat("Saving splitBar {0}", splitBar.ID);
    RadSplitBarSetting splitbarSettings = splitBar.GetSettings();
    SerializableDictionary<string, RadSplitBarSetting> splitBarStates = GetStates<SerializableDictionary<string, RadSplitBarSetting>>();

    bool splitbarIsKnown = splitBarStates.ContainsKey(splitbarSettings.ID);

    if (splitbarIsKnown)
    {
        Logger.Debug("SplitBar is known. Overwriting data.");
        splitBarStates[splitbarSettings.ID] = splitbarSettings;
    }
    else
    {
        Logger.Debug("SplitBar is unknown. Saving data.");
        splitBarStates.Add(splitbarSettings.ID, splitbarSettings);
    }

    RadControlManager.SetStates<SerializableDictionary<string, RadSplitBarSetting>>(splitBarStates);
}

The logic of the code is reused in saving various other objects. I am trying to make this code more generic so that I will be able to have one Save() method rather than SaveX(), SaveY(), and SaveZ() all performing the same work on different objects.

I have a bit of a snag, however. While all of the objects being passed into Save have the “GetSettings()” method, many of them have this method defined in an extension method:

//RadControlExtensions.cs Extension Methods
public static RadSplitBarSetting GetSettings(this RadSplitBar splitbar)
{
    RadSplitBarSetting radSplitbarSetting = new RadSplitBarSetting(splitbar.ID, splitbar.Parent.ID, splitbar.CollapseMode, splitbar.AdjacentPanesNames);
    return radSplitbarSetting;
}

As such, when I tried to refactor SaveSplitbar, I realize that I would have to be switching on the type of my parameter. When I started going down this route I thought, “Well, this is stupid, if I have to determine the type of the object I should just write Save multiple times as extension methods for each object.”

While this is an “OK” solution, I am unhappy with it. The logic behind saving each type of control is identical and I would prefer not having copy/pasted logic in my extension class.

My next thought was, “Well, if any parameter passed into Save is guaranteed to have a GetSettings method — maybe there is a way to have an IRadControlExtensions interface which exposes this method. I was unable to determine any such way as each GetSettings method has a different signature – does not seem possible to announce it just once in the interface.

So that’s where I am at. How should I be approaching this problem?

EDIT: Another Save method

public static void SavePane(CormantRadPane pane)
{
    Logger.InfoFormat("Saving pane {0}", pane.ID);
    RadPaneSetting paneSettings = pane.GetSettings();
    SerializableDictionary<string, RadPaneSetting> paneStates = GetStates<SerializableDictionary<string, RadPaneSetting>>();

    bool paneIsKnown = paneStates.ContainsKey(paneSettings.ID);

    if (paneIsKnown)
    {
        Logger.Debug("Pane is known. Overwriting data.");
        paneStates[paneSettings.ID] = paneSettings;
    }
    else
    {
        Logger.Debug("Pane is unknown. Saving data.");
        paneStates.Add(paneSettings.ID, paneSettings);
    }

    RadControlManager.SetStates<SerializableDictionary<string, RadPaneSetting>>(paneStates);
}

EDIT2: Perhaps a better question is, “When is it time to stop extending a class and time to start creating a new class which inherits from the class currently being extended?”

  • 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-23T01:50:35+00:00Added an answer on May 23, 2026 at 1:50 am

    Firstly i would have each type implement a marker interface with the required properties and methods for this operation, something like

     public class CormantRadPane : IWidget
     {
      ...
     } 
    
     public class RadSplitBar : IWidget
     {
      ...
     } 
    

    where IWidget looking something like

    public interface IWidget
    {
        int ID;
    }
    

    and each Settings class with something like ISetting

    But then your problem comes in with the extention methods, which one should be resolved for the correct concreate IWidget. To get that type you could simply use.

      Type contreteWidgetType = widget.GetType(); 
    

    Remember extention methods is actually compiler trickery. The compiler automatically emits ExtensionAttribute on extension methods when using the this keyword.
    Taken from some funky code from Jon Skeet here you could scan the assembly for the correct extention method, and invoke it.

    Jon said..

    Look for classes decorated with ExtensionAttribute, and then methods within that class which are also decorated with ExtensionAttribute. Then check the type of the first parameter to see if it matches the type you’re interested in.

        static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly,
            Type extendedType)
        {
            var query = from type in assembly.GetTypes()
                        where type.IsSealed && !type.IsGenericType && !type.IsNested
                        from method in type.GetMethods(BindingFlags.Static
                            | BindingFlags.Public | BindingFlags.NonPublic)
                        where method.IsDefined(typeof(ExtensionAttribute), false)
                        where method.GetParameters()[0].ParameterType == extendedType
                        select method;
            return query;
        }
    

    Hope this helps 🙂

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

Sidebar

Related Questions

No related questions found

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.