跳過到頁腳內容
.NET幫助

C# 內部 (對開發者如何運作)

在管理靜態 void 主程式內的表單類別、方法和屬性的可見性時,存取修改器在 C# 元件式開發程式語言中是不可或缺的。 在建立模組化且可維護的圖形化使用者介面時,其中一個相當相關的存取修改器就是內部。 本文將討論 C# 內部的概念,以及 IronPDF 的幾個實用應用程式,IronPDF 是用於管理 PDF 文件的彈性 C# 框架。

如何在 C# 基於元件的開發中使用內部成員

1.建立 C# 專案。 2.瞭解內部存取修改器。 3.將"內部"應用於成員。 4.在程式集層級組織程式碼。 5.使用同一組合內的內部成員。 6.編譯程式碼。

瞭解內部存取修改器

C# 中的內部關鍵字/存取修改器會限制某一類型或成員的可見性,使其與同一程序集內的其他成員的可見性相同。 這意味著任何類別,無論是衍生類別、方法或標示為內部的屬性,都可以由同一程序集內的其他類型存取,但對程序集以外的類型則不可用。 這種程度的存取控制對於封裝是非常重要的,因為它可以讓您指定實作的具體細節,這些細節應該在相同的程式集內以私有的方式專用。

在 C# 中,您可以下列方式使用內部修改器:

內部類別

透過使用 internal.NET 來宣告一個只能在同一個程序集內使用的類別。

// Assembly1
internal class InternalClass
{
    // Members of InternalClass
}
// Assembly1
internal class InternalClass
{
    // Members of InternalClass
}
' Assembly1
Friend Class InternalClass
	' Members of InternalClass
End Class
$vbLabelText   $csharpLabel

內部類別成員

透過套用內部程式集,將類別成員(例如欄位、屬性和方法)的可見性限制在相同的程式集內。

// Assembly1
internal class MyClass
{
    internal static int InternalField;
    internal void InternalMethod() { }
}
// Assembly1
internal class MyClass
{
    internal static int InternalField;
    internal void InternalMethod() { }
}
' Assembly1
Friend Class [MyClass]
	Friend Shared InternalField As Integer
	Friend Sub InternalMethod()
	End Sub
End Class
$vbLabelText   $csharpLabel

內部介面

透過使用內部存取修改器,宣告只能在同一個程序集內存取的介面。

// Assembly1
internal interface IInternalInterface
{
    // Interface members
}
// Assembly1
internal interface IInternalInterface
{
    // Interface members
}
' Assembly1
Friend Interface IInternalInterface
	' Interface members
End Interface
$vbLabelText   $csharpLabel

內部巢狀類別

宣告一個只能在相同的程式集內使用 internal 來存取的巢狀類別。

// Assembly1
public class OuterClass
{
    internal class InternalNestedClass
    {
        // Members of InternalNestedClass
    }
}
// Assembly1
public class OuterClass
{
    internal class InternalNestedClass
    {
        // Members of InternalNestedClass
    }
}
' Assembly1
Public Class OuterClass
	Friend Class InternalNestedClass
		' Members of InternalNestedClass
	End Class
End Class
$vbLabelText   $csharpLabel

內部組裝

透過在程序集層級應用內部,限制從外部程序集存取整個程序集。

using System.Runtime.CompilerServices;

// Allowing "ExternalAssembly" to access internal members of this assembly
[assembly: InternalsVisibleTo("ExternalAssembly")]
using System.Runtime.CompilerServices;

// Allowing "ExternalAssembly" to access internal members of this assembly
[assembly: InternalsVisibleTo("ExternalAssembly")]
Imports System.Runtime.CompilerServices

' Allowing "ExternalAssembly" to access internal members of this assembly
<Assembly: InternalsVisibleTo("ExternalAssembly")>
$vbLabelText   $csharpLabel

在開發和測試期間,可以使用 InternalsVisibleTo 屬性使內部存取修飾符可供指定的外部組件存取。

