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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:02:24+00:00 2026-06-05T06:02:24+00:00

Writing a bit of code that deals with a Response and a Request. Both

  • 0

Writing a bit of code that deals with a Response and a Request. Both can be in the form of XML, and both can be in the form of a C# Object created through a transform and serialization. (this is .NET 2.0)

Response and Request are base implementations of larger message types. Right now I have GetEligibility and FindCandidates.

Example of the Model.MessageModel classes used below:

public partial class GetEligibilityResponseMessage : ResponseMessage

public partial class ResponseMessage : Message

Because I won’t want to duplicate my mapping functionality I’ve decided to use generics to simplify the process, and it’s working out great:

Base Class Code

    public virtual Model.MessageModel.Message MapToModel<T>(XmlDocument xml)
    {
        V3Mapper mapper = new V3Mapper();
        Model.MessageModel.Message message = mapper.MapToDomainModel<T>(xml, Environment) as Model.MessageModel.Message;
        return message;
    }

    public virtual XmlDocument MapToXml<T>(Model.MessageModel.Message message)
    {
        V3Mapper mapper = new V3Mapper();
        XmlDocument xml= mapper.MapToV3Message<T>(message, Environment);
        return xml;
    }

When my code is first called, it has an XML document. I know this document will be mapped as a request, and so I call a virtual method that is overriden (and I think it’s ugly). The reason to keep the mapping code in the base is to not duplicate code, yet I find I am doing the exact thing I want to avoid by the following:

GetEligibility : BaseClass

   public override Model.MessageModel.Message MapToModel<T>(XmlDocument xml)
    {   
        if(typeof(T).IsAssignableFrom(typeof(GetEligibilityResponseMessage)))
        {
            return base.MapToModel<GetEligibilityResponseMessage>(xml);
        }
        else if (typeof(T).IsAssignableFrom(typeof(GetEligibilityRequestMessage))) 
        {
            return base.MapToModel<GetEligibilityRequestMessage>(xml);
        }
        return null;//because this is a quick code snippet
    }

Is there a more elegant way of doing this? I always know if I’m working with a Response or Request. I want to leave the functionality open so it’s not too tightly-coupled, but at the same time have it functional and fast.

This will be implemented by a number of different message types, and I really hate copy/paste style of coding, so an elegant solution would be great, but I’m not sure if there is one. (.NET 2.0)

  • 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-06-05T06:02:26+00:00Added an answer on June 5, 2026 at 6:02 am

    You can use the MethodInfo.MakeGenericMethod Method to avoid having to check types before calling your generic method. Below is a quick usage example:

    class Program
    {
        public static void Generic<T>(T toDisplay)
        {
            Console.WriteLine("\r\nHere it is: {0}", toDisplay);
        }
    
        static void Main(string[] args)
        {
            MethodInfo mi = typeof(Program).GetMethod("Generic");
            MethodInfo miConstructed = mi.MakeGenericMethod(typeof(DateTime));
    
            DateTime now = DateTime.Now;
            miConstructed.Invoke(null, new object[] { now });
        }
    }
    

    Notice that I used typeof(DateTime), but in your case you can replace that by typeof(T) to achieve the desired loosely-coupled solution.

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

Sidebar

Related Questions

I'm having issues with a bit of code that I am writing in C#.
I recently came across a bit of not-well-tested legacy code for writing data that's
I'm writing a bit of code that I want to populate a TreeView, which
I am writing a bit of code that appends my site nav with and
I'm writing a bit of code that will open a MessageBox on a separate
So I'm writing a bit of code that needs to raise a function's return
I'm writing a bit of code to run a shell script using process that
I am writing bit of code that relies on XslTransformation of one submodule. Since
I am writing a bit of actionscript that loads an xml file and displays
I'm writing some code that uses a Tree (a regular tree that can have

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.