[英]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.