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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:52:52+00:00 2026-05-24T10:52:52+00:00

Given basetype A And three derived types B : A C : A D

  • 0

Given basetype A

And three derived types
B : A
C : A
D : A

I have a project P.
I want a List to represent occurrences of A in P.

There is a strategy for extracting Bs from P, which I want to be part of class B.
There is a strategy for extracting Cs from P, which I want to be part of class C.
etc.
I want them all to be listed in one big List.

I want to later be able to add a class E : A, touching as little as possible. It would be cool to have a virtual static Factory method on A accepting instance of P as a parameter, which would polymorphically run all static overloads in the derived classes where for example the overload in C would extract Cs from P and put them in List.
Of course, no such thing as a virtual static method exists in C#.

I can’t immediately see how to implement this so that it would be possible to add classes D:A and E:A without touching the base class or some continuously updated “God-method” factory method with a concrete dependency to every derived type.

It’s kinda late here, so I may be missing something obvious.
Your thoughts?

EDIT:

My specific case is that I have a process plant control system consisting of control modules. I want to be able to recognize certain higher level constructs such as control loop, feedback tuning etc. The logic for recognizing and managing these constructs is specific to the individual construct type in question. In short, I want to bundle the code for identifying and for handling constructs.

Let’s consider an analogy. I have a text document. In it there are types of words. Word is the “base” type “A”. The text document is the “P” project. I can implement the word types “noun” and “verb”. The way in which a “noun” is identified within the text is specific to “noun” and the code should. The List becomes longer as more and more types are implemented and thusly identified within the text.

To me, it makes sense to implement this within the noun class as:

 static function IEnumerable<noun> IdentifyAll (P project)

And do a

CompleteWordList.AddRange(noun.IdentifyAll(p));

During initialization, but this creates a dependency to the specific “noun” type from the central initialization/factory method. It would then be difficult to add more word classes without touching it. As I’m writing this, I feel myself sort of leaning towards MEF.

This is a simple example. Maybe “phrase” or something would be a more appropriate analogy for the base type, but it’ll have to do for now.

Even though these projects are networks of connected control nodes with inspectable properties (and not a text document as such), it very much feels like a parser. It would be good if there is some general solution that eludes me.

  • 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-24T10:52:53+00:00Added an answer on May 24, 2026 at 10:52 am

    Okay, I think I get it now. If I understand, you want to take a an object of a type derived from A and determine its actual type (say B), but without the factory knowing how to classify it as B. Then you should be able to introduce any number of subtypes of A in future without having to modify the factory for each new type.

    Someone has know how to get from A to B. If it isn’t the factory, then it must be B itself. I see you’ve gone down this path and thought “I need a virtual factory method so that each subtype of A can provide the specification to identify itself.”

    As you said, virtual static methods don’t exist so you can’t do it how you were thinking. But why not use an instance method?

    As long as the project contains a list of all known subtypes, it can simply iterate through these types, creating instances of them and asking them “is this object one of you?”. And if it says “yes”, you can then ask it “give me an instance of you equivalent to this base type instance”. (That sounds inefficient; in practice of course you could just keep one “factory instance” of each type alive so you don’t have to keep recreating them.)

    A contrived example using something like your word analogy:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Drawing;
    
    interface IWord
    {
        bool IsTypeOf(IWord word);
        string Text { get; set; }
        IWord MakeFrom(IWord word);
    }
    
    // the base type
    class UntypedWord : IWord
    {
        public virtual string Text { get; set; }
        public virtual bool IsTypeOf(IWord word)
        {
            throw new NotImplementedException();
        }
        public virtual IWord MakeFrom(IWord word)
        {
            throw new NotImplementedException();
        }
    }
    
    // one specific subtype
    class ColorWord : UntypedWord
    {
        public Color Color { get; private set; }
    
        public override bool IsTypeOf(IWord word)
        {
            return word.Text == "red" || word.Text == "green" || word.Text == "blue";
        }
    
        public override IWord MakeFrom(IWord word)
        {
            var newMe = new ColorWord();
            newMe.Text = word.Text;
            if (word.Text == "red") newMe.Color = Color.Red;
            else if (word.Text == "blue") newMe.Color = Color.Blue;
            else if (word.Text == "yellow") newMe.Color = Color.Yellow;
            return newMe;            
        }
    }
    
    // another specific type
    class NumberWord : IWord // note: not an UntypedWord (see comments below)
    {
        public int Number { get; set; }
        public string Text { get; set; }
    
        public bool IsTypeOf(IWord word)
        {
            return word.Text == "one" || word.Text == "two" || word.Text == "three";
        }
    
        public IWord MakeFrom(IWord word)
        {
            var newMe = new NumberWord();
            newMe.Text = word.Text;
            if (word.Text == "one") newMe.Number = 1;
            else if (word.Text == "two") newMe.Number = 2;
            else if (word.Text == "three") newMe.Number = 3;
            return newMe;
        }
    }
    
    
    class WordList
    {
        Collection<Type> WordTypes = new Collection<Type>();
        Collection<IWord> UntypedWords = new Collection<IWord>();
        Dictionary<Type, Collection<IWord>> StronglyTypedWords = new Dictionary<Type, Collection<IWord>>();
    
        public void AddWordType<T>() where T : IWord
        {
            WordTypes.Add(typeof(T));
    
            if (!StronglyTypedWords.ContainsKey(typeof(T)))
                StronglyTypedWords[typeof(T)] = new Collection<IWord>();
        }
    
        public void Add(IWord word)
        {
            bool foundType = false;
            foreach (Type type in WordTypes)
            {
                // in practice you'd cache these factories for efficiency
                IWord instance = Activator.CreateInstance(type) as IWord;
                if (instance.IsTypeOf(word))
                {
                    if (!StronglyTypedWords.ContainsKey(type))
                        StronglyTypedWords[type] = new Collection<IWord>();
    
                    StronglyTypedWords[type].Add(instance.MakeFrom(word));
                    foundType = true;
                }
            }
            if (!foundType)
                UntypedWords.Add(word);
        }
    
        public int HowManyWordsOfType<T>()
        {
            if (StronglyTypedWords.ContainsKey(typeof(T)))
                return StronglyTypedWords[typeof(T)].Count;
            return 0;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var sentence = new WordList();
            sentence.AddWordType<ColorWord>();
            sentence.AddWordType<NumberWord>();
    
            sentence.Add(new UntypedWord { Text = "two" });
            sentence.Add(new UntypedWord { Text = "green" });
            sentence.Add(new UntypedWord { Text = "frogs" });
            sentence.Add(new UntypedWord { Text = "and" });
            sentence.Add(new UntypedWord { Text = "one" });
            sentence.Add(new UntypedWord { Text = "red" });
            sentence.Add(new UntypedWord { Text = "rose" });
    
            Console.WriteLine("color words: " + sentence.HowManyWordsOfType<ColorWord>());
            Console.WriteLine("number words: " + sentence.HowManyWordsOfType<NumberWord>());
        }
    }    
    

    Output:

    color words: 2
    number words: 2
    

    Now when you want to add a new word type, the only code you have to add is:

    sentence.AddWordType<NewWordType>();
    

    You’ll notice that ColorWord is a subtype of UntypedWord but NumberWord isn’t. Word types don’t have to share a common base type as long as they implement IWord.

    Obviously this is a ridiculous example but it shows how each subtype owns the logic to classify itself based on properties of a given object of the common supertype (or common interface), and it knows how to create an instance of itself based on the given object.

    So now the WordList (your P) class never has to be updated to accommodate new word types. You just need to tell it about all the types at runtime.

    And you could write your IdentifyAll(WordList sentence) method, only it would be an instance method instead of static.

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

Sidebar

Related Questions

Given a Python object of any kind, is there an easy way to get
I have some types that extend a common type, and these are my models.
I have got warning from subject on one of my classes. Actually class is
I have a derived class with a method that overrides the base class's method,
After successfully getting a list of specific types out of an assembly using reflection,
Given the code below: static void Main() { Console.WriteLine(typeof(MyEnum).BaseType.FullName); } enum MyEnum : ushort
Given long long int x, y; , I want a function that can compare
Given an inorder-traversal list, what's the best way to create a Binary Min/Max Heap?
Given this pattern match: List(1,2,3) match { case head :: tail => println(>>> head=
Given a specific DateTime value, how do I display relative time, like: 2 hours

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.