Can I create a .NET framework class to implement the interface that I define?

I want to take a .NET class (say, FileInfo for discussion) and implement its interface. For example:

public interface IDeletable
{
    void Delete();
}

Note that FileInfo DOES has a Delete () method, so this interface may make sense.

So I might have code like this:

FileInfo fileinfo = ...;
DeletObject(fileInfo);

public void DeleteObject(IDeletable deletable)
{
    deletable.Delete();
}

Is it possible to map an existing class with such an interface?

  • Sean
+3
source share
3 answers

. . , "" framework . , framework.

, .

+5

, . Delete ( ), : # 3.0 - :

public static void SomeMethod(this FileInfo file)
{ ... your code ... }

:

FileInfo file = ...
file.SomeMethod();

.

+2

@Neil Barnwell, - . , , , .

., , "", , "", , , - ( ), , . , , , , .NET.

, , "", , ( , ) , , .

For example, I may have a class with the Dispose method ... this does not necessarily mean that it implements IDisposable ... maybe this is not because my Dispose method can throw an exception or block a certain time, therefore, by executing the contract, it does not consistent with the spirit.

Now this can happen in your application, but it’s much less likely because you know the interface, so only naturally you stick to its spirit ... but how could a .NET Framework developer do the same

+1
source

Source: https://habr.com/ru/post/1703389/


All Articles