Add custom method to File Class

J

joecool1969

I would like to create a class that inherits the File Class so I can
add a method to it. Then my class will have all of the functionality
of the original File class with the addition of my custom method.

But it appears that the File Class is a static class and you cannot
inherit from a static class.

Is there some way this can be done?
 
A

Alberto Poblacion

I would like to create a class that inherits the File Class so I can
add a method to it. Then my class will have all of the functionality
of the original File class with the addition of my custom method.

But it appears that the File Class is a static class and you cannot
inherit from a static class.

Is there some way this can be done?

Instead of inheriting, you could encapsulate the class inside your own.
In other words, write your own class, declare a File inside your class, and
add your method inside your class. You can either expose the File itself
through a public property of your class, or add methods in your class to
call the methods in the encapsulated File.

Alternatively, you could use the FileInfo class instead of the File
class. FileInfo provides just about the same functionality but uses instance
methods instead of static methods. You can still not inherit from it because
it is a sealed class, but you can add an Extension method (if you are using
C# 3.0), which may be adequate for your needs.
 
P

Paul Shapiro

Lookup "extension methods" in the online help. You can add methods to an
existing .Net class without inheritance.
 
A

Alberto Poblacion

Paul Shapiro said:
Lookup "extension methods" in the online help. You can add methods to an
existing .Net class without inheritance.

Can you use extension methods on a static class? I thought that you
always had to invoke the extension method on an instance of the class.
 
J

joecool1969

    Instead of inheriting, you could encapsulate the class inside your own.
In other words, write your own class, declare a File inside your class, and
add your method inside your class. You can either expose the File itself
through a public property of your class, or add methods in your class to
call the methods in the encapsulated File.

    Alternatively, you could use the FileInfo class instead of the File
class. FileInfo provides just about the same functionality but uses instance
methods instead of static methods. You can still not inherit from it because
it is a sealed class, but you can add an Extension method (if you are using
C# 3.0), which may be adequate for your needs.

Thanks for the replies.

Specifically I woud like a method that takes two parameters, paths to
two files, and returns true if the files are identical or false if
there are any differences. Like File.Compare(path1, path2).
 
D

Dude

    Instead of inheriting, you could encapsulate the class inside your own.
In other words, write your own class, declare a File inside your class, and
add your method inside your class. You can either expose the File itself
through a public property of your class, or add methods in your class to
call the methods in the encapsulated File.

    Alternatively, you could use the FileInfo class instead of the File
class. FileInfo provides just about the same functionality but uses instance
methods instead of static methods. You can still not inherit from it because
it is a sealed class, but you can add an Extension method (if you are using
C# 3.0), which may be adequate for your needs.

Since File is a Static class, I don't think you can extend it. I
think you are SOL here.
I agree with the suggestion to just extent FileInfo instead.
 
D

Dude

I would like to create a class that inherits the File Class so I can
add a method to it. Then my class will have all of the functionality
of the original File class with the addition of my custom method.

But it appears that the File Class is a static class and you cannot
inherit from a static class.

Is there some way this can be done?

I think your only option here (If you really want to do this) is
simply to create a new static class, and re-create the methods on
File. You could call

public static class MyFile
{
public static void AppendAllText(string path, string contents)
{
System.IO.File.AppendAllText(path, contents);
}
...
public static void MyMethod()
{
...
}
}

I just don't know that I see the reasoning behind this. What the
difference between calling

System.IO.File.MyMethod()
vs
Calling MyFile.MyMethod();

If it were possible, your new method would need to be static anyway.
 
A

Arne Vajhøj

I would like to create a class that inherits the File Class so I can
add a method to it. Then my class will have all of the functionality
of the original File class with the addition of my custom method.

But it appears that the File Class is a static class and you cannot
inherit from a static class.

Is there some way this can be done?

No.

And there are not really any point.

Since all the methods are static just make your MyFile class
and use File.Foo and MyFile.Bar - I see the different names
as an advantage.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top