.NET 帮助 C# 区别联合(开发者用法) Jacob Mellor 已更新:2025年7月28日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 可辨识联合,也称为标记联合或和类型,是一种强大的工具,用于建模可以采取不同形式但具有明确且有限可能情况的数据。 虽然C#不像其他一些语言(例如F#或Rust)那样具有原生的可辨识联合,但您可以使用语言中的几种技术模拟可辨识联合。 在本教程中,我们将深入研究可辨识联合,如何在C#中实现它们,并通过IronPDF库探讨其实用用例。 什么是可辨识联合? 简而言之,可辨识联合是一种可以包含几种预定义形式或值的类型。 它提供了一种创建类型安全结构的方法,能够封装不同类型或值,同时在编译时确保只有有效的情况被处理。 想象一个您希望表示操作结果的场景。 操作可能成功,返回一些数据,或者失败,返回一条错误消息。 可辨识联合可以让您在单一类型中表示这两种可能的结果。 Example: Simulating Discriminated Union in C# 以下是如何使用类结构在C#中模拟可辨识联合的示例: // Define an abstract base class representing the operation result. public abstract class OperationResult<t> { // Private constructor to ensure the class cannot be instantiated directly. private OperationResult() { } // Nested class representing a successful operation result. public sealed class Success : OperationResult<t> { public T Value { get; } public Success(T value) => Value = value; public override string ToString() => $"Success: {Value}"; } // Nested class representing a failed operation result. public sealed class Failure : OperationResult<t> { public string Error { get; } public Failure(string error) => Error = error; public override string ToString() => $"Failure: {Error}"; } // Factory method to create a successful operation result. public static OperationResult<t> CreateSuccess(T value) => new Success(value); // Factory method to create a failed operation result. public static OperationResult<t> CreateFailure(string error) => new Failure(error); } // Define an abstract base class representing the operation result. public abstract class OperationResult<t> { // Private constructor to ensure the class cannot be instantiated directly. private OperationResult() { } // Nested class representing a successful operation result. public sealed class Success : OperationResult<t> { public T Value { get; } public Success(T value) => Value = value; public override string ToString() => $"Success: {Value}"; } // Nested class representing a failed operation result. public sealed class Failure : OperationResult<t> { public string Error { get; } public Failure(string error) => Error = error; public override string ToString() => $"Failure: {Error}"; } // Factory method to create a successful operation result. public static OperationResult<t> CreateSuccess(T value) => new Success(value); // Factory method to create a failed operation result. public static OperationResult<t> CreateFailure(string error) => new Failure(error); } $vbLabelText $csharpLabel 在这个例子中,OperationResult<t>是一个抽象类,表示我们的判别联合类型。 它可以是一个值类型为Failure。 私有构造函数确保这种类的实例只能通过预定义情况下创建。 使用模式匹配与可辨识联合 C#提供了强大的模式匹配功能,非常适合与可辨识联合一起使用。 让我们通过一个使用switch表达式处理不同情况的方法来扩展我们的OperationResult<t>示例。 // Method to handle the result using pattern matching. public string HandleResult(OperationResult<int> result) => result switch { OperationResult<int>.Success success => $"Operation succeeded with value: {success.Value}", OperationResult<int>.Failure failure => $"Operation failed with error: {failure.Error}", _ => throw new InvalidOperationException("Unexpected result type") }; // Method to handle the result using pattern matching. public string HandleResult(OperationResult<int> result) => result switch { OperationResult<int>.Success success => $"Operation succeeded with value: {success.Value}", OperationResult<int>.Failure failure => $"Operation failed with error: {failure.Error}", _ => throw new InvalidOperationException("Unexpected result type") }; $vbLabelText $csharpLabel 这里的switch表达式处理Failure的情况。 这确保在编译时覆盖所有可能情况,提供类型安全性并减少运行时错误的风险。 可辨识联合的扩展方法 您可以使用扩展方法扩展可辨识联合的功能。 例如,让我们为我们的OperationResult<t>创建一个扩展方法,以确定结果是否成功: // Static class to hold extension methods for OperationResult<t>. public static class OperationResultExtensions { // Extension method to check if the operation result indicates success. public static bool IsSuccess<t>(this OperationResult<t> result) => result is OperationResult<t>.Success; } // Static class to hold extension methods for OperationResult<t>. public static class OperationResultExtensions { // Extension method to check if the operation result indicates success. public static bool IsSuccess<t>(this OperationResult<t> result) => result is OperationResult<t>.Success; } $vbLabelText $csharpLabel 这个静态方法检查结果是否是Success情况的一个实例。 Native Support for Discriminated Unions in C# C#不像其他一些语言那样对可辨识联合提供原生支持,但社区中正在进行关于添加该功能的讨论。 原生可辨识联合将更易于定义和使用联合类型,而无需依赖类层次结构。 编译器错误和类型安全 可辨识联合的一个主要优点是它们提供的类型安全性。由于所有可能的情况在编译时已知,编译器可以确保所有情况都得到处理。 这导致更少的运行时错误,使代码更不容易出错。 例如,如果您忘记在switch语句中处理某个特定情况,编译器会产生一个错误,提示您处理缺失的情况。 这在处理具有多种可能情况的复杂数据结构时尤其有用。 Using IronPDF with Discriminated Unions in C# IronPDF是一个C# PDF库,帮助开发人员从HTML创建PDF文件,并允许他们轻松修改PDF文件。 在C#中处理PDF时,您可以将IronPDF与可辨识联合结合使用,以处理在生成或处理PDF文件时的不同场景。 例如,您可能有一个成功生成PDF或遇到错误的过程。 可辨识联合允许您清晰地建模此过程。 让我们创建一个简单的示例,使用IronPDF生成PDF并将结果作为可辨识联合返回。 // Using directives for necessary namespaces. using IronPdf; using System; // Define an abstract base class representing the PDF generation result. public abstract class PdfResult { // Private constructor to ensure the class cannot be instantiated directly. private PdfResult() { } // Nested class representing a successful PDF generation result. public sealed class Success : PdfResult { public PdfDocument Pdf { get; } public Success(PdfDocument pdf) => Pdf = pdf; public override string ToString() => "PDF generation succeeded"; } // Nested class representing a failed PDF generation result. public sealed class Failure : PdfResult { public string ErrorMessage { get; } public Failure(string errorMessage) => ErrorMessage = errorMessage; public override string ToString() => $"PDF generation failed: {ErrorMessage}"; } // Factory method to create a successful PDF result. public static PdfResult CreateSuccess(PdfDocument pdf) => new Success(pdf); // Factory method to create a failed PDF result. public static PdfResult CreateFailure(string errorMessage) => new Failure(errorMessage); } // Class to generate PDFs using IronPDF. public class PdfGenerator { // Method to generate a PDF from HTML content and return the result as a PdfResult. public PdfResult GeneratePdf(string htmlContent) { try { // Create a new ChromePdfRenderer instance. var renderer = new ChromePdfRenderer(); // Attempt to render the HTML content as a PDF. var pdf = renderer.RenderHtmlAsPdf(htmlContent); // Return a success result with the generated PDF. return PdfResult.CreateSuccess(pdf); } catch (Exception ex) { // Return a failure result with the error message if an exception occurs. return PdfResult.CreateFailure(ex.Message); } } } // Using directives for necessary namespaces. using IronPdf; using System; // Define an abstract base class representing the PDF generation result. public abstract class PdfResult { // Private constructor to ensure the class cannot be instantiated directly. private PdfResult() { } // Nested class representing a successful PDF generation result. public sealed class Success : PdfResult { public PdfDocument Pdf { get; } public Success(PdfDocument pdf) => Pdf = pdf; public override string ToString() => "PDF generation succeeded"; } // Nested class representing a failed PDF generation result. public sealed class Failure : PdfResult { public string ErrorMessage { get; } public Failure(string errorMessage) => ErrorMessage = errorMessage; public override string ToString() => $"PDF generation failed: {ErrorMessage}"; } // Factory method to create a successful PDF result. public static PdfResult CreateSuccess(PdfDocument pdf) => new Success(pdf); // Factory method to create a failed PDF result. public static PdfResult CreateFailure(string errorMessage) => new Failure(errorMessage); } // Class to generate PDFs using IronPDF. public class PdfGenerator { // Method to generate a PDF from HTML content and return the result as a PdfResult. public PdfResult GeneratePdf(string htmlContent) { try { // Create a new ChromePdfRenderer instance. var renderer = new ChromePdfRenderer(); // Attempt to render the HTML content as a PDF. var pdf = renderer.RenderHtmlAsPdf(htmlContent); // Return a success result with the generated PDF. return PdfResult.CreateSuccess(pdf); } catch (Exception ex) { // Return a failure result with the error message if an exception occurs. return PdfResult.CreateFailure(ex.Message); } } } $vbLabelText $csharpLabel Failure。 Failure情况则持有一个错误信息。 PdfResult返回。 如果PDF生成成功,它返回包含生成的PDF的Success情况。 如果发生异常,它返回带有错误信息的Failure情况。 结论 C#中的可辨识联合提供了一种强大且灵活的方法来建模具有多种可能情况的数据。 虽然C#不支持可辨识联合,但您可以使用类层次结构、模式匹配和其他技术对其进行模拟。 生成的代码更加类型安全,更不容易出错,并且更易于维护。 IronPDF提供免费试用,帮助您免费体验软件。 您可以探索所有功能,并查看它们如何符合您的需求。 试用结束后,许可证价格从$799开始提供。 常见问题解答 如何在 C# 中创建判别联合? 您可以通过定义一个具有嵌套子类的抽象类在 C# 中创建判别联合。每个子类代表可能的情况,例如成功或错误状态,您可以使用模式匹配来处理这些情况。 IronPDF 库在处理判别联合中的作用是什么? IronPDF 库可以与判别联合一起使用以管理 PDF 生成结果。通过将这些结果建模为判别联合,您可以确保类型安全,并处理成功的 PDF 创建及任何发生的错误。 模式匹配如何增强 C# 中的判别联合? 模式匹配通过允许开发人员优雅地处理每种可能的情况来增强 C# 中的判别联合。通过模式匹配,您可以安全地管理不同的结果,确保在编译时涵盖所有场景。 为什么判别联合对 C# 中的 PDF 生成有利? 判别联合对于 C# 中的 PDF 生成有益,因为它们提供了一种结构化的方法来处理成功和错误情况。此方法可确保在编译时解决潜在问题,减少 PDF 创建期间的运行时错误。 可以在 C# 中扩展判别联合以获得额外的功能吗? 是的,可以使用扩展方法扩展判别联合以获取其他功能。这允许您添加自定义行为,例如检查 PDF 生成的成功状态,而无需更改基础结构。 是否可以在没有本机支持的情况下在 C# 中模拟判别联合? 是的,尽管 C# 没有本机支持判别联合,但可以使用类层次结构来模拟它们。可以使用一个抽象的基类和嵌套类来表示不同的可能结果,例如成功或失败的情况。 C# 开发人员如何有效地处理 PDF 生成中的错误? C# 开发人员可以通过使用判别联合来建模潜在结果来有效地处理 PDF 生成中的错误。此方法可确保在编译时解决错误,从而提高代码的可靠性和可维护性。 在 C# 项目中使用 IronPDF 与判别联合有何优势? 在 C# 项目中使用 IronPDF 与判别联合可以在 PDF 生成过程中提供强大的错误处理。这种组合可以清晰地区分成功的操作和错误,增强代码的安全性和可靠性。 判别联合如何促进 C# 中的类型安全? 判别联合通过确保在编译期间处理所有潜在情况来促进 C# 中的类型安全。这减少了运行时错误的可能性,使代码更具可预测性且更易于维护。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多 已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新2025年12月20日 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 C# HttpClient(开发者用法)C# 新 GUID(开发者用法)
已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多
已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多