extension-methods 相关问题

扩展方法是某些语言的语言特性,如Swift,Visual Basic.NET和C#。扩展方法使您可以向现有类型“添加”方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型。

Swift Dictionary 的非下标 getter 访问方法

我正在尝试覆盖自定义词典类型的默认下标函数。某些键不应该具有某些类型的值,所以我想允许/拒绝下标 sette 中的值...

回答 1 投票 0

将范围添加到集合中

今天一位同事问我如何将范围添加到集合中。他有一个继承自Collection的类。该类型有一个仅获取属性,该属性已包含一些项目。他是...

回答 10 投票 0

这些 Kotlin 函数等价吗?

我使用 Kotlin 的时间不长,但我遇到了使用这些模式的代码库,我想知道这 3 个扩展函数是否等效,或者行为上是否存在一些细微的差异?...

回答 1 投票 0

为什么 Kotlin 允许在没有支持字段的情况下委托扩展属性?

我明白为什么 Kotlin 不允许初始化扩展属性,例如 val SomeClass.anExtensionProperty = "不能这样做" 因为没有支持字段来保存该值。 ...

回答 1 投票 0

如何在 ASP.net Core c# 中创建自己的 ServiceCollectionExtension 并将依赖项作为参数传递

我尝试为我的服务“FdmMediator”创建自己的 ServiceCollectionExtension。 我想将服务“FdmMediator”注册为单例,并且需要传递一些参数。参数为

回答 1 投票 0

为什么MutableList没有一个返回MutableList的sorted()函数?

Kotlin 的索引集合 List 和 MutableList 可以使用一组函数进行排序,但其中一些函数(如排序())返回一个 List,而不是分别返回 List 和 MutableList。原因是什么

回答 1 投票 0

为什么 MutableList 没有排序函数?

Kotlin 的索引集合 List 和 MutableList 可以使用一组函数进行排序,但其中一些函数(如排序())返回一个 List,而不是分别返回 List 和 MutableList。原因是什么

回答 1 投票 0

具有协变返回类型的 Kotlin 泛型扩展方法

如何为具有协变返回类型的方法的类型层次结构编写通用扩展方法。 例如,以下类型: 密封接口 MyInterface { 有趣的地图():MyInterf...

回答 1 投票 0

“super”关键字不能在扩展中使用,因为扩展没有超类

我收到以下错误 'super' 关键字不能在扩展中使用,因为扩展没有超类。 有了这段代码, 动物类{ 最终字符串名称; 动物({必填...

回答 1 投票 0

如何在 Maui xaml 中使用方法扩展?

我有一个 Dto 对象: 公共类 SearchResultDto { 公共布尔 IsOne { 得到;放; }; 公共布尔IsTwo { 得到;放; } } 我有一个方法扩展: 公共静态类 SearchResultDtoExtens...

回答 1 投票 0

如何将 Linq 查询扩展方法与其文档中的签名相关联?

从 MSDN 获取此代码: 下面的代码示例演示了如何使用 Where(IEnumerable, Func) 进行过滤 一个序列。 列出水果= 新列表 {“苹果”,“

回答 2 投票 0

IEnumerable 与 Ilist - IsNullOrEmpty 扩展方法

我有一个自定义集合 IList 作为用户。当我尝试检查用户是否为空或为空时,我没有得到任何情报帮助(如 IsNullOrEmpty),所以我写了下面的扩展...

回答 2 投票 0

Rodzadra 的地理位置错误(Yii2 扩展)

这是我遇到的错误。我已经在 web.php 中添加了 rodzadra 的组件。我应该做什么来修复它? 谢谢。 编辑 !!!! 在 autoload_psr4.php 中添加扩展路径后,我收到了提到

回答 1 投票 0

如何在我的扩展方法中获取正确的 IServiceProvider 实例

我正在为要在主机构建器中使用的 api 编写扩展方法,并且我需要在其中获取 IServiceProvider 的实例。 我看到 builder.Services.AddDbContext() 有一个参数 (

回答 1 投票 0

编译时检测目标框架版本

我有一些使用扩展方法的代码,但使用VS2008中的编译器在.NET 2.0下编译。为了促进这一点,我必须声明 ExtensionAttribute: /// /// 我有一些使用扩展方法的代码,但使用 VS2008 中的编译器在 .NET 2.0 下编译。为了促进这一点,我必须声明 ExtensionAttribute: /// <summary> /// ExtensionAttribute is required to define extension methods under .NET 2.0 /// </summary> public sealed class ExtensionAttribute : Attribute { } 但是,我现在希望包含该类的库也可以在 .NET 3.0、3.5 和 4.0 下编译 - 没有“ExtensionAttribute 在多个位置定义”警告。 当目标框架版本是 .NET 2 时,是否可以使用任何编译时指令来仅包含 ExtensionAttribute? 带有“创建 N 个不同配置”的链接 SO 问题当然是一种选择,但是当我需要这个时,我只是添加了条件 DefineConstants 元素,因此在我的 Debug|x86 (例如)中,在 DEBUG;TRACE 的现有 DefineConstants 之后,我添加了这 2 个,检查在 csproj 文件的第一个 PropertyGroup 中设置的 TFV 中的值。 <DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' ">RUNNING_ON_4</DefineConstants> <DefineConstants Condition=" '$(TargetFrameworkVersion)' != 'v4.0' ">NOT_RUNNING_ON_4</DefineConstants> 显然,你不需要两者,但它只是提供 eq 和 ne 行为的示例 - #else 和 #elif 也可以正常工作:) class Program { static void Main(string[] args) { #if RUNNING_ON_4 Console.WriteLine("RUNNING_ON_4 was set"); #endif #if NOT_RUNNING_ON_4 Console.WriteLine("NOT_RUNNING_ON_4 was set"); #endif } } 然后我可以在目标 3.5 和 4.0 之间切换,它会做正确的事情。 我有一些改进目前给出的答案的建议: 使用 Version.CompareTo()。相等性测试不适用于尚未命名的更高版本的框架。例如 <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' "> 与您通常想要的 v4.5 或 v4.5.1 不匹配。 使用导入文件,这样这些附加属性只需定义一次。我建议将导入文件置于源代码控制之下,以便更改与项目文件一起传播,而不需要额外的努力。 在项目文件的末尾添加导入元素,以便它独立于任何配置特定的属性组。这还有一个好处是需要在项目文件中添加一行。 这是导入文件(VersionSpecificSymbols.Common.prop) <!-- ****************************************************************** Defines the Compile time symbols Microsoft forgot Modelled from https://msdn.microsoft.com/en-us/library/ms171464.aspx ********************************************************************* --> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) &gt;= 0">$(DefineConstants);NETFX_451</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) &gt;= 0">$(DefineConstants);NETFX_45</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) &gt;= 0">$(DefineConstants);NETFX_40</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) &gt;= 0">$(DefineConstants);NETFX_35</DefineConstants> <DefineConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) &gt;= 0">$(DefineConstants);NETFX_30</DefineConstants> </PropertyGroup> </Project> 将导入元素添加到项目文件 通过在末尾、标记之前添加来从 .csproj 文件中引用它。 … <Import Project="VersionSpecificSymbols.Common.prop" /> </Project> 您需要修复路径以指向放置此文件的公共/共享文件夹。 使用编译时符号 namespace VersionSpecificCodeHowTo { using System; internal class Program { private static void Main(string[] args) { #if NETFX_451 Console.WriteLine("NET_451 was set"); #endif #if NETFX_45 Console.WriteLine("NET_45 was set"); #endif #if NETFX_40 Console.WriteLine("NET_40 was set"); #endif #if NETFX_35 Console.WriteLine("NETFX_35 was set"); #endif #if NETFX_30 Console.WriteLine("NETFX_30 was set"); #endif #if NETFX_20 Console.WriteLine("NETFX_20 was set"); #else The Version specific symbols were not set correctly! #endif #if DEBUG Console.WriteLine("DEBUG was set"); #endif #if MySymbol Console.WriteLine("MySymbol was set"); #endif Console.ReadKey(); } } } 一个常见的“现实生活”例子 在.NET 4.0之前实现Join(字符串分隔符,IEnumerable字符串) // string Join(this IEnumerable<string> strings, string delimiter) // was not introduced until 4.0. So provide our own. #if ! NETFX_40 && NETFX_35 public static string Join( string delimiter, IEnumerable<string> strings) { return string.Join(delimiter, strings.ToArray()); } #endif 参考文献 属性函数 MSBuild 属性评估 我可以根据 .NET Framework 版本制作预处理器指令吗? C#中根据框架版本进行条件编译 属性组仅被覆盖,因此这会取消您对 DEBUG、TRACE 或任何其他设置的设置。 - 请参阅MSBuild 属性评估 此外,如果从命令行设置 DefineConstants 属性,则您在项目文件中对其执行的任何操作都无关紧要,因为该设置将变为全局只读。这意味着您对该值的更改会默默失败。 维护现有已定义常量的示例: <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v2.0' ">V2</CustomConstants> <CustomConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' ">V4</CustomConstants> <DefineConstants Condition=" '$(DefineConstants)' != '' And '$(CustomConstants)' != '' ">$(DefineConstants);</DefineConstants> <DefineConstants>$(DefineConstants)$(CustomConstants)</DefineConstants> 本节必须位于任何其他定义的常量之后,因为这些常量不太可能以附加方式设置 我只定义了这两个,因为这是我对我的项目最感兴趣的,ymmv。 另请参阅:常见的 MsBuild 项目属性 目标框架的预定义符号现已内置到 dotnet 工具和 VS 2017 及以后版本使用的 MSBuild 版本中。请参阅 https://learn.microsoft.com/en-us/dotnet/standard/frameworks#how-to-specify-a-target-framework 了解完整列表。 #if NET47 Console.WriteLine("Running on .Net 4.7"); #elif NETCOREAPP2_0 Console.WriteLine("Running on .Net Core 2.0"); #endif 我想贡献一个更新的答案来解决一些问题。 如果您设置 DefineConstants 而不是 CustomConstants,在进行某些框架版本切换后,您最终会在条件编译符号调试命令行中出现重复的条件常量(即:NETFX_451;NETFX_45;NETFX_40;NETFX_35;NETFX_30;NETFX_20;NETFX_35; NETFX_30;NETFX_20;)。 这是解决任何问题的 VersionSpecificSymbols.Common.prop。 <!-- ********************************************************************* Defines the Compile time symbols Microsoft forgot Modelled from https://msdn.microsoft.com/en-us/library/ms171464.aspx ********************************************************************* Author: Lorenzo Ruggeri ([email protected]) --> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Choose> <When Condition=" $(TargetFrameworkVersion) == 'v2.0' "> <PropertyGroup> <CustomConstants >$(CustomConstants);NETFX_20</CustomConstants> </PropertyGroup> </When> <When Condition=" $(TargetFrameworkVersion) == 'v3.0' "> <PropertyGroup> <CustomConstants >$(CustomConstants);NETFX_30</CustomConstants> <CustomConstants >$(CustomConstants);NETFX_20</CustomConstants> </PropertyGroup> </When> <When Condition=" $(TargetFrameworkVersion) == 'v3.5' "> <PropertyGroup> <CustomConstants >$(CustomConstants);NETFX_35</CustomConstants> <CustomConstants >$(CustomConstants);NETFX_30</CustomConstants> <CustomConstants >$(CustomConstants);NETFX_20</CustomConstants> </PropertyGroup> </When> <Otherwise> <PropertyGroup> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5.1')))) &gt;= 0">$(CustomConstants);NETFX_451</CustomConstants> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.5')))) &gt;= 0">$(CustomConstants);NETFX_45</CustomConstants> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('4.0')))) &gt;= 0">$(CustomConstants);NETFX_40</CustomConstants> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.5')))) &gt;= 0">$(CustomConstants);NETFX_35</CustomConstants> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('3.0')))) &gt;= 0">$(CustomConstants);NETFX_30</CustomConstants> <CustomConstants Condition="$([System.Version]::Parse('$(TargetFrameworkVersion.Substring(1))').CompareTo($([System.Version]::Parse('2.0')))) &gt;= 0">$(CustomConstants);NETFX_20</CustomConstants> </PropertyGroup> </Otherwise> </Choose> <PropertyGroup> <DefineConstants>$(DefineConstants);$(CustomConstants)</DefineConstants> </PropertyGroup> </Project> 使用反射来判断类是否存在。如果是,则动态地创建并使用它,否则使用可以定义的.Net2解决方法类,但不用于所有其他.net版本。 这是我用于 AggregateException 的代码,仅适用于 .Net 4 及更高版本: var aggregatException = Type.GetType("System.AggregateException"); if (aggregatException != null) // .Net 4 or greater { throw ((Exception)Activator.CreateInstance(aggregatException, ps.Streams.Error.Select(err => err.Exception))); } // Else all other non .Net 4 or less versions throw ps.Streams.Error.FirstOrDefault()?.Exception ?? new Exception("Powershell Exception Encountered."); // Sanity check operation, should not hit. 适用于 .NET 5+(和 .NET Core) 您可以在编译时使用Preprocessor symbols,更有趣的一点是也可以使用GREATER版本: #if NET8_0_OR_GREATER Console.WriteLine("Running on .Net 8 or greater versions"); #elif NET6_0_OR_GREATER Console.WriteLine("Running on .Net 6 or greater versions"); #elif NETCOREAPP3_1_OR_GREATER Console.WriteLine("Running on .Net Core 3.1 or greater versions"); #endif 在此处查看更多选项: https://learn.microsoft.com/en-us/dotnet/standard/frameworks#preprocessor-symbols

回答 7 投票 0

在两个chrome后台脚本之间发送消息

我有2个后台脚本,一个是background.js,另一个是background.ts,当我在构建文件中构建所有内容时,它看起来像这样: /建造 |-background.js |-助手 |--背景...

回答 1 投票 0

为什么非静态类中的静态方法不能成为扩展方法? [重复]

可能的重复: 扩展方法要求类是静态的 为什么不支持 C# 静态类扩展方法? 在.NET中: 为什么非静态类中的静态方法不能成为扩展方法?

回答 4 投票 0

在扩展类型中重新声明“==”运算符 - Dart

我无法在扩展类型中重新声明“==”运算符 扩展类型 Compare(List types) { @redeclare 布尔运算符 ==(List other) =>true; } 我明白了

回答 1 投票 0

如何重载执行相同操作但参数不同的扩展方法

我试图防止重复扩展代码 例如 UrlHelper(this SomeDataEventClass e) 和 UrlHelper(this AnotherDataEventClass e)。他们正在做同样的事情,这正在产生你......

回答 1 投票 0

如何在服务器中启用.zip扩展名

我想在我的服务器中启用 .zip 扩展名。我在 php.ini 文件中添加了以下代码。但网站将采用默认的 php.ini 文件。 扩展名=php_zip.dll 扩展名=zip.so 如果我上传...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.