zms9110750.MetaSourceGenerator.AttributeFactory 0.1.0

dotnet add package zms9110750.MetaSourceGenerator.AttributeFactory --version 0.1.0
                    
NuGet\Install-Package zms9110750.MetaSourceGenerator.AttributeFactory -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="zms9110750.MetaSourceGenerator.AttributeFactory" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="zms9110750.MetaSourceGenerator.AttributeFactory" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="zms9110750.MetaSourceGenerator.AttributeFactory" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add zms9110750.MetaSourceGenerator.AttributeFactory --version 0.1.0
                    
#r "nuget: zms9110750.MetaSourceGenerator.AttributeFactory, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package zms9110750.MetaSourceGenerator.AttributeFactory@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=zms9110750.MetaSourceGenerator.AttributeFactory&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=zms9110750.MetaSourceGenerator.AttributeFactory&version=0.1.0
                    
Install as a Cake Tool

简介

为特性类生成基于AttributeData的创建实例

主要API

引入命名空间zms9110750.MetaSourceGenerator.AttributeFactory

  • FromAttributeDataAttribute:为附着的特性生成工厂方法

注意事项

  • 方法名始终为Create,没有避让措施。
  • 有可访问的类型为TypeType[]字段时,会自动生成ITypeSymbolITypeSymbol[]属性。属性为源属性名后加Symbol后缀。
    • 没有避让措施。
  • 在源数据中,特性参数里的TypeType[]在分析器中都会解析为ITypeSymbolITypeSymbol[]
    • 为属性赋值时会自动转为对Symbol赋值
    • 但构造器不会自动生成。仅在有对应重载的构造器时,才会被调用。
  • 会创建一个FullName名子的常量。用于ForAttributeWithMetadataName方法使用。没有避让措施。

示例

[FromAttributeData]
public partial class ExtensionForAttribute : Attribute
{
    public Type? AppendType { get; }

    public string? InstanceParameterName { get; set; }

    public GenerateMembers DefaultGenerateMembers { get; set; }

    public ExtensionForAttribute(Type appendType)
    {
        AppendType = appendType;
    }
    internal ExtensionForAttribute(INamedTypeSymbol symbol)
    {
        AppendTypeSymbol = symbol.IfThen(symbol.IsUnboundGenericType, symbol.OriginalDefinition); 
    }
}

生成

partial class ExtensionForAttribute
{
    internal const string FullName = "zms9110750.InterfaceImplAsExtensionGenerator.ExtensionForAttribute";
    
    /// <summary>
    /// 自动生成。为<see cref="AppendType"/>在<see cref="AttributeData"/>中的<see cref="ITypeSymbol"/>表现形式
    /// </summary>
    internal global::Microsoft.CodeAnalysis.ITypeSymbol AppendTypeSymbol{ get; set; }
    
    public static global::zms9110750.InterfaceImplAsExtensionGenerator.ExtensionForAttribute Create(global::Microsoft.CodeAnalysis.AttributeData data)
    {
        if (data == null)
        {
            return null;
        }
        var format = global::Microsoft.CodeAnalysis.SymbolDisplayFormat.FullyQualifiedFormat;
        if (data.AttributeClass.ToDisplayString(format) != "global::zms9110750.InterfaceImplAsExtensionGenerator.ExtensionForAttribute")
        {
            return null;
        }
        
        global::zms9110750.InterfaceImplAsExtensionGenerator.ExtensionForAttribute value = null;
        switch (string.Join("|", global::System.Linq.Enumerable.Select(data.AttributeConstructor.Parameters, p => p.Type.ToDisplayString(format))))
        {
            case "global::System.Type":
                return null;
            default:
                return null;
            }
            foreach (var symbol in data.NamedArguments)
            {
                switch (symbol.Key)
                {
                    case "AppendType":
                        value.AppendTypeSymbol = (global::Microsoft.CodeAnalysis.ITypeSymbol)symbol.Value.Value;
                        break;
                    case "InstanceParameterName":
                        value.InstanceParameterName = (string)symbol.Value.Value;
                        break;
                    case "DefaultGenerateMembers":
                        value.DefaultGenerateMembers = (global::zms9110750.InterfaceImplAsExtensionGenerator.Config.GenerateMembers)symbol.Value.Value;
                        break;
                }
            }
            return value;
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 99 3/7/2026