【问题标题】:.net MCV makes use of constructors without method bodies, How is this possible and how can it be duplicated?.net MCV 使用没有方法体的构造函数,这怎么可能?如何复制?
【发布时间】:2016-05-05 16:44:40
【问题描述】:

在 MVC 应用程序中,有几个类使用没有方法体的构造函数。例如其中一个类是 ActionResult 类:

using System;

namespace System.Web.Mvc
{
    // Summary:
    //     Encapsulates the result of an action method and is used to perform a framework-level
    //     operation on behalf of the action method.
    public abstract class ActionResult
    {
        // Summary:
        //     Initializes a new instance of the System.Web.Mvc.ActionResult class.
        protected ActionResult();

        // Summary:
        //     Enables processing of the result of an action method by a custom type that
        //     inherits from the System.Web.Mvc.ActionResult class.
        //
        // Parameters:
        //   context:
        //     The context in which the result is executed. The context      information includes
        //     the controller, HTTP content, request context, and route data.
        public abstract void ExecuteResult(ControllerContext context);
    }
}

此类构造函数没有方法体。 当我尝试在应用程序中复制代码时,我得到了预期的错误,即构造函数需要有一个方法体。

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;


    public abstract class MyBase
    {
        protected MyBase();
    }
}

在这种情况下,Microsoft 做了什么让对象构造函数以这种方式工作?

【问题讨论】:

    标签: c# .net frameworks


    【解决方案1】:

    当您转到项目外部的类型定义时(例如在引用的库中),您看到的是 type元数据,一个与包含的成员一起预览。

    所有这些成员都确实使用常规 C# 语法实现(除了课程摘要)。

    Reflector 之类的工具可以为 Visual Studio 添加扩展,并允许您反编译并查看实际代码。

    【讨论】:

    • 所以澄清一下,元数据是代码的表示,而不是实际的代码?
    • 是的,你看到的不是真正的代码,只是类型的定义。
    • 好的,谢谢,这回答了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2011-11-25
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多