本文介绍了MethodInfo和ConstructorInfo有什么区别??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎两者都可以调用构造函数方法,但是,有什么区别呢?


Marcus

Seems both can call constructor method , but , what's the difference ???


Marcus

推荐答案

MethodInfo类派生自MethodBase,并且仅表示方法(而非构造函数).它将添加一个ReturnType属性,该属性提供一个Type对象,该对象指示该方法的返回类型(Thre是一种特殊的系统类型,System.Void,其Type对象为 当方法没有返回类型时在此处使用.)

The MethodInfo class derives from MethodBase and represents only methods(and not constructors). It add a ReturnType property that provides a Type object indicating the method’s return type (Threre’s a special system type, System.Void, whose Type object is used here when a method has no return type.)

除了从MethodBase继承的属性外,ConstrcutorInfo类不添加任何属性.但是,它确实定义了两个只读静态字段:ConstructorName和TypeConstrucotrName.它们分别包含字符串".ctor"和".cctor",其中 是在实例和静态构造函数的ContructorInfo对象的Name属性中可以找到的值.就CLR而言,有真实名称-尽管在C#中,构造函数似乎与其包含类型具有相同的名称, 仅在您的C#源文件中有效,而在运行时则无效.

The ConstrcutorInfo class does not add any properties beyond those it inherits from MethodBase. It does define two read-only static fields, though: ConstructorName and TypeConstrucotrName. These contain the string ".ctor" and ".cctor", respectively, which are the values you will find in the Name property for ContructorInfo objects for instance and static constructors. As far as the CLR is concerned, there are the real names – although in C# constructors appear to have the same name as their containing type, that’s true only in your C# source files, and not at runtime.

您可以通过调用Invoke方法来调用由MethodInfo或ConstructorInfo表示的方法或构造函数.这与Type.InvodeMember的作用相同,但是,由于Invoke专门用于处理方法和构造函数, 它使用起来更简单,而且还具有可以在Core个人资料中使用的优势.使用ConstructorInfo,您只需要传递一个参数数组.使用MethodInfo,您还可以传递要在其上调用方法的对象;如果需要,可以传递null 调用静态方法.

You could invoke the method or constructor represented by a MethodInfo or ConstructorInfo by calling Invoke method. This does the same thing as Type.InvodeMember, However, because Invoke is specialized for working with just methods and constructors, it’s rather simpler to use, and also has benefit of being available in the Core profile. With a ConstructorInfo, you need to pass only an array of arguments. With MethodInfo, you also pass the object on which you want to invoke the method, or null if you want to invoke a static method.

最诚挚的问候,

吴可乐


这篇关于MethodInfo和ConstructorInfo有什么区别??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:05