0

Consider the following interface...

public interface ILibraryService<T>
where T : Library
{
    ReadOnlyObservableCollection<T> AvailableLibraries { get; }
}

I want to define a static property which can hold any object which implements this interface, like this pseudo-code...

public static class Services
{
    public static ILibraryService<T> LibraryService { get; set; }
}

...but I can't figure out how to define the property. I know it's something simple, but I'm just not seeing it.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

1 Answers1

0

Are any of these what you're after? To my knowledge, Microsoft deliberately avoided giving us the ability to add type clauses on properties.

public static class Services
{
    public static ILibraryService<T> GetLibraryService<T>() 
        where T : Library
    {
        return null; // ...
    }
}

public static class Services<T>
    where T : Library
{
    public static ILibraryService<T> LibraryService { get; set; }
}

public static class Services<T>
    where T : ILibraryService<Library>
{
    public static T LibraryService { get; set; }
}

public static class Services<TService, TLibrary>
    where TService : ILibraryService<TLibrary>
    where TLibrary : Library
{
    public static ILibraryService<TLibrary> LibraryService { get; set; }
}
Trevor Ash
  • 623
  • 4
  • 8
  • Aaah... I got excited there before I realized your first example is a function, not a property. No idea why they limit the latter when the former is fine. But I guess the answer is I can't do it as a property, which is what I was hoping for. – Mark A. Donohoe Jul 05 '16 at 17:08