使用继承向内置类添加函数

Ste*_*ven 1 c# asp.net

我正在尝试添加一个我自己写入HttpPostedFile类的IsImage属性,这样如果用户上传文件,我可以这样做:

FileUpload1.PostedFile.IsImage
Run Code Online (Sandbox Code Playgroud)

我怎么能在C#中做到这一点?

Li0*_*liQ 5

您可以使用扩展方法来实现此目的.

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