PropertyBind 0.1.2
See the version list below for details.
dotnet add package PropertyBind --version 0.1.2
NuGet\Install-Package PropertyBind -Version 0.1.2
<PackageReference Include="PropertyBind" Version="0.1.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add PropertyBind --version 0.1.2
#r "nuget: PropertyBind, 0.1.2"
// Install PropertyBind as a Cake Addin #addin nuget:?package=PropertyBind&version=0.1.2 // Install PropertyBind as a Cake Tool #tool nuget:?package=PropertyBind&version=0.1.2
PropertyBind
Property synchronization process using Source Generator.
Demo
Assume that the Blog class has a Post class collection, and the Post class has a Blog property.
In this case, you need to write code like the following to synchronize the Blog class without any conflicts.
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public class Blog
{
public ObservableCollection<Post> Posts { get; } = new();
public Blog()
{
Posts.CollectionChanged += Posts_CollectionChanged;
}
private void Posts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewItems == null) return;
foreach (Post item in e.NewItems)
{
item.Blog = this;
}
}
}
}
public class Post
{
public Blog Blog { get; set; } = null!;
}
The above code is very tedious. Therefore, by using the SourceGenerator PropertyBind, you can write code with the same meaning as above using attributes.
using System.Collections.ObjectModel;
[GeneratePropertyBind(nameof(Posts), nameof(Post.Blog))]
public partial class Blog
{
public ObservableCollection<Post> Posts { get; } = new();
}
public class Post
{
public Blog Blog { get; set; } = null!;
}
Specify the property name of the collection in the first argument.
In the second argument, specify the property name you want to associate with itself.
Note: auto-generated code
GeneratePropertyBindAttribute.cs
namespace PropertyBind
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class GeneratePropertyBindAttribute : Attribute
{
public string ObservableCollectionPropertyName { get; }
public string BindPropertyName { get; }
public GeneratePropertyBindAttribute(string observableCollectionPropertyName, string bindPropertyName)
{
this.ObservableCollectionPropertyName = observableCollectionPropertyName;
this.BindPropertyName = bindPropertyName;
}
}
}
Blog.g.cs
using System.Collections.Specialized;
public partial class Blog
{
public Blog()
{
Posts.CollectionChanged += Posts_CollectionChanged;
}
private void Posts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewItems == null) return;
foreach (Post item in e.NewItems)
{
item.Blog = this;
}
}
}
}
Learn more about Target Frameworks and .NET Standard.
This package has 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.