如何转换多个嵌套接口 - C#
How to cast several nested interfaces - C#
我有IReport
界面。这个接口是通用的,有多个属性,为了不凸屏,假设它只有一个ID
属性和T object
:
public interface IReport<T>
{
public ind ID {get;}
public T ReportedObject {get;}
/*And more properties I want to see when receiving a report*/
}
现在,我有另一个界面可以模拟我的数据库中的一些书籍。
public interface IBook
{
public string Title {get;}
public int ID {get;}
/*and more*/
}
/*so I made:*/
public interface IReportBook<T> : IReport<T> where T : <IBook>
{}
我有一个异步方法可以从数据库(以及更多)中获取书籍。我想给它传递一个 `IProgress` 以便我可以监控它:
/*In BookFinder.cs */
public async Task<IBook> FindThisBook(int bookID, IProgress<IReportBook<IBook>>)
{
/*Does somethings*/
}
实施:
//1) The book. This implementation is unique for the UI.
public class UIBook : SomeClassIMustInheritFrom, IBook
{
/*This implementations has a lot of methods unique to it*/
}
//2) The book report for the generic book:
public class UIBookReport : IReportBook<UIBook>
{
/*This implementations has a UIBook property and an ID*/
}
我的目标是能够将 IReportBook 的不同实现传递给 BookFinder,具体取决于我是否在控制台、WPF 等上。
即:
/*in some UI script */
private readonly BookFinder;
public async Task<UIBook > PassBook(IBook bookInLibrary)
{
Progress<UIBookReport> Report = new Progress<UIBookReport>();
Report.ProgressChanged += DoSomething;
var book = await BookFinder.FindThisBook(bookInLibrary.ID, Report); //<--
return book as UIBook;
}
/*---------Again, here's how this method looks:---------*/
/*In BookFinder.cs */
public async Task<IBook> FindThisBook(int bookID, IProgress<IReportBook<IBook>>)
{
/*Does somethings*/
}
/*is there a way to just say
IProgress<IReportBook>
instead of
IProgress<IReportBook<IBook>>
? because all IReportBooks use IBook...
*/
我的错误是:
... cannot convert type 'X' to 'Y'... via a reference conversion, boxing conversion, wrapping conversion, nor null type conversion...
我应该做哪种类型的铸造?是否存在错误或以某种方式简化了整个事情?谢谢。
基本上我改变了 3 件事。
1) 我做了一个继承自 Progress 的 class
public class BookProgress : Progress<IBookReport>
{
//Left it blank
}
2) 为 IBookReport 做了一个名为 UIBookReport 的实现
public interface IBook {}
public class UIBook : IBook {}
public interface IReport<T> {}
public interface IBookReport : IReport<IBook>{}
public class UIBookReport : IBookReport {}
3)
/*--------- in the ui*/
public UIBook PassBook(IBook bookInLibrary)
{
BookFinder bookFinder = new BookFinder();
BookProgress progress = new BookProgress();
progress.ProgressChanged += DoSomething;
var book = await bookFinder.FindThisBook(bookInLibrary.ID, progress); //<--
return book as UIBook;
}
/*-------- In BookFinder.cs*/
public async Task<Book> FindBook(int bookID, IProgress<IBookReport> progress)
{
/*Do stuff*/
var GenericBookReport = AppFactory.GetBookReport(/*some args*/);
//AppFacotry is a script that returns the right implementation of some interfaces depending of the circumstances
//In this case, it returns an UIBookReport (the class)
progress.Report(GenericBookReport);
/*more stuff*/
}
我有IReport
界面。这个接口是通用的,有多个属性,为了不凸屏,假设它只有一个ID
属性和T object
:
public interface IReport<T>
{
public ind ID {get;}
public T ReportedObject {get;}
/*And more properties I want to see when receiving a report*/
}
现在,我有另一个界面可以模拟我的数据库中的一些书籍。
public interface IBook
{
public string Title {get;}
public int ID {get;}
/*and more*/
}
/*so I made:*/
public interface IReportBook<T> : IReport<T> where T : <IBook>
{}
我有一个异步方法可以从数据库(以及更多)中获取书籍。我想给它传递一个 `IProgress` 以便我可以监控它:
/*In BookFinder.cs */
public async Task<IBook> FindThisBook(int bookID, IProgress<IReportBook<IBook>>)
{
/*Does somethings*/
}
实施:
//1) The book. This implementation is unique for the UI.
public class UIBook : SomeClassIMustInheritFrom, IBook
{
/*This implementations has a lot of methods unique to it*/
}
//2) The book report for the generic book:
public class UIBookReport : IReportBook<UIBook>
{
/*This implementations has a UIBook property and an ID*/
}
我的目标是能够将 IReportBook 的不同实现传递给 BookFinder,具体取决于我是否在控制台、WPF 等上。
即:
/*in some UI script */
private readonly BookFinder;
public async Task<UIBook > PassBook(IBook bookInLibrary)
{
Progress<UIBookReport> Report = new Progress<UIBookReport>();
Report.ProgressChanged += DoSomething;
var book = await BookFinder.FindThisBook(bookInLibrary.ID, Report); //<--
return book as UIBook;
}
/*---------Again, here's how this method looks:---------*/
/*In BookFinder.cs */
public async Task<IBook> FindThisBook(int bookID, IProgress<IReportBook<IBook>>)
{
/*Does somethings*/
}
/*is there a way to just say
IProgress<IReportBook>
instead of
IProgress<IReportBook<IBook>>
? because all IReportBooks use IBook...
*/
我的错误是:
... cannot convert type 'X' to 'Y'... via a reference conversion, boxing conversion, wrapping conversion, nor null type conversion...
我应该做哪种类型的铸造?是否存在错误或以某种方式简化了整个事情?谢谢。
基本上我改变了 3 件事。
1) 我做了一个继承自 Progress 的 class
public class BookProgress : Progress<IBookReport>
{
//Left it blank
}
2) 为 IBookReport 做了一个名为 UIBookReport 的实现
public interface IBook {}
public class UIBook : IBook {}
public interface IReport<T> {}
public interface IBookReport : IReport<IBook>{}
public class UIBookReport : IBookReport {}
3)
/*--------- in the ui*/
public UIBook PassBook(IBook bookInLibrary)
{
BookFinder bookFinder = new BookFinder();
BookProgress progress = new BookProgress();
progress.ProgressChanged += DoSomething;
var book = await bookFinder.FindThisBook(bookInLibrary.ID, progress); //<--
return book as UIBook;
}
/*-------- In BookFinder.cs*/
public async Task<Book> FindBook(int bookID, IProgress<IBookReport> progress)
{
/*Do stuff*/
var GenericBookReport = AppFactory.GetBookReport(/*some args*/);
//AppFacotry is a script that returns the right implementation of some interfaces depending of the circumstances
//In this case, it returns an UIBookReport (the class)
progress.Report(GenericBookReport);
/*more stuff*/
}