考虑以下界面......
public interface ILibraryService<T>
where T : Library
{
ReadOnlyObservableCollection<T> AvailableLibraries { get; }
}
我想定义一个静态属性,它可以容纳任何实现此接口的对象,就像这个伪代码......
public static class Services
{
public static ILibraryService<T> LibraryService { get; set; }
}
...但我无法弄清楚如何定义属性。我知道这很简单,但我只是没有看到它。
答案 0 :(得分:0)
这些都是你追求的吗?据我所知,微软故意避免让我们在属性上添加类型子句。
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; }
}