简体   繁体   English

System.Reflection.MethodInfo类是抽象的,那么为什么它有一个受保护的构造函数?

[英]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. 使抽象类的构造函数protected是一种相当普遍的模式。 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. 在试图实例化该类的情况下(对于抽象类是不可能的),它会隐藏构造函数(例如,从Intellisense中隐藏),因此您将获得额外的提示,表明该类无法实例化。

    From the Framework Design Guidelines, chapter 4.4 "Abstract class design": 根据框架设计指南的第4.4章“抽象类设计”:

    "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). 同时构造仍然是可用的子类(如可以看出,例如在Visual Studio中的对象资源管理器或引用文档中的某些其他形式),在事实类型意味着使用这种方式(子类)暗示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命令绑定无法将“System.Reflection.RuntimeEventInfo”类型的对象强制转换为“System.Reflection.MethodInfo”类型 - Command binding Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo' C#:System.Reflection.MethodInfo原因:(对象与目标类型不匹配) - C#:System.Reflection.MethodInfo cause : (Object does not match target type) 如何使用反射创建或调用抽象类的受保护构造函数? - How to create or invoke protected constructor of abstract class using reflection? 无法访问抽象类中的受保护构造函数 - cannot access protected constructor in abstract class 保护与公共构造函数的抽象类? 有区别吗? - protected vs public constructor for abstract class? Is there a difference? 如何使用Reflection将构造函数作为MethodInfo获取 - How to get constructor as MethodInfo using Reflection System.Reflection.MethodInfo.Invoke和多个线程 - System.Reflection.MethodInfo.Invoke and multiple threads 我是否需要为抽象类提供一个受保护的空构造函数? - Do I need to provide an empty protected constructor for an abstract class? 反映受保护的成员 - Reflection of protected member of a class 模拟具有构造函数依赖项的抽象类(使用Moq) - Mocking abstract class that has constructor dependencies (with Moq)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM