Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Make Extension Methods For System.IO.Path Class? [duplicate]

Tags:

c#

.net

.net-4.0

How Can I Make Extension Method For System.IO.Path Class what i mean that i need something like below:

Path.GetExtension(sFilePath) 

i want to make method:

Path.GetMimeType(sFilePath)

Extension Method:

public static string GetMIMEType(this Path sPath,string sFilePath)
{
        string sExtension = Path.GetExtension(sFilePath).ToLowerInvariant();

        if (sExtension.Length > 0 && dicMIMETypes.ContainsKey(sExtension.Remove(0, 1)))
        {
            return dicMIMETypes[sExtension.Remove(0, 1)];
        }
        return "unknown/unknown";
}

but when compile above code method get error ('System.IO.Path': static types cannot be used as parameters).

Thanks All,

like image 268
Khaled Questions Avatar asked Feb 16 '23 21:02

Khaled Questions


1 Answers

Path is static, and therefore you cannot create an extension method for it. Extension methods require an instance of an object.

like image 172
Haney Avatar answered Feb 18 '23 12:02

Haney