Home > Net >  How to call a specific method in a child class, based on the type of the generic interface implement
How to call a specific method in a child class, based on the type of the generic interface implement

Time:02-10

Is there a way to pass an object to an abstract base class in such a way that the derived classes themselves can determine if this object is relevant to them. Where ‘relevent to them’ means that the derived class is implementing a generic interface Interface, and T is matching with the type of the object we passed to the abstract class. Interface enforces implementation of a method receiving T as input. This is the method that should be receiving the object passed to the abstract class.

My example: I have an abstract 'criterion'-class, which represents a value to use in filtering. So maybe I have concrete criteria like ProgrammingLanguageCriterion (with possible values like c# and java), DateRangeCriterion and ExperienceLevelCriterion (with values like 1..4).

But also there exists something like global settings, if active, only specific values are available to be set on the criteria. If such a global setting is applied, all currently active criteria should be checked if they are consistent with this new setting. For example in 'easy mode' it is not possible to use an ExperienceLevel > 2, and you can only choose c# as your programming language (?). Other criteria may not be affected.

(There are different global settings (for example: SomeSettingClass), I store them in an object called 'ConsistencyCheck')

    public class ConsistencyCheck
    {
        private object _settingToCheck;

        public T GetSettingToCheck<T>() where T : class
        {
            return _settingToCheck as T;
        }

        public void SetSettingToCheck<T>(T settingToCheck) where T : class
        {
            _settingToCheck = settingToCheck;
        }

I would like to call a method on 'AbstractCriterion' to apply this new setting, and let the derived class itself check if the new setting is consistent with the current value of the criterion. Like this:

    public abstract class AbstractCriterion 
    {
        public virtual void ProcessConsistencyCheck(ConsistencyCheck settingToProcess)  
        {
          /// magic should happen here ...
        }

    }

This magic should be: The method should call the 'CheckConsistencyWith'-method in the derived class, based on if the derived class implements a generic interface with type corresponding with the object which is returned by the 'settingToProcess.GetSettingToCheck()'-method, and should then call the method belonging to this generic interface.

    public interface ICheckFilterConsistency<T> where T:class
    {
        void CheckConsistencyWith(T item);
    }

    public class ExperienceLevelCriterion : AbstractCriterion, ICheckFilterConsistency<SomeSettingClass>
    {
        //
        // some other code ..
        //


        public void CheckConsistencyWith(SomeSettingClass item)
        {
            // check consistency and do something if needed
        }
    }

Is there some easy way I have missed?

CodePudding user response:

  •  Tags:  
  • Related