0

I have class like:

public interface IDriver
{
    string DriverName { get; }
    IDriverConfiguration Config { get; set; }
}

public interface IDriverConfiguration
{
    string DriverName { get; }
}

And driver class implementation:

public class MyDriver : IDriver
{
    public Configuration Config { get; set; }
}

public class Configuration : IDriverConfiguration
{
    public string DriverName { get; private set; }

    public bool EnableLogger { get; private set; }

    public int ArchiveLogs { get; private set; }
}

It seems that Config property inside MyDriver should return exact interface IDriverConfiguration instead of DriverConfiguration that inherits from IDriverConfiguration.

Is there any way to ensure that MyDriver.Config property will implement at least DriverName property?

6
  • Does this code compile? Commented Sep 19, 2017 at 5:39
  • Nope. Thats my attempt to illustrate my need. Commented Sep 19, 2017 at 5:40
  • And did you try to understand the compile error and resolve them? Members of interface must be implemented in the implementing class that's the basic rule. You might want to read about interface basics and resolve compile time issues. Commented Sep 19, 2017 at 5:43
  • Your IDriverConfiguration interface ensures that "MyDriver.Config" will at least implement the DriverName property. So just change your return type of the Config property to IDriverConfiguration and you are there... Commented Sep 19, 2017 at 5:44
  • I understand compiler messages and accept them :) And i have asked this question for advice - how to achieve specified constraints. Commented Sep 19, 2017 at 5:46

1 Answer 1

3

I suppose you want the Config property of MyDriver to be a more specific type - Configuration instead of IDriverConfiguration, but writing this

public Configuration Config { get; set; }

causes your MyDriver class to "not implement all the interface members" and a compiler error occurs.

Note that it is perfectly OK for you to use IDriverConfiguration here, unless you have some extra code in Configuration that you haven't shown.

What you can do is make IDriver generic:

interface IDriver<TConfig> where TConfig: IDriverConfiguration {
    string DriverName { get; }
    TConfig Config { get; set; }
}

Your implementation will be like:

public class MyDriver : IDriver<Configuration>
{
    public Configuration Config { get; set; }
    public string DriverName => Config.DriverName;
}

You can do extension methods on this like this:

public static void MyMethod<T>(this IDriver<T> driver) where T : IDriverConfiguration {

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

9 Comments

Still doesn't compile because MyDriver doesn't have a DriverName property!
@benichka Add it in then! See my edited answer. Did it help?
I'm not the one who asked the question but this way it could be a good way to get around the problem!
Well this solved the problem i have presented! But have another :( with that construct - is there a way to write Extension Method for IDriver<IDriverConfiguration> - i mean extension that will be recognized for all drivers with any Configuration based on IDriverConfiguration interface
@ITMan You can write a generic extension method for IDriver<T>.
|

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.