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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:06:51+00:00 2026-05-23T11:06:51+00:00

I have several collections that all look the same but operate on different types

  • 0

I have several collections that all look the same but operate on different types of collection items, like this:

[CollectionDataContract(ItemName = "Culture")]
public sealed class Cultures : List<LangCult>
{
}

The important piece of code here is the CollectionDataContract attribute which allows us to serialize this collection into an XML file with DataContractSerializer. The entire collection is serialized into one XML file named as the name of the type of collection class, for instance Cultures.xml

I would like to come up with a repository, precisely with an interface for a repository, that would work on all of the possible collections, for instance public class Slugs : List<string>.

I tried something like this, but I don’t know whether it’s the best option:

public interface ICollectionsRepository<T> where T : IEnumerable

Your thoughts?

Please do not respond with instructions on how to serialize because that is not the issue here and I have it working.

  • 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-23T11:06:51+00:00Added an answer on May 23, 2026 at 11:06 am

    Maybe I should’ve said I wanted an interface for generic collection with generic items where items have a common base class. This is how I solved it and hopefully someone one day finds it useful.

    /// <summary>
    /// This repo enables us to work with serialisable collections. Collection class has
    /// to inherit from IEnumerable and must be described with CollectionDataContract attribute
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ICollectionsRepository<T, V> where T : Collection<V>
    {
        /// <summary>
        ///     Get collection from datastore
        /// </summary>
        /// <returns>List of items</returns>
        T Load();
    
        /// <summary>
        ///     Add new collection item to datastore
        /// </summary>
        /// <param name="item">Item to be added to the collection</param>
        void Add(V item);
    }
    
    public class XmlCollectionsProvider<T, V> : ICollectionsRepository<T, V> where T: Collection<V>, new() where V: CollectionItem
    {
        private readonly string _file = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof(T).Name + ".xml");
    
        public T Load()
        {
            if (!DefaultsExist()) {
                CreateDefaults();
            }
    
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            T obj = null;
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(new FileStream(_file, FileMode.Open, FileAccess.Read),
                                                                                         new XmlDictionaryReaderQuotas());
    
            obj = (T)dcs.ReadObject(reader, true);
            reader.Close();
            return obj;
        }
    
        public void Add(V item)
        {
            T collection = Load();
            collection.Add(item);
            Save(collection);
        }
    }
    
    [CollectionDataContract(ItemName = "Culture")]
    public sealed class Cultures : List<LangCult>   {   }
    
    [DataContract]
    public class LangCult : CollectionItem
    {
        ...
    }
    
    [DataContract]
    public abstract class CollectionItem
    {
        [DataMember]
        public string Id
        {
            get;
            set;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection that contains instances of several different classes, and I want
I have a class TContainer that is an aggregate of several stl collections pointers
I have several methods that populate a SQLCommand objects parameter collection from an object
We have several jobs that run concurrently that have to use the same config
I have several old 3.5in floppy disks that I would like to backup. My
I have written my Container<T> class which backups its T items in several collections
I have designed several animated Panel s in C# that all inherit from one
I have a huge collection of visual foxpro dbf files that I would like
Several questions have been asked about the SIFT algorithm , but they all seem
I'm building a database with several collections. I have unique strings that I plan

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.