Re: Add custom method to File Class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Apr 17, 11:16 am, "Alberto Poblacion" <earthling-
quitaestoparacontes...@xxxxxxxxxxxxx> wrote:
<joecool1...@xxxxxxxx> wrote in message

news:991a5a9a-9a69-4fc3-a142-583052de0961@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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.

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).
.



Relevant Pages

  • Re: Include module and class inheritance? which one to choose?
    ... but we can achieve the same thing by inherit a class which will get ... You need to approach this issue in terms of semantics rather than ... functionality which is not dependent on a specific object, ... A Math object wouldn't make much ...
    (comp.lang.ruby)
  • Re: Need help using VS 2008 Unit Test framework.
    ... It should have created an accessor that has this. ...     public class Class1 ... For some bizarre reason Unit Test Accessors stop at framework ... If I inherit from a custom object the inherited fields and methods get ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Add custom method to File Class
    ...     Instead of inheriting, you could encapsulate the class inside your own. ... FileInfo provides just about the same functionality but uses instance ... Since File is a Static class, I don't think you can extend it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Changing an indexed field
    ... Not exactly sure what you mean by a "set-base update", Gene. ... handful of which need processing by this program ... new functionality is that not every flagged record gets processed like ...
    (microsoft.public.fox.programmer.exchange)
  • Re: subclass with static constructor
    ... it seems to me that you have an inheritance relationship between the functionality. ... But you can only inherit with instances. ... It seems to me that the closest next alternative would be to replicate the entire general-purpose API found in the base class, as static members in the database-specific classes. ...
    (microsoft.public.dotnet.languages.csharp)