GFramework/GFramework.SourceGenerators.Common/generator/MetadataAttributeClassGeneratorBase.cs
GeWuYou b49079de3e style(coding-style): 统一代码风格并优化文档格式
- 移除多余using语句和空行,统一代码缩进格式
- 优化注释文档中的缩进和对齐格式
- 简化条件语句和方法实现,提升代码可读性
- 重构协程系统相关类的字段和方法定义格式
- 更新架构服务中容器访问方式的实现
- 调整异步操作类的属性和方法组织结构
- [skip ci]
2026-01-27 20:30:04 +08:00

40 lines
1.4 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.

using Microsoft.CodeAnalysis;
namespace GFramework.SourceGenerators.Common.generator;
/// <summary>
/// 元数据属性类生成器基类,用于基于元数据名称解析特性的抽象基类
/// </summary>
public abstract class MetadataAttributeClassGeneratorBase
: AttributeClassGeneratorBase
{
/// <summary>
/// 获取特性元数据名称的抽象属性
/// </summary>
protected abstract string AttributeMetadataName { get; }
/// <summary>
/// 根据元数据名称解析指定符号上的特性
/// </summary>
/// <param name="compilation">编译对象,用于获取类型信息</param>
/// <param name="symbol">命名类型符号,用于查找其上的特性</param>
/// <returns>如果找到匹配的特性则返回AttributeData对象否则返回null</returns>
protected override AttributeData? ResolveAttribute(
Compilation compilation,
INamedTypeSymbol symbol)
{
// 通过元数据名称获取特性符号
var attrSymbol =
compilation.GetTypeByMetadataName(AttributeMetadataName);
if (attrSymbol is null)
return null;
// 在符号的所有特性中查找与目标特性符号匹配的第一个特性
return symbol.GetAttributes()
.FirstOrDefault(a =>
SymbolEqualityComparer.Default.Equals(
a.AttributeClass,
attrSymbol));
}
}