.net 相关问题

不要用于有关.NET Core的问题。 .NET框架是一个主要为Microsoft Windows操作系统设计的软件框架。它包括基类库,公共语言运行时(通常称为CLR),公共类型系统(通常称为CTS)和动态语言运行时的实现。它支持许多编程语言,包括C#,VB.NET,F#和C ++ / CLI。

Google recaptcha enterprise:未找到您的默认凭据

我正在为我的网站 (.NET MVC) 设置 Google reCaptcha 企业。它在我的开发环境中运行良好,但在生产服务器上找不到应用程序默认凭据。我得到的是...

回答 1 投票 0

如何使用 .net 8.0.2 处理 vb.net 中打开的 excel 文件。?

我正在使用vb.net和.net 8.0.2。应用程序在 Windows 11 上运行。 我已经尝试了几个小时来指向用户已打开的 Excel 文件。 我试过: 将 app_excel 调暗为 Microsoft。

回答 1 投票 0

从 C# 调试 C++ cli - C++ 断点未命中

我有一个 C# win-exe-64 项目的解决方案,引用了 C++(托管、cli、64)库 vcxproj。使用 Visual Studio 16.10.3。 一切正常,我可以在 C# 中使用 C++ 中的类。 问题是

回答 2 投票 0

Entity Core Framework - HasColumnType 和 HasPrecision 之间有什么区别?

我最近开始使用 C# 中的 Entity Core Framework 开发一个项目,我的问题与实体字段的 2 种配置方法有关。正如您可能已经了解每个条目一样...

回答 1 投票 0

动态SQL参数化查询

我们正在使用 Snowflake 客户端 nuget 包并创建动态查询,如下所示: 使用 Snowflake.Client; 使用 (var conn = new SnowflakeDbConnection()) { conn.ConnectionString =

回答 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

无法作为 Windows 主机运行 .net Windows 服务 - sc.exe(该服务未及时响应启动或控制请求)

我编写了自己的程序,目的是将其作为后台服务(Windows 服务)运行,而不是作为应用程序运行。 我从向导创建了默认程序(https://learn.microsoft.com/en-us/do...

回答 1 投票 0

.Net Core C#使用HttpClientFactory发送请求时如何添加参数?

使用HttpClient发送请求时如何添加参数? 我的目标 API 有问题;它需要两个参数,我需要弄清楚如何向我的请求添加两个参数。会...

回答 1 投票 0

适用于 Mac 或 Linux 的 .NET 反编译器

我需要反编译一个用.NET编写的小应用程序并将其转换为C++。我没有安装 Windows,但我知道有许多适用于 Windows 的 .NET 反编译器。因为我只有 Mac 和...

回答 5 投票 0

.Net 8 控制台应用程序 DI 错误“无法解析类型“Microsoft.Extensions.Logging.ILoggerFactory”的服务

我正在尝试按照本视频 How to use Dependency Injection (DI) in C# Console Application with Logging 中的说明向 .Net 8 控制台应用程序添加依赖项注入,但我得到...

回答 1 投票 0

Linq 查询分组依据并选择第一项

我有一个类似这样的字符串数组: // 图标、类别、工具 字符串[,] subButtonData = 新字符串[,] { {"graphics/gui/brushsizeplus_icon", "绘图", "DrawBrushPlus"}, {“图形/gui/

回答 5 投票 0

如何从 Blazor 应用程序中的类库访问连接字符串?

在我的解决方案中,我有三个项目: 类库(业务逻辑层/数据访问) Blazor WebAssembly 应用程序(客户端应用程序) 控制台应用程序(客户端应用程序) 我的类库拥有所有业务逻辑:...

回答 1 投票 0

有关将 C# 代码与 django 后端集成的建议

我使用 C# 将文档(pdf、docx 等)转换为其他格式(docx、xls、ppt 等),我的后端是用 Python (django) 编写的 我成功编写并加载了一个 C# dll,我可以称之为

回答 1 投票 0

InvalidOperationException:调度程序处理已暂停,但消息仍在处理中

我们遇到了与此异常有关的多个问题,但我找不到有关问题的真正原因、此错误的所有可能来源以及我们应该避免什么的技术文档...

回答 2 投票 0

如何解决System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()异常

我有一个读取 SQL Server 数据库的 C# 应用程序。 有时我会收到错误并且应用程序崩溃。 如果我查看事件查看器,我会得到以下信息: 应用程序:XXXXXXXXX_CLI.EXE 弗拉姆版本...

回答 1 投票 0

.NET 依赖注入与通用接口和继承

我最近想到了一个案例,我可以将新闻导入器的抽象构建到 .NET DI Contianer 中。 这是我的示例案例。 这个类代表一个新闻源。 公共抽象类 S...

回答 1 投票 0

将三个有序列表中缺少某些数据的相同数据配对

数据集如下: int[] X = { 1, 2, 3, 5, 6, 7}; int[] Y = { 1, 2, 4, 5, 6, 7}; int[] Z = { 1, 2, 4, 6, 7 }; 我正在编写一个 C# 程序,将相同的值配对在一起并留下遗漏的

回答 1 投票 0

读取 BACNET IP 路由器上的设备属性

我在从 BACNET IP 路由器上的设备读取属性时遇到问题。为此,我使用 .net 库 https://github.com/ela-compil/BACnet 如果我与多个设备交互,每个设备都有它的...

回答 3 投票 0

容器化 .NET 8 Blazor WASM 在从 .NET 6 更新后中断

将 .NET 版本从 6 更新到 8 后,启动容器后我无法使用已部署的 WebApp,并出现如下所示的异常 我现在有了这个 Dockerfile,它与 .N 配合得很好...

回答 1 投票 0

为什么“公共事件EventHandler cccc”将为空?

为什么“公共事件EventHandler cccc”将为空? 我有一堂课是 公共类生成器 { 公共事件 EventHandler StartedWorking; 公共生成器() { // 构造函数

回答 5 投票 0

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