简体   繁体   中英

System.Reflection.MethodInfo class is abstract then why it has a protected constructor?

无法实例化抽象类和接口。那么为什么System.Reflection.MethodInfo拥有自己的受保护构造函数。请解释一下?

Making an abstract class' constructor protected is a fairly common pattern. The intention is to guide programmers towards the intended use of the class/constructor:

  1. It hides the constructor (eg from Intellisense) in cases where one is attempting to instantiate the class (which would not be possible with an abstract class), so you get an additional hint that the class cannot be instantiated.

    From the Framework Design Guidelines, chapter 4.4 "Abstract class design":

    "Constructors should be public only if users will need to create instances of the type. Because you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed and misleading to the users."

  2. At the same time the constructor is still available for subclasses (as could be seen eg in Visual Studio's Object Explorer or in some other form of reference documentation), hinting at the fact that the type is meant to be used that way (subclassed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM