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
  • 2 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

Related Questions

I'm learning to use raw sockets, and im trying to prase out the tcp
I'm learning to use the write function and am trying to print only a
I'm learning to use Allegro. I'm trying to make my character cut out. How
I'm learning to use object literals in JS, and I'm trying to get a
I am learning to use the Dvorak keyboad layout, but I am not good
I am learning to use xmlhttprequest/AJAX. In this sample code from w3schools, I do
I've been full of questions lately, but thanks to this awesome community, I'm learning
I am learning to use json_decode . I want to try out some php
I'm learning to use the PHP interactive shell, but I'm having trouble with multi-line
I am learning to use Processing , and have modified one of the examples

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.