我试图将自己编写的IsImage属性添加到HttpPostedFile类中,以便如果用户正在上传文件,则可以执行以下操作:

FileUpload1.PostedFile.IsImage


如何在C#中做到这一点?

最佳答案

您可以将extension methods用于此目的。

public static class HttpPostedFileExtension
{
    public static bool IsImage(this HttpPostedFile file)
    {
        /*your method code goes here*/
    }
}

关于c# - 使用继承将函数添加到内置类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2485603/

10-11 15:52