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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:55:00+00:00 2026-05-11T23:55:00+00:00

Picture the following situation. I have an XML document as follows, <Form> <Control Type=Text

  • 0

Picture the following situation. I have an XML document as follows,

<Form>
    <Control Type="Text" Name="FirstName" />
    <Control Type="DateTime" Name="DateOfBirth" />
    <Control Type="Text" Name="PlaceOfBirth" />
</Form>

I have an abstract class called Control with a single abstract method called Process which takes a single parameter of HttpRequest. I also have two other classes that are derived from Control called TextControl and DateTimeControl. Both Text and DateTime override the Process method to provide their own implementations.

I also have a Form class which has a Process method taking a single parameter of type HttpRequest, and a constructor which takes a single parameter of type XmlDocument.

A new instance of Form is created and the above Xml is passed in via the XmlDocument parameter (how we get from the string to an XmlDocument is irrelevant). I then call the Process method on the instance of Form I just created and pass in a parameter of type HttpRequest as expected.

All good so far. Now onto the question.

To make the processing of Controls extensible I would like to be able to map Classes to control types.

eg.

Form.RegisterControl("Text", Text)
Form.RegisterControl("DateTime", DateTimeControl)

Within the Process method of Form I would like to itterate over each Control node in the document (how to do this is again irrelevant) and instantiate an instance of the class which matches its type based on the Classes registered by our RegisterControl method. I would be able at this stage to specify that they are derived from Control but could not explicitly specify their type. Because they are both derived from Control I want to call the Process method which I know will be implemented.

Is this even possible? If so how would I go about it?

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

    (This answer is in some ways two different answers, depending on the meaning of your question. Hopefully one part of it is helpful, anyway 🙂


    Probably the best way is to pass in an argument which is a function which can be used to create the new control at the right time. If you’re using C# 3, this is as simple as:

    Form.RegisterControl("Text", () => new Text())
    

    Alternatively, you could make it a generic method with two constraints: one for being a control, and one for having a parameterless constructor.

    public void RegisterControl<T>(string name) where T : Control, new()
    

    then call it with:

    Form.RegisterControl<Text>("Text");
    Form.RegisterControl<DateTimeControl>("DateTime");
    

    RegisterControl would have to remember typeof(T) in whatever storage it’s using, but at least then it could be reasonably sure that Activator.CreateInstance(Type) would work later on – and you’d have compile-time checking.

    Personally I like the flexibility of the first form though – if you’re passing in a delegate, that can choose to use a singleton, or perhaps some internal or even private constructor (depending on where it’s being called from). You could also use a delegate which took the appropriate data, too:

    Form.RegisterControl("Text", data => new Text(data));
    

    You can’t express that sort of constructor with a generic constraint, and it would be relatively hard to invoke later anyway.


    EDIT: It’s possible that both myself and Mehrdad have misinterpreted the question. Do you actually have different overloads of RegisterControl based on the control type? If so, then the only ways of directly calling the right overload at execution time are to either use reflection or use dynamic typing in C# 4.

    Another alternative would be to use double dispatch – put a method in the control itself which knows how to register itself with a form. This would be specified in the interface or base class, but overridden in the concrete subclasses. So your code which is currently:

    Form.RegisterControl("Text", control);
    

    would become:

    control.RegisterWith(Form, "Text");
    

    That could then call the correct overload with no issues.

    Basically you need to remember that overload resolution is performed at compile time, but override resolution is performed at execution time – so if you want to make something dynamic, try to approach it with polymorphism.

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

Sidebar

Related Questions

I have the following situation, presented in the picture. Grey div is the parent
What does it mean when you have something as the following picture? alt text
Consider the following picture representing the WCF channel stack: alt text http://i.msdn.microsoft.com/ee672186.image001(en-us).jpg I'm implementing
I have a graph created with MS Chart like the following picture. As you
i have following code for moving 4 picture around circular way,but it can rotate
In Excel I have attached a picture to a cell using the following Sub
I tried to read the source code in Wordpress at following picture. I have
I have the following Picture: And you guessed it: I want to extract all
I have the following picture : And I would like to make a legend
I have a data structure similar to the following picture. Table A has_many Table

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.