zms9110750.InterfaceImplAsExtensionGenerator 0.2.0

dotnet add package zms9110750.InterfaceImplAsExtensionGenerator --version 0.2.0
                    
NuGet\Install-Package zms9110750.InterfaceImplAsExtensionGenerator -Version 0.2.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.InterfaceImplAsExtensionGenerator" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="zms9110750.InterfaceImplAsExtensionGenerator" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="zms9110750.InterfaceImplAsExtensionGenerator" />
                    
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.InterfaceImplAsExtensionGenerator --version 0.2.0
                    
#r "nuget: zms9110750.InterfaceImplAsExtensionGenerator, 0.2.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.InterfaceImplAsExtensionGenerator@0.2.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.InterfaceImplAsExtensionGenerator&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=zms9110750.InterfaceImplAsExtensionGenerator&version=0.2.0
                    
Install as a Cake Tool

简介

这个包可以为接口API生成扩展方法。

让显式实现的方法也可以调用。

主要API

引入命名空间zms9110750.InterfaceImplAsExtensionGenerator

  • ExtensionGlobalConfig:全局配置,作用于程序集
  • ExtensionSourceAttribute:接口配置,作用于接口
  • ExtensionForAttribute:类配置,作用于顶级非泛型静态类
  • ExtensionIncludeAttribute:成员配置,并强制包含该成员生成扩展
  • ExtensionIgnoreAttribute:强制不包含该成员

配置项

效果 全局配置 接口配置 类配置 成员配置 默认设置 备注
使用扩展语法 UseSyntax - - - 自动 -
附带public修饰 UsePublic UsePublic(bool) - - 跟随接口 全局配置使用枚举。接口配置使用bool,类配置自行为声明类添加修饰符
作用目标接口 - 特性附着的接口 构造器参数 - - -
类名 TypeNameSuffix ExtensionClassName 特性附着类 - Extension 全局配置仅设置后缀
类所在命名空间 NamespaceSuffix ExtensionClassNamespace 特性附着类 - - 全局配置仅设置后缀。后缀名即子命名空间。
实例参数名 InstanceParameterName InstanceParameterName InstanceParameterName InstanceParameterName instance 扩展方法的实例变量名可被成员配置影响。扩展块实例名仅能被接口配置和类配置影响。
成员名 - - - ReplacementMemberName 跟随成员名 扩展方法版本的访问器会有get_,set_,add_,remove_前缀。索引器总是get_Itemset_Item
包含的成员 DefaultGenerateMembers DefaultGenerateMembers DefaultGenerateMembers 附着成员强制包含 属性和方法 目前扩展块语法不支持索引器和事件。在支持的时候可以自行启用

配置枚举项

PublicModifier

全局配置的UsePublic成员枚举。

  • NoModifier:始终不添加
  • FollowInterface:跟随接口
  • Always:总是添加

GenerateMembers

所有配置的DefaultGenerateMembers成员枚举。 指示为哪些成员生成扩展。按位枚举。

  • Property:属性
  • Indexer:索引器
  • Event:事件
  • Method:方法
  • All:全部

ExtensionSyntaxVersion

全局配置的UseSyntax成员枚举。

  • Auto:如果当前项目语言>=c#14,以扩展块生成。否则以扩展方法生成。
  • ExtensionBlock:扩展块
  • ExtensionMethod:扩展方法
  • Both:生成基于#if NET10_OR_GREATER的条件编译。

边缘测试

  • 方法或属性返回refref readonly类型
  • 参数或成员名为关键字
  • 泛型名与声明处有同名泛型(泛型方法和泛型接口,泛型接口和嵌套泛型接口)
  • 参数有ref,in,out,params修饰
  • 泛型约束
  • 访问器独立访问权限修饰符(仅在publicinternal时区分)
  • 属性没有有效访问器(仅有init访问器。get访问器不存在或访问权限过低)
  • internal protected视为internal
  • 区分有返回值方法和void方法
  • 参数默认值
  • 没有对应字段或多个对应字段的枚举默认值
  • 生成对源成员的文档注释引用

示例

namespace Hello.World
{
    [ExtensionFor(typeof(IList<int>))]
    [ExtensionFor(typeof(IList<>))]
    public static partial class Apple
    {
        [ExtensionSource]
        public interface IOrange
        {
            string GetSet { get; set; }
#if NET10_0
            string GetInit { get; init; }
            string PropertyGet { protected get; set; }
#endif
            int this[string index] { get; }
            internal void Hello<T1, T2, T3>(out T1 t)
                 where T1 : class, IList<int>, new()
                 where T2 : struct, ISet<T2>
                 where T3 : class, IDog<T3>, new();

            ref string Hello(out string t, params int[] i);

            internal void Collection<T>(ref int a, int b = 10, string s = "hello\t" + @"  {你好}""{哈哈}  ", bool d = true | true ^ true, float f = 34, IDog<T>.Cat<T>.Color c = IDog<T>.Cat<T>.Color.B, params int[] p);
        }
    }
    interface IDog<T>
    {
        public interface Cat<T>
        {
            @class @event<@int>(@int @string);
            public enum Color
            {
                Red = 1, Green = 2, Blue = 4, Ambient = 8, Cyan = 16, DarkBlue = 32,
                A = 33,
                B = 33
            }
        }
    }
    class @class { }
}
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 (1)

Showing the top 1 NuGet packages that depend on zms9110750.InterfaceImplAsExtensionGenerator:

Package Downloads
zms9110750.TreeCollection

一个泛型集合类型。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 104 3/7/2026
0.1.1.3 213 9/29/2025
0.1.0 154 9/26/2025

新的边缘处理
- 可以处理方法或属性返回`ref`和`ref readonly`类型
- 可以处理参数或成员名为关键字
- 可以处理泛型名与声明处有同名泛型(泛型方法和泛型接口,泛型接口和嵌套泛型接口)

破坏性更新,API名字和命名空间迁移。
- 现在可以基于语法版本自动决定生成扩展块或扩展方法。