C#,. net,通用编程架构.GetType或Enum:哪个更好?

Guy*_*ito 4 .net c# enums types

你认为哪个好些..

        if (thing.GetType() == typeof(thisthing))
        {
           //do stuff for this type of thing.
        }
Run Code Online (Sandbox Code Playgroud)

或者给对象一个Enum属性

        if (thing.TypeName == NamesEnum.thisthing)
        {
           //do stuff for this type of thing.
        }
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 12

这些都不是特别可扩展或可维护的方法.

通常最好将其直接设计到类型层次结构中的虚方法中,然后调用该方法.这允许其他类覆盖并提供适当的自定义功能.

在您的情况下,thisthing类型(ThisThing如果您想遵循.NET命名约定,应该命名)将只有一个DoStuff方法,如果需要可以是虚拟的,并调用它.