gpt4 book ai didi

c# - 为什么我们只能访问 aspx 或 ascx 中的 Protected 和 Public 方法?

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:44 24 4
gpt4 key购买 nike

我有一个 ASP.net webform 项目,当我有一个 protectedpublic 方法时,我可以从我在 aspx 中的内联代码调用它们,但我不能调用私有(private) 方法。为什么?它的行为类似于 inheritance 类。与继承有关吗?

我的 ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewFamilyTree.aspx.cs" Inherits="Mobini.WebForms.ViewFamilyTree" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<%= GetHelloWorlds_Protected() %> <%-- Works --%>
<%= GetHelloWorlds_Public() %> <%-- Works --%>
<%= GetHelloWorlds_Private() %> <%-- Error: is inaccessible due to its protection level --%>
</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>

C#代码:

namespace Mobini.WebForms
{
public partial class ViewFamilyTree : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

private string GetHelloWorlds_Private()
{
return "Hello Worlds";
}

protected string GetHelloWorlds_Protected()
{
return "Hello Worlds";
}

public string GetHelloWorlds_Public()
{
return "Hello Worlds";
}
}
}

最佳答案

Why we only have access to Protected and Public methods in aspx or ascx?

对您的问题的简单回答是:是的,这是因为继承。由于在 aspx 文件中,有一个指令指定要继承的类,(通常是代码隐藏类)

这就引出了你的第二个问题:

But is an aspx file a class?

是的。在编译时,将为aspx 文件生成一个类。在 ASP.Net 2.0 的原始实现中,aspx 类和代码隐藏类是相同的部分类,如 .但后来由于灵 active 而改变了。

See: Codebehind and Compilation in ASP.NET 2.0

At this point, you may be wondering why the ASP.NET team bothered to use inheritance at all with this new codebehind model. ASP.NET could easily generate all of the control variable declarations in addition to the rendering methods from the .aspx file as a partial class which could then be merged with your simplified codebehind class. This is exactly how Windows Forms works in the .NET Framework 2.0. All of the designer-generated code is placed into a sibling partial class which is then merged with your application logic and event handlers into a single Form-derived class, creating a clean separation between machine-generated code and developer code without resorting to inheritance.

Well, it turns out that the original implementation of codebehind in ASP.NET 2.0 did exactly this—the codebehind class was just a partial class that was merged with the parsed .aspx file class definition. It was simple and effective, but unfortunately, not flexible enough. The problem with this model was that it was no longer possible to deploy the codebehind files in precompiled binary assemblies along with intact .aspx files since they now had to be compiled at the same time (a restriction when using partial classes is that all partial pieces of a class must be merged during a single compilation, and class definitions cannot span assemblies). This restriction was unacceptable to many developers as they were already used to being able to deploy binary codebehind assemblies along with intact .aspx files which could then be updated in place without having to recompile. This is, in fact, the exact model used by default in Visual Studio .NET 2003, and is thus very prevalent in practice.

As a result of reintroducing the inheritance model and shifting the partial class into the base class, .aspx files can now be deployed and compiled independently from the codebehind class. To complete the picture, you need some way to generate the sibling partial classes containing control variable declarations during compilation or deployment since this was always done in the past on demand in response to requests. Enter the ASP.NET compiler.

关于c# - 为什么我们只能访问 aspx 或 ascx 中的 Protected 和 Public 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com