Files
Fishing2Server/Fantasy/Fantasy.Net/Fantasy.SourceGenerator/Common/IsExternalInit.cs
2025-10-29 17:59:43 +08:00

34 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ReSharper disable CheckNamespace
//------------------------------------------------------------------------------
// 这个 IsExternalInit 类是一个 polyfill兼容性填充
// 用于在 .NET Standard 2.0 或较低版本的框架中启用 C# 9.0 的 init 访问器和 record 类型功能。
// 为什么需要它?
// C# 9.0 引入了 init 访问器(只在初始化时可设置的属性)
// 编译器在编译 init 属性时,会查找 IsExternalInit 类型
// 示例:
// public class Person
// {
// public string Name { get; init; } // 需要 IsExternalInit
// public int Age { get; init; }
// }
// 使用
// var person = new Person { Name = "Alice", Age = 30 };
// person.Name = "Bob"; // ❌ 编译错误init 属性只能在对象初始化时设置
// 不定义会怎样?
// 如果目标框架是 netstandard2.0 但没定义 IsExternalInit编译器会报错
// error CS0518: Predefined type 'System.Runtime.CompilerServices.IsExternalInit'
// is not defined or imported
// 实际应用场景
// 在 IncrementalGenerator 中,你可能会生成或使用带 init 的代码
//------------------------------------------------------------------------------
#if NETSTANDARD2_0 || NETFRAMEWORK
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Polyfill for C# 9.0 record types in netstandard2.0
/// </summary>
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static class IsExternalInit { }
}
#endif