简体   繁体   English

使用继承将函数添加到内置类

[英]Add function to built-in class using inheritance

I'm trying to add a IsImage property that I wrote myself to the HttpPostedFile class, so that if a user is uploading a file, I can do something like this: 我试图将自己编写的IsImage属性添加到HttpPostedFile类中,以便如果用户正在上传文件,则可以执行以下操作:

FileUpload1.PostedFile.IsImage

How can I do that in C#? 如何在C#中做到这一点?

You can use extension methods for that purpose. 您可以为此使用扩展方法

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM