![](/img/trans.png)
[英]Consuming an API which has a two types which are the same but have different names. How can I reuse my code?
[英]How to reuse the same variable, but for different types?
我有一個小問題,無法理解“ T型”幕后的情況是如何做到這一點的,我希望你們中的一些人可以向我展示隧道盡頭的光:)
我有一個COM對象 ,幾乎分配了一些東西(屬性),但是我需要對所有對象都使用它,我只想這樣做一次,並且只有在所有類型上都可以使用。
打印機類型:
switch (type)
{
case convert2pdf.ConvertFileType.Word:
WordPrintJob oPrintJob = null; break;
case convert2pdf.ConvertFileType.Excel:
ExcelPrintJob oPrintJob = null; break;
case convert2pdf.ConvertFileType.PowerPoint:
PowerPointPrintJob oPrintJob = null; break;
case convert2pdf.ConvertFileType.IE:
IEPrintJob oPrintJob = null; break;
case convert2pdf.ConvertFileType.Publisher:
PublisherPrintJob oPrintJob = null; break;
case convert2pdf.ConvertFileType.Visio:
VisioPrintJob oPrintJob = null; break;
default:
GenericPrintJob oPrintJob = null; break;
}
然后,無論我創建的對象類型是什么,每次都執行以下操作:
PDFSetting oPDFSetting = null;
oPrintJob = oPrinter.GenericPrintJob;
oPDFSetting = oPrintJob.PDFSetting;
/*put water mark on the first page, set the water mark text to "BCL EasyPDF */
oPDFSetting.set_Watermark(0, true);
oPDFSetting.set_WatermarkColor(0, (uint)System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue));
oPDFSetting.set_WatermarkFirstPageOnly(0, true);
oPDFSetting.set_WatermarkText(0, "EasyPDF");
/*set the meta data for the pdf file*/
oPDFSetting.MetaData = true;
oPDFSetting.MetaDataAuthor = "Your Name";
oPDFSetting.MetaDataCreator = "BCL";
oPDFSetting.MetaDataKeywords = "PDF";
oPDFSetting.MetaDataSubject = "Converter";
oPDFSetting.MetaDataTitle = "easyPDF SDK";
我如何使用“ T型”的東西(伙計們。。。經過3年的C#學習,無論我讀什么,我仍然不明白,而且我閱讀了Wikipedia,ASP.NET 3.5 Professional書,教程,... ):(
換句話說,我該如何重用屬性。
我考慮過創建一個ExtensionMethod,但是我將不得不編寫所有這些方法,而不要重用任何代碼...我考慮過創建一個通用控件,並創建一個繼承該基本控件的新控件,以便我可以使用
GlocalObject oPrintJob = null;
...
WordPrintJob oPrintJob = (WordPrintJob)GlocalObject;
我有什么意義嗎?
從答案更新
好的,因此,除了基類/接口之外,沒有“類型T”……我然后嘗試創建該接口,以便可以繼承它,並在下面得到此圖像:
替代文字http://www.balexandre.com/temp/2009-06-02_1521_soQuestion_Interface.png
如果在界面中輸入PrintJob類型,如何返回WordPrintJob類型? :-(我不明白...
您需要定義一個基類PrintJob
並將PDFSetting
作為其屬性。
然后將WordPrintJob
, ExcelPrintJob
等定義為PrintJob
子類。
自完成此操作以來已經有很長時間了,所以我不記得您是否能夠從子類訪問PDFSettings
,或者是否必須先將變量轉換為基類。
克里斯本質上是正確的,因此您的switch語句應如下所示:
PrintJob oPrintJob = null;
switch (type)
{
case convert2pdf.ConvertFileType.Word:
oPrintJob = new WordPrintJob();
break;
case convert2pdf.ConvertFileType.Excel:
oPrintJob = new ExcelPrintJob();
break;
case convert2pdf.ConvertFileType.PowerPoint:
oPrintJob = new PowerPointPrintJob();
break;
case convert2pdf.ConvertFileType.IE:
oPrintJob = new IEPrintJob();
break;
case convert2pdf.ConvertFileType.Publisher:
oPrintJob = new PublisherPrintJob();
break;
case convert2pdf.ConvertFileType.Visio:
oPrintJob = new VisioPrintJob();
break;
default:
oPrintJob = new GenericPrintJob();
break;
}
PDFSettings屬性將在您的基本PrintJob類中定義,並且每個特定的打印作業類都將從該基類繼承。
與Lazurus的答案一起 ,您可能還想創建一個Interface,並針對它進行大部分編碼,而不是基類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.