Why Can't I Inherit IO.Directory?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:41:06

问题


Why can't I create a class in VB.NET that inherits System.IO.Directory? According to Lutz Roeder, it is not declared as NotInheritable!

I want to create a utility class that adds functionality to the Directory class. For instance, I want to add a Directory.Move function.

Please advise and I will send you a six pack. OK nevermind I'm not sending you anything but if you come to the bar tonight I will hook you up and then beat you in pool.


回答1:


From the Meta Data of .NET

namespace System.IO
{
    // Summary:
    //     Exposes static methods for creating, moving, and enumerating through directories
    //     and subdirectories. This class cannot be inherited.
    [ComVisible(true)]
    public static class Directory

You cannot inherit from a Static Class.




回答2:


Are you using C# 3.0 VB.NET 2008 -- then you could add an Extension Method




回答3:


If you use the DirectoryInfo class, you will have access to a MoveTo function.

EDIT: I'll correct myself... The static Directory class already has a Move method.




回答4:


I'd guess that Reflector isn't picking up the sealed attribute correctly for VB (or perhaps just not displaying it properly). If you look at the IL, it is sealed:

class public abstract auto ansi sealed beforefieldinit Directory



来源:https://stackoverflow.com/questions/135759/why-cant-i-inherit-io-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!