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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:32:36+00:00 2026-05-11T02:32:36+00:00

I am learning to use polymorphism in C#, but cannot figure out this one.

  • 0

I am learning to use polymorphism in C#, but cannot figure out this one. I’m trying to write a class that allows me to get a filtered list of files from a repository.

The repository could be a file system folder or the embedded resources in an arbitrary already-loaded assembly (but it is not the one currently executing).

Any suggestions?

  • 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. 2026-05-11T02:32:36+00:00Added an answer on May 11, 2026 at 2:32 am

    You could define pair of interfaces like this:

    public interface IReadableFile {     Stream OpenRead(); }  public interface IRepository {     IEnumerable<IReadableFile> Search(string pattern); } 

    And have two different implementations of them:

    public class FolderFile : IReadableFile {     readonly private string _name;      public FolderFile(string name)     {         _name = name;     }      #region IFile Members      public Stream OpenRead()     {         return File.OpenRead(_name);     }      #endregion }  public class FolderRepository : IRepository {     readonly private string _directory;      public FolderRepository(string directory)     {         _directory = directory;     }      #region IRepository Members      public IEnumerable<IReadableFile> Search(string pattern)     {         return Array.ConvertAll(Directory.GetFiles(_directory, pattern), name => new FolderFile(name));     }      #endregion }  public class AssemblyFile : IReadableFile {     readonly private Assembly _assembly;     readonly private string _name;      public AssemblyFile(Assembly assembly, string name)     {         _assembly = assembly;         _name = name;     }      #region IReadableFile Members      public Stream OpenRead()     {         return _assembly.GetManifestResourceStream(_name);     }      #endregion }  public class AssemblyRepository : IRepository {     readonly private Assembly _assembly;      public AssemblyRepository(Assembly assembly)     {         _assembly = assembly;     }      #region IRepository Members      public IEnumerable<IReadableFile> Search(string pattern)     {         return _assembly.GetManifestResourceNames().Where(name => name.Contains(pattern)).Select(name => new AssemblyFile(_assembly, name)).ToArray();     }      #endregion } 

    And then you can write your algorithms dependant on only these interfaces and not on their implementations.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There is a list of all the time zones in… May 11, 2026 at 10:52 am
  • added an answer The reason why this does not work is += when… May 11, 2026 at 10:52 am
  • added an answer If you want to detect if someone is piping data… May 11, 2026 at 10:52 am

Related Questions

I am learning to use polymorphism in C#, but cannot figure out this one.
I am learning to use LabVIEW as part of my honours project, and was
I am learning to use the Dvorak keyboad layout, but I am not good
I am learning how to use NUnit. I have my main project in it's
I am interested in using/learning RoR in a project where I have to use
I am learning Python and trying to figure out an efficient way to tokenize
I am learning Silverlight and WPF on my own to expand my programming base.
I am learning WPF and was wondering if there is a way to show
I am learning C#. I want to code an on the fly encryption program.
I am pretty new to php, but I am learning! I have a simple

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.