https://github.com/ex-exe/enumnotifygenerator
EnumAttributeGenerator is a C# source generator that facilitates the automatic generation of attributed properties based on changes to specified enum values.
https://github.com/ex-exe/enumnotifygenerator
csharp library sourcegenerator
Last synced: 5 months ago
JSON representation
EnumAttributeGenerator is a C# source generator that facilitates the automatic generation of attributed properties based on changes to specified enum values.
- Host: GitHub
- URL: https://github.com/ex-exe/enumnotifygenerator
- Owner: EX-EXE
- License: mit
- Created: 2024-03-03T12:46:53.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-06T12:43:02.000Z (almost 2 years ago)
- Last Synced: 2025-03-16T04:19:19.168Z (12 months ago)
- Topics: csharp, library, sourcegenerator
- Language: C#
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/nu/EnumNotifyGenerator)
# EnumNotifyGenerator
EnumAttributeGenerator is a C# source generator that facilitates the automatic generation of attributed properties based on changes to specified enum values.
When an enum value is modified, properties adorned with specific attributes are automatically updated.
## Usage
### Install by NuGet
PM> Install-Package [EnumNotifyGenerator](https://www.nuget.org/packages/EnumNotifyGenerator)
### Base Source
```csharp
using EnumNotifyGenerator;
public enum LanguageType
{
Japanese,
English,
}
[EnumNotify(typeof(LanguageType),"CurrentLanguage")]
public partial class Message : INotifyPropertyChanged
{
private static Message? instance = null;
public static Message Shared => instance ??= new Message();
public event PropertyChangedEventHandler? PropertyChanged;
[EnumValue(LanguageType.Japanese, "成功")]
[EnumValue(LanguageType.English, "Success")]
private string successMessage = string.Empty;
[EnumValue(LanguageType.Japanese,"エラー")]
[EnumValue(LanguageType.English, "Error")]
private string errorMessage = string.Empty;
}
```
### Generated Source
```csharp
partial class Message
{
private global::EnumNotifyWpf.LanguageType _CurrentLanguage;
public global::EnumNotifyWpf.LanguageType CurrentLanguage
{
get
{
return _CurrentLanguage;
}
set
{
if(_CurrentLanguage == value)
{
return;
}
_CurrentLanguage = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("CurrentLanguage"));
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("SuccessMessage"));
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("ErrorMessage"));
}
}
}
public string SuccessMessage
{
get
{
switch(_CurrentLanguage)
{
case (global::EnumNotifyWpf.LanguageType)0:
return @"成功";
case (global::EnumNotifyWpf.LanguageType)1:
return @"Success";
default:
break;
}
return successMessage;
}
}
public string ErrorMessage
{
get
{
switch(_CurrentLanguage)
{
case (global::EnumNotifyWpf.LanguageType)0:
return @"エラー";
case (global::EnumNotifyWpf.LanguageType)1:
return @"Error";
default:
break;
}
return errorMessage;
}
}
}
```
### Usage(WPF)

#### MessageBinding.cs
```csharp
public class MessageBinding(string path) : MarkupExtension
{
public string Path { get; set; } = path;
public override object ProvideValue(IServiceProvider serviceProvider)
{
var binding = new Binding(Path)
{
Source = Message.Shared,
Mode = BindingMode.OneWay,
};
return binding.ProvideValue(serviceProvider);
}
}
```
#### MainWindow.xaml
```xaml
Japanese
English
```
#### MainWindow.xaml.cs
```csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(sender is ComboBox comboBox &&
comboBox.SelectedItem is ComboBoxItem comboBoxItem &&
comboBoxItem.Content is string comboBoxItemContent)
{
switch(comboBoxItemContent)
{
case "Japanese":
Message.Shared.CurrentLanguage = LanguageType.Japanese;
break;
case "English":
Message.Shared.CurrentLanguage = LanguageType.English;
break;
}
}
}
}
```