0

I have a class structure like this:

public class BaseDataType
{
    //... some properties
}


public class FirstCustomDataType : BaseDataType
{
    //...some more properties
}


public class SecondCustomDataType : BaseDataType
{
    //...some more properties
}


public interface IMonitor<T> where T : BaseDataType
{
    List<List<DataSample<T>>> GetData();
}


public abstract class GenericMonitor<T> : IMonitor<T> where T : BaseDataType 
{
    protected List<List<DataSample<T>>> Data { get; set; }

    public List<List<DataSample<T>>> GetData() { return Data; }
}


public class FirstNonGenericMonitor : GenericMonitor<FirstCustomDataType> { }


public class SecondNonGenericMonitor : GenericMonitor<SecondCustomDataType> { }

Now I would like to have a dictionary, that will hold all my non generic monitors:

Dictionary< ResourceToMonitorEnum, IMonitor<BaseDataType> > Monitors

However, I don't know how should I add a new monitor to this dictionary, because this:

Monitors.Add( ResourceToMonitorEnum.Monitor1, new FirstNonGenericMonitor() )

won't let me add new FirstNonGenericMonitor as the IMonitor< T > because the types don't match. I know that it's the type issue but I don't know how to solve it.

1
  • why do you even need such complex inheritance? i think you are picking a wrong way to what you want to achieve... Commented May 25, 2016 at 20:02

1 Answer 1

1

You can cast your FirstGenericMonitor instance as a IMonitor<BaseDataType> like so:

Monitors.Add(ResourceToMonitorEnum, new FirstNonGenericMonitor() as IMonitor<BaseDataType>);

Echoing M.kazem Akhgary's comment, this does seem to be a pretty complicated inheritance model you have here. It's worth considering if all of this is truly necessary, or if you are needlessly introducing complexity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.