LazyProperties 1.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package LazyProperties --version 1.0.1
NuGet\Install-Package LazyProperties -Version 1.0.1
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="LazyProperties" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LazyProperties --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LazyProperties, 1.0.1"
#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.
// Install LazyProperties as a Cake Addin #addin nuget:?package=LazyProperties&version=1.0.1 // Install LazyProperties as a Cake Tool #tool nuget:?package=LazyProperties&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
LazyProperties
Template property code generator based on partial
property syntax. 基于 partial
属性语法的属性模板化代码生成器。
例:
为属性生成延迟获取的代码
internal partial class SampleClass(IServiceProvider serviceProvider)
{
[LazyProperty]
partial IService Service { get; }
public T GetInstance<T>() => (T)(serviceProvider.GetService(typeof(T)) ?? throw new InvalidOperationException());
}
生成代码为
partial class SampleClass
{
private IService __Service;
partial IService Service
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get => __Service ??= GetInstance<IService>();
}
}
使用方法
引用包
<ItemGroup>
<PackageReference Include="LazyProperties" Version="1.0.0" />
</ItemGroup>
声明类型
- 需要
C#13
(partial 属性) 及以上 - 类型必须标记为
partial
- 需要自动生成的属性必须标记为
partial
- 可使用
LazyPropertyTemplateAttribute
为类型标记生成时获取对象实例所使用的模板,默认获取模板为$FieldName$ ??= GetInstance<$Type$>()
- 模板支持以下变量
$Type$
: 类型名称$PropertyName$
: 属性名称$FieldName$
: 生成的字段名称 (仅当模板包含此变量时才会生成字段)
- 模板支持以下变量
- 使用
LazyPropertyAttribute
标记属性为自动生成- 当使用
LazyPropertyAttribute
标记类型时,类型的所有partial
属性都会进行生成
- 当使用
partial class SampleService
{
[LazyProperty]
public partial IService Service { get; }
private T GetInstance<T>() => throw new NotImplementedException());
}
设置全局模板
针对单个类型设置 LazyPropertyTemplateAttribute
在一些时候过于麻烦,可以使用 LazyPropertyGlobalTemplate
进行针对项目的全局设置,示例:
<PropertyGroup>
<LazyPropertyGlobalTemplate>$FieldName$ ??= default</LazyPropertyGlobalTemplate>
</PropertyGroup>
LazyPropertyTemplateAttribute
的优先级高于LazyPropertyGlobalTemplate
- xml中某些特殊字符需要转义,如:
<
→<
、>
→>
There are no supported framework assets in this 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.