类类型的字典 [英] Dictionary of class types

查看:74
本文介绍了类类型的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组类,每个类都可以使用外部应用程序打开不同类型的文件,并告诉应用程序将文件打印到特定的打印机。

 内部接口IApplicationPrinter:IDisposable 
{
字符串ApplicationExe {get; }
字符串ApplicationName {get; }
string [] PrintableExtensions {get; }

IApplicationPrinter CreateInstance(字符串文件名,字符串打印机);
void Print();
bool ExitApplicationAfterPrint {get;组; }
bool WaitApplicationExitOnPrint {get;组; }
System.IO.FileInfo PdfFile {get;保护组; }
}
内部抽象类ApplicationPrinter:IApplicationPrinter
{
...
}
内部类WordPrinter:ApplicationPrinter
{
内部静态字符串[] PrintableExtensions {get {return new string [] {。doc,.docx}; }}
...
}
内部类ExcelPrinter:ApplicationPrinter
{
内部静态字符串[] PrintableExtensions {get {return new string [] {。xls ,.xlsx}; }}
...
}

我试图创建一个 Dictionary 可打印的文件扩展名和相应的键入可以打印这些文件的类。

  private static Dictionary< string,Type> ; FileConverters; 
static Printer()
{
FileConverters = new Dictionary< string,Type>();

foreach(WordPrinter.PrintableExtensions中的字符串转换)
{
FileConverters.Add(ext,typeof(WordPrinter));
}

string filename =textfile.txt;
string extension = filename.Substring(filename.LastIndexOf(。)); (FileConverters.ContainsKey(扩展名))
{
IApplicationPrinter printer =((IApplicationPrinter)FileConverters [扩展名])。CreateInstance(filename,Printer);

if
printer.Print();






$ b

有没有办法让 Dictionary< string,Type> FileConverters 通过将其限制为实现IApplicationPrinter的值更安全吗?换句话说,就像这样:

  private static Dictionary< string,T> FileConverters其中T:IApplicationPrinter; 

更新:
我不想存储以下两个原因的实例: string [] PrintableExtensions
)。该字典将扩展名存储为密钥。创建和存储同一类的多个分离实例没有实用性。

  • 每个打印机类都使用COM API和Office Interop来创建第三方应用程序的实例。最好在需要时为打印作业创建每个类的新实例,并且垃圾收集器可以在之后清理。 不要直接记住你放入字典的东西是类型对象,而不是实现<$ c的对象$ c $> IApplicationPrinter 。



    可能这里最好的选择是检查你添加到字典中的每种类型是否实现 IApplicationPrinter ,通过检查 type.GetInterface(IApplicationPrinter)是否返回null。


    I have a set of classes, each of which can open different types of files using an external application and tell that application to print the file to a particular printer. The classes all inherit a common abstract class and an interface.

    internal interface IApplicationPrinter : IDisposable
    {
        string ApplicationExe { get; }
        string ApplicationName { get; }
        string[] PrintableExtensions { get; }
    
        IApplicationPrinter CreateInstance(string Filename, string Printer);
        void Print();
        bool ExitApplicationAfterPrint { get; set; }
        bool WaitApplicationExitOnPrint { get; set; }
        System.IO.FileInfo PdfFile { get; protected set; }
    }
    internal abstract class ApplicationPrinter : IApplicationPrinter
    {
        ...
    }
    internal class WordPrinter : ApplicationPrinter
    {
        internal static string[] PrintableExtensions { get { return new string[]{".doc", ".docx" }; } }
        ...
    }
    internal class ExcelPrinter : ApplicationPrinter
    {
        internal static string[] PrintableExtensions { get { return new string[]{".xls", ".xlsx" }; } }
        ...
    }
    

    I am trying to create a Dictionary of printable file extensions and corresponding Types of classes that can print such files. I do not want to instantiate the classes in the dictionary.

    private static Dictionary<string, Type> FileConverters;
    static Printer()
    {
        FileConverters = new Dictionary<string, Type>();
    
        foreach (string ext in WordPrinter.PrintableExtensions)
        {
            FileConverters.Add(ext, typeof(WordPrinter));
        }
    
        string filename = "textfile.txt";
        string extension = filename.Substring(filename.LastIndexOf("."));
    
        if (FileConverters.ContainsKey(extension))
        {
            IApplicationPrinter printer = ((IApplicationPrinter)FileConverters[extension]).CreateInstance(filename, "Printer");
            printer.Print();
        }
    }
    

    Is there any way to make Dictionary<string, Type> FileConverters more type-safe, by restricting it to values that implement IApplicationPrinter? In other words, is something like this possible:

    private static Dictionary<string, T> FileConverters where T: IApplicationPrinter;
    

    Update: I do not want to store instances for the following two reasons:

    1. Each class can handle several different file types (see string[] PrintableExtensions). The dictionary stores extensions as keys. There is no utility in creating and storing multiple separates instance of the same class.
    2. Each printer class uses COM API and Office Interop to create instances of third-party applications. It's better that a new instance of each class is created for a print job when so required, and that the garbage collector can clean up afterwards.

    解决方案

    Not directly - remember that the things you're putting in your dictionary are Type objects, not objects that implement IApplicationPrinter.

    Probably the best option here is to check that each type you add to your dictionary implements IApplicationPrinter, by checking whether type.GetInterface("IApplicationPrinter") returns null or not.

    这篇关于类类型的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