简体   繁体   English

如何将条件与类匹配(实现接口)并调用其应用方法

[英]How to match condition with a class(implements interface) and call its apply method

In my console application, i have an interface named IFileConverter given below:在我的控制台应用程序中,我有一个名为IFileConverter的接口,如下所示:

public interface IFileConverter
{
    string InputFileFormat { get; }
    string OutputFileFormat { get; }
    object Convert(object input);
}

And some of class templates that implement this interface are: Png2JpgConverter class code is;而部分实现该接口的class模板有: Png2JpgConverter class 代码为;

/// <summary>
/// Image of class of converter Png file to Jpg file.
/// </summary>
public class Png2JpgConverter : IFileConverter
{
    public string InputFileFormat => "png";

    public string OutputFileFormat => "jpg";

    public object Convert(object input) => "this is a jpg file.";   // TODO ...
}

and another class for example: Jpg2BmpConverter class code is;另一个 class 例如: Jpg2BmpConverter class 代码是;

/// <summary>
/// Image of class of converter Jpg file to Bmp file.
/// </summary>
public class Jpg2BmpConverter : IFileConverter
{
    public string InputFileFormat => "jpg";

    public string OutputFileFormat => "bmp";

    public object Convert(object input) => "this is a bmp file.";   // TODO ..
}

And i have InputFileFormat which is: string inputFileFormat = "png";我有InputFileFormat这是: string inputFileFormat = "png";

OutputFileFormat which is: string outputFileFormat = "jpg"; OutputFileFormat即: string outputFileFormat = "jpg";

And object is object input = "this is a png file."; object 是object input = "this is a png file.";

My question is;我的问题是; i want to call Convert() method of appropriate class for imaginary conversion process.我想调用适当的 class 的Convert()方法进行虚拟转换过程。 But i neeed to do that in valid operation.但我需要在有效操作中做到这一点。 Is there any way to do like that given below?有没有办法像下面给出的那样做?

static void Main(string[] args)
    {
        string inputFileFormat = "png";
        string outputFileFormat = "jpg";
        object input = "this is a png file."; // for example


        //here i want to call method of Convert() like this:
        if(var selectedConverter is IFileConverter converter)
        {
            object output = selectedConverter.Convert(input);
        }
        // but i don't know how to do it and how to match inputFileFormat and outputFileFormat
        // in appropriate class that impelents IFileConverter interface...
    }

Any help would be nice..你能帮忙的话,我会很高兴..

if I have understood what you meant, I think the concept of "Polymorphism" will help in such a situation如果我明白你的意思,我认为“多态性”的概念在这种情况下会有所帮助

var converters = new List<IFileConverter> {new Jpg2BmpConverter(), new Png2JpgConverter()};

var outputList = new List<object>();

foreach (var fileConverter in converters)
{
    outputList.Add(fileConverter.Convert(input));
}

now you have a list of the outputs, you can do what you want with it.现在你有了一个输出列表,你可以用它做你想做的事。

An image in memory is only a matrix Width X Height X Channels. memory 中的图像只是一个矩阵宽度 X 高度 X 通道。 PNG and JPG are compression methods used for save these matrixes on disk. PNG 和 JPG 是用于将这些矩阵保存在磁盘上的压缩方法。

Want you convert disk files?想要转换磁盘文件吗?

bmp -> png example bmp -> png 示例

string filepath = "PathToBMPImage.bmp";
string savePath = "PathToPngOutout.png";
//Load the image, don't use new System.Drawing.Bitmap(string path) because it'll convert the image in 32BPP
using(var bmp = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(filepath))
 //Now inside bmp there is a matrix WxHxC
 bmp.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);

All GDI encoder and decoders are here .所有 GDI 编码器和解码器都在这里 You can add parameters to encoders parameters like jpg compression.您可以将参数添加到编码器参数,例如 jpg 压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在自己的类中调用接口方法? - How to call the interface method in its own class? 确定 class 是否实现通用接口,然后调用接口方法 - Determine if a class implements a generic interface and then call a the interfaces method 实现接口的类具有自己的属性时,如何使用接口? - how to use interface when a class that implements it have its own attributes? 接口方法返回类型是实现接口的类 - Interface method return type to be class that implements the interface 如何从实现接口的类中调用新方法 - How to call new methods from a class which implements interface 检查“ this”是否实现了接口,然后从通用基类内部为子类调用其成员? - Check if 'this' implements an interface then call its member from inside generic base class for subclass? 在不知道谁实现接口的情况下调用接口方法。 - Call Interface method, without knowing who implements the Interface? 如何在不浏览其继承树的情况下检查类是否实现了接口? - How to check if a class implements an interface without navigating through its inheritance tree? 通过接口方法实现接口的类的汇编版本 - Getting Assembly Version of a class that implements interface through interface method 接口方法的签名包括一个class,它实现了接口本身 - Signature of interface's method includes a class, which implements the interface itself
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM