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

  • Home
  • SEARCH
  • 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 7071941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:44:53+00:00 2026-05-28T05:44:53+00:00

I’m working on legacy code that looks very similar to this: public class BaseParser

  • 0

I’m working on legacy code that looks very similar to this:

public class BaseParser
{
    public BaseParser(Stream stream) 
    {
        // Do something with stream
    }
}

public class JsonParser : BaseParser
{
    public JsonParser(Stream stream) 
        : base(stream)
    {
        // Do something
    }
}

public class XmlParser : BaseParser
{
    public XmlParser(Stream stream, string someOtherParam) 
        : base(stream)
    {
        // Do something
    }
}

We have an application that parses incoming files using a particular parser. To determine which type we need to instantiate, there’s a large IF/ELSE block in a factory method:

public static BaseParser Create(string type)
{
    if (type == "xml")
        return new XmlParser(new FileStream("path", FileMode.Open), "test");

    if (type == "json")
        return new JsonParser(new FileStream("path", FileMode.Open));

    // more...

    return null;
}

Since there are a large number of parsers that reside in a single assembly (lets call it Demo.dll), I’d like to split each of these parsers into their own assembly. The “core” classes such as the BaseParser will remain in Demo.dll, but the other parsers and any dependencies will live in Demo.Json.dll, Demo.Xml.dll, etc….

The “core” library will load these assemblies at runtime.

string[] paths = Directory.GetFiles(Environment.CurrentDirectory, "Demo.*.dll");

foreach (string path in paths)
    Assembly.LoadFrom(path);

List<BaseParser> parsers = AppDomain.CurrentDomain
    .GetAssemblies()
    .SelectMany(x => x.GetTypes())
    .Where(x => x.IsSubclassOf(typeof(BaseParser)))
    .Select(x => (BaseParser) Activator.CreateInstance(x))
    .ToList();

The problem starts here. The code above will not work since each of the legacy parsers do not have a parameterless constructor. Also, I can’t think of a good way to replace the IF/ELSE block and let each parser determine which file it can handle. I was thinking about possibly adding a virtual method on the base parser:

public virtual bool ShouldHandle(string type)
{
    return false;
}

… and each derived parser will override this virtual method. However, there’s two problems:

  1. No parameterless constructor and each class’s constructor can have a different number of parameters. This isn’t something I can change since these legacy classes are used everywhere.

  2. I have to instantiate the class before I can call the ShouldHandle method. This poses a problem for classes that read from the stream in the constructor.

Is there an alternative route I can take to split these parsers into their own assemblies?

EDIT:

This is a .NET 3.5 application. No MEF unfortunately.

  • 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-28T05:44:54+00:00Added an answer on May 28, 2026 at 5:44 am

    I’ve found that using an abstract factory class can help in these situations.

    Each assembly has a factory that is specific to creating the classes that reside in that assembly.

    In effect you then have a factory create a concrete factory in order to create your concrete parser.

    By doing it this way you can also put logic in place whereby you check for the presence of an assembly before attempting to create the concrete factory associated with it.

    I know that it doesn’t get around the if else structure (although that can be replaced with a switch, passing in a parameter for each class, but it does allow you to break it up and make it easier to handle and support.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.