I'm using interfaces in this case mostly as a handle to an immutable instance of an object. The problem is that nested interfaces in C# are not allowed. Here is the code:
public interface ICountry { ICountryInfo Info { get; } // Nested interface results in error message: // Error 13 'ICountryInfo': interfaces cannot declare types public interface ICountryInfo { int Population { get; } string Note { get; } } } public class Country : ICountry { CountryInfo Info { get; set; } public class CountryInfo : ICountry.ICountryInfo { int Population { get; set; } string Note { get; set; } ..... } ..... }
I'm looking for an alternative, anybody would have a solution?
We can declare interfaces as member of a class or another interface. Such an interface is called as member interface or nested interface. Interfaces (or classes) can have only public and default access specifiers when declared outside any other class (Refer this for details).
The problem is that nested interfaces in C# are not allowed.
Yes, you can define a class inside an interface.
An inner interface has to be static in order to be accessed. The interface isn't associated with instances of the class, but with the class itself, so it would be accessed with Foo. Bar , like so: public class Baz implements Foo.
VB.NET allows this. So, you can create a VB.NET assembly only with the interface definitions that you need:
Public Interface ICountry ReadOnly Property Info() As ICountryInfo Public Interface ICountryInfo ReadOnly Property Population() As Integer ReadOnly Property Note() As String End Interface End Interface
As for the implementation, C# does not support covariant return types, so you must declare your class like this:
public class Country : ICountry { // this property cannot be declared as CountryInfo public ICountry.ICountryInfo Info { get; set; } public class CountryInfo : ICountry.ICountryInfo { public string Note { get; set; } public int Population { get; set; } } }
If the end goal is to use this with dependency injection, what's wrong with injecting them into each other instead of nesting?
public interface ICountry { ICountryInfo Info { get; } } public interface ICountryInfo { int Population { get; set; } string Note { get; set; } }
and implement as:
public class Country : ICountry { private readonly ICountryInfo _countryInfo; public Country(ICountryInfo countryInfo) { _countryInfo = countryInfo; } public ICountryInfo Info { get { return _countryInfo; } } } public class CountryInfo : ICountryInfo { public int Population { get; set; } public string Note { get; set;} }
Then once you set up your bindings for ICountry & ICountryInfo, CountryInfo will inject into Country whenever Country is injected.
You could then restrict the binding, if you wanted, to only inject CountryInfo into Country and nowhere else. Example in Ninject:
Bind<ICountry>().To<Country>(); Bind<ICountryInfo>().To<CountryInfo>().WhenInjectedInto<Country>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With