// Assembly A
public class MyClassA
{
    internal void MyInternalMethod()
    {
        // Implementation details only accessible within Assembly A
    }
}

// Assembly B
public class MyClassB
{
    void SomeMethod()
    {
        MyClassA myObject = new MyClassA();
        myObject.MyInternalMethod(); // This will result in a compilation error
    }
}
// Assembly A
public class MyClassA
{
    internal void MyInternalMethod()
    {
        // Implementation details only accessible within Assembly A
    }
}

// Assembly B
public class MyClassB
{
    void SomeMethod()
    {
        MyClassA myObject = new MyClassA();
        myObject.MyInternalMethod(); // This will result in a compilation error
    }
}
' Assembly A
Public Class MyClassA
	Friend Sub MyInternalMethod()
		' Implementation details only accessible within Assembly A
	End Sub
End Class

' Assembly B
Public Class MyClassB
	Private Sub SomeMethod()
		Dim myObject As New MyClassA()
		myObject.MyInternalMethod() ' This will result in a compilation error
	End Sub
End Class
$vbLabelText   $csharpLabel

由於 MyInternalMethod 在本例中被指定為內部成員,因此只能在組件 A 中存取。 如果您嘗試從組合 B 存取此功能,將會發生編譯錯誤。

結合受保護和內部存取修飾子的結果是受保護的內部存取修飾子。 透過受保護的內部複合存取修改器,一個成員(方法、屬性或欄位)或一種類型(類、介面或委託)可以在其程式集內外被派生類型存取。 受保護的內部存取等級提供了受保護與內部存取等級分別給予的可見度之間的平衡。

IronPDF

使用 C# 程式語言,IronPDF Official Site 是一個 .NET 函式庫,可讓開發人員生成、編輯和修改 PDF 文件。 它提供了一系列工具和功能,以多種方式與 PDF 檔案互動,包括從 HTML 建立 PDF、將 HTML 轉換為 PDF、合併或分割 PDF 文件,以及在現有的 PDF 上附加註解、文字和照片。

安裝 IronPDF

取得 IronPDF 函式庫; 這是未來補丁所需要的。 在套件管理員控制台中輸入以下指令即可完成:

Install-Package IronPdf

C# Internal (How It Works For Developers):圖 1 - 安裝 IronPDF

使用 NuGet Package Manager 搜尋套件"IronPDF"是另一個選擇。我們可以從這個與 IronPDF 相關的所有 NuGet 套件清單中選擇並下載所需的套件。

C# Internal (How It Works For Developers):圖 2 - IronPDF 套件

IronPDF 擅長於 HTML 至 PDF 的轉換,可確保精確保留原始版面與樣式。 它非常適合從網頁內容(如報告、發票和文件)建立 PDF。 IronPDF 支援 HTML 檔案、URL 和原始 HTML 字串,可輕鬆製作高品質的 PDF 文件。

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()

		' 1. Convert HTML String to PDF
		Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
		Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
		pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")

		' 2. Convert HTML File to PDF
		Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
		Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
		pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")

		' 3. Convert URL to PDF
		Dim url = "http://ironpdf.com" ' Specify the URL
		Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
		pdfFromUrl.SaveAs("URLToPDF.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF 的功能

*將 HTML 轉換為 PDF:使用 IronPDF,您可以從任何類型的 HTML 資訊(包括文件、URL 和 HTML 程式碼字串)建立 PDF 文件。

  • PDF 產生:使用 C# 程式語言,您可以以程式設計方式為 PDF 文件添加文字、圖形和其他元件。
  • PDF 操作: IronPDF 提供將 PDF 文件分割成多個文件、將多個 PDF 文件合併成一個文件以及修改現有 PDF 的功能。
  • PDF 表單:該程式庫在需要收集和處理表單資料的情況下非常有用,因為它允許使用者建立和填寫 PDF 表單。 *安全功能:* PDF 文件可使用 IronPDF 進行密碼和權限保護和加密。 文字擷取:** IronPDF 可用於從 PDF 檔案中擷取文字。

使用 IronPDF 對 PDF 進行封裝處理。

IronPDF 提供了大量生成、修改和處理 PDF 文件的功能。 透過將 PDF 處理程式碼封閉在內部類別或方法中,可以將實作細節隱藏在程序集邊界之後。 要瞭解 IronPDF 的更多資訊,請參閱 IronPDF 文件

檢視以下情況:

// Assembly A (PDFHandlingLibrary)
internal class PdfProcessor
{
    internal void AddWatermark(IronPdf.PdfDocument pdfDocument, string watermarkText)
    {
        // Implementation details for adding a watermark using IronPDF
    }

    internal IronPdf.PdfDocument MergePdfDocuments(IEnumerable<IronPdf.PdfDocument> pdfDocuments)
    {
        // Implementation details for merging PDF documents using IronPDF
        IronPdf.PdfDocument mergedPdfDocument = new IronPdf.PdfDocument();
        // Logic to merge documents
        return mergedPdfDocument;
    }
}

// Assembly B (MainApplication)
public class MainClass
{
    void ProcessPdfDocuments()
    {
        // Create an instance of the PdfProcessor within the same assembly
        PdfProcessor pdfProcessor = new PdfProcessor();

        // Assuming pdfDocumentList is defined
        IEnumerable<IronPdf.PdfDocument> pdfDocumentList = new List<IronPdf.PdfDocument>();

        // Accessing internal methods within the same assembly is allowed
        pdfProcessor.AddWatermark(new IronPdf.PdfDocument(), "Confidential");
        IronPdf.PdfDocument mergedPdf = pdfProcessor.MergePdfDocuments(pdfDocumentList);
    }
}
// Assembly A (PDFHandlingLibrary)
internal class PdfProcessor
{
    internal void AddWatermark(IronPdf.PdfDocument pdfDocument, string watermarkText)
    {
        // Implementation details for adding a watermark using IronPDF
    }

    internal IronPdf.PdfDocument MergePdfDocuments(IEnumerable<IronPdf.PdfDocument> pdfDocuments)
    {
        // Implementation details for merging PDF documents using IronPDF
        IronPdf.PdfDocument mergedPdfDocument = new IronPdf.PdfDocument();
        // Logic to merge documents
        return mergedPdfDocument;
    }
}

// Assembly B (MainApplication)
public class MainClass
{
    void ProcessPdfDocuments()
    {
        // Create an instance of the PdfProcessor within the same assembly
        PdfProcessor pdfProcessor = new PdfProcessor();

        // Assuming pdfDocumentList is defined
        IEnumerable<IronPdf.PdfDocument> pdfDocumentList = new List<IronPdf.PdfDocument>();

        // Accessing internal methods within the same assembly is allowed
        pdfProcessor.AddWatermark(new IronPdf.PdfDocument(), "Confidential");
        IronPdf.PdfDocument mergedPdf = pdfProcessor.MergePdfDocuments(pdfDocumentList);
    }
}
' Assembly A (PDFHandlingLibrary)
Friend Class PdfProcessor
	Friend Sub AddWatermark(ByVal pdfDocument As IronPdf.PdfDocument, ByVal watermarkText As String)
		' Implementation details for adding a watermark using IronPDF
	End Sub

	Friend Function MergePdfDocuments(ByVal pdfDocuments As IEnumerable(Of IronPdf.PdfDocument)) As IronPdf.PdfDocument
		' Implementation details for merging PDF documents using IronPDF
		Dim mergedPdfDocument As New IronPdf.PdfDocument()
		' Logic to merge documents
		Return mergedPdfDocument
	End Function
End Class

' Assembly B (MainApplication)
Public Class MainClass
	Private Sub ProcessPdfDocuments()
		' Create an instance of the PdfProcessor within the same assembly
		Dim pdfProcessor As New PdfProcessor()

		' Assuming pdfDocumentList is defined
		Dim pdfDocumentList As IEnumerable(Of IronPdf.PdfDocument) = New List(Of IronPdf.PdfDocument)()

		' Accessing internal methods within the same assembly is allowed
		pdfProcessor.AddWatermark(New IronPdf.PdfDocument(), "Confidential")
		Dim mergedPdf As IronPdf.PdfDocument = pdfProcessor.MergePdfDocuments(pdfDocumentList)
	End Sub
End Class
$vbLabelText   $csharpLabel

在這個例子中,程式集 A 的 PdfProcessor 類別使用 IronPDF 來封裝 PDF 處理程式碼。 由於這些方法被指定為內部方法,因此只能由同一程序集的其他內部成員存取。 彙編 B 的 MainClass 可以輕鬆使用這些內部函數。 要瞭解 IronPDF 程式碼的更多資訊,請參閱 IronPDF HTML to PDF Example

C# Internal (How It Works For Developers):圖 3 - 輸出

結論

最後,C# 內部修改器提供了強大的控制能力,可控制哪些類型和成員在程序集內是可見的。 與 IronPDF 配合使用時,有助於建立安全、模組化且可維護的應用程式。 您可以在抽象性、安全性和可用性之間取得折衷,將 IronPDF 相關的程式碼封閉在內部的類別或方法中。 在使用 IronPDF 這類管理 PDF 文件處理等基本功能的函式庫時,尤其需要掌握封裝與有限存取的概念,以促進 C# 應用程式的穩定與可擴充架構。

IronPDF 的 $999 輕量級套裝組合包含非常強大的授權、重新設計選項和更長的程式支援期限。 在水印測試期間,客戶可以在真實的應用程式設定中測試該項目。 進一步了解 IronPDF授權,以瞭解其優點、審批流程和草擬形式。 請參閱 Iron Software 網站,瞭解更多資訊。

常見問題解答

C# 中的 internal 關鍵字如何增強封裝性?

C# 中的 internal 關鍵字透過將類型或成員的可見性限制在同一組件內部來增強封裝性,從而防止外部組件存取內部實作細節。這促進了更清晰的架構和程式碼庫的可維護性。

C# 中 InternalsVisibleTo 屬性的作用是什麼?

C# 中的 InternalsVisibleTo 屬性允許您將一個組件的內部成員的存取權授予指定的外部組件。這對於測試特別有用,因為它使測試組件能夠存取內部成員進行驗證,同時在部署期間保持封裝性。

internal 存取修飾符可以用於 C# 的 PDF 處理嗎?

可以,internal 存取修飾符可與像 IronPDF 這樣的庫結合使用,以在組件內封裝 PDF 處理邏輯。這確保了敏感的 PDF 操作功能不會向外部公開,從而提高安全性和可維護性。

C# 中 internal 關鍵字的一些常見使用情境是什麼?

C# 中 internal 關鍵字的常見使用情境包括限制對內部類別、方法和屬性的存取,特別是在建構模塊化組件如圖形使用者介面時,或在像 IronPDF 這樣的庫中封裝商業邏輯以進行 PDF 文件管理時。

如何使用 C# 將 HTML 轉換為 PDF?

您可以使用 C# 利用 IronPDF 將 HTML 轉換為 PDF。該庫提供了像 RenderHtmlAsPdf 這樣的方法將 HTML 字串轉換為 PDF 文件,以及 RenderHtmlFileAsPdf 用於直接轉換 HTML 文件。

在庫開發中使用內部存取修飾符有什麼好處?

在庫開發中使用內部存取修飾符提供了好處,例如透過隱藏外部組件的敏感實作細節來增強安全性,並透過將複雜邏輯封裝在庫內部來改善可維護性,只公開必要的介面。

如何在 PDF 處理過程中使用 IronPDF 確保文件安全?

IronPDF 可透過應用如密碼保護、加密和存取控制等功能來確保 PDF 處理過程中的文件安全,確保只有授權用戶可以查看或修改在安全環境中生成或操作的 PDF 文件。

Jacob Mellor, Team Iron 首席技術官
首席技術官

Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我