C#.NET 编程开发技术、MSBUILD写法任务等...
C#.NET 编程开发技术
综合
C#.NET网络通讯TCP、UDP、Socket
类库:
SHA256等算法工具库 - https://github.com/ron4fun/SharpHash
SharpHash.Base.HashFactory.Crypto.CreateSHA2_256().ComputeString(StrIn, new ASCIIEncoding()).GetBytes();
.NET框架对应C#语言版本
目标 |
版本 |
C# 语言版本的默认值 |
.NET |
9.x |
C# 13 |
.NET |
8.x |
C# 12 |
.NET |
7.x |
C# 11 |
.NET |
6.x |
C# 10 |
C#语法
死记:
C#实例对象不允许调用其静态方法。
C#转换类型时,若有信息丢失则必须显式转换: int a=(int)0.1;
out就地声明变量(还能将值类型变引用类型):if (int.TryParse(input, out var result)) { WriteLine(result); }
单例:
public sealed class Singleton
{
private static readonly Lazy lazy =
new Lazy(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton() { }
}
流式响应下载:
[HttpGet("products")]
public async IAsyncEnumerable<Product> GetProductsAsync() {
await foreach (var product in _context.Products.AsAsyncEnumerable()) { yield return product; }
}
工具、构建
SDK:
内建SDK 或自定义SDK(带版本号则从NuGet获取) 。
私有包则应在项目目录或任意上级目录定义个 nuget.config :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="github" value="https://nuget.pkg.github.com/iwangxiaodong/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="iwangxiaodong" />
<add key="ClearTextPassword" value="apiKey" />
</github>
</packageSourceCredentials>
</configuration>
MSBUILD各种构建任务(x.csproj):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>同名则取后者</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.2" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition=" '$(Configuration)' == 'ExportRelease' ">
<Delete Files="$(IntermediateOutputPath)/$(AssemblyName).dll" />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'ExportRelease' ">
<!-- 构建时所在目录即项目源码根目录 -->
<Message Text="Dir: $(MSBuildProjectDirectory)" />
<Copy SourceFiles="$(TargetPath)" DestinationFiles=".\x.dll" SkipUnchangedFiles="true" />
<Exec Command="tool.exe ./.config/x.xml" EnvironmentVariables="x=$(x);y=$(y)" />
<Copy SourceFiles=".\x.dll" DestinationFiles="$(IntermediateOutputPath)/$(AssemblyName).dll" SkipUnchangedFiles="true" />
<WriteLinesToFile File="./x.cfg" Overwrite="true" /><!-- 创建或清空文件 -->
<Move SourceFiles="./x.cfg" DestinationFiles="./x.cfg-bak" /><!-- SourceFiles若不存在则报错!? -->
</Target>
</Project>