SurrogateAttribute.Fody
0.5.26
See the version list below for details.
dotnet add package SurrogateAttribute.Fody --version 0.5.26
NuGet\Install-Package SurrogateAttribute.Fody -Version 0.5.26
<PackageReference Include="SurrogateAttribute.Fody" Version="0.5.26" />
paket add SurrogateAttribute.Fody --version 0.5.26
#r "nuget: SurrogateAttribute.Fody, 0.5.26"
// Install SurrogateAttribute.Fody as a Cake Addin #addin nuget:?package=SurrogateAttribute.Fody&version=0.5.26 // Install SurrogateAttribute.Fody as a Cake Tool #tool nuget:?package=SurrogateAttribute.Fody&version=0.5.26
A Fody weaver that allows mapping attributes to any other attributes, thus making C#'s attributes a bit more useful.
Installation
Install Fody and SurrogateAttribute.Fody:
PM> Install-Package Fody
PM> Install-Package SurrogateAttribute.Fody
Then add FodyWeavers.xml
file to your .csproj
with the following content:
<Weavers>
<SurrogateAttribute />
</Weavers>
This includes SurrogateAttribute
Fody add-in to the IL weaving process.
See Samples for a working solution.
In a nutshell
Instead of having:
class Cat
{
[Required(ErrorMessageResourceType = typeof(Translations), ErrorMessageResourceName = nameof(Translations.NameRequired))]
[MinLength(3, ErrorMessageResourceType = typeof(Translations), ErrorMessageResourceName = nameof(Translations.NameBadLength))]
[MaxLength(10, ErrorMessageResourceType = typeof(Translations), ErrorMessageResourceName = nameof(Translations.NameBadLength))]
[DisplayAttribute(ResourceType = typeof(Translations), Name = nameof(Translations.CatName))]
public string Name { get; set; }
}
you can create attributes that implement ISurrogateAttribute
and have reusable abstractions suitable to your requirements, e.g.
[AttributeUsage(AttributeTargets.Property)]
class RequiredWithLengthAttribute : Attribute, ISurrogateAttribute
{
[PropertyDefaultValue(1)]
public int MinLength { get; set; }
[PropertyDefaultValue(10)]
public int MaxLength { get; set; }
[PropertyDefaultValue(typeof(Translations))]
Type TranslationResourceType { get; }
Attribute[] ISurrogateAttribute.TargetAttributes => [
new RequiredAttribute { ErrorMessageResourceType = TranslationResourceType, ErrorMessageResourceName = nameof(Translations.NameRequired) },
new MinLengthAttribute(MinLength) { ErrorMessageResourceType = TranslationResourceType, ErrorMessageResourceName = nameof(Translations.NameBadLength) },
new MaxLengthAttribute(MaxLength) { ErrorMessageResourceType = TranslationResourceType, ErrorMessageResourceName = nameof(Translations.NameBadLength) },
];
}
[AttributeUsage(AttributeTargets.Property)]
class TranslationAttribute(string translationKey) : Attribute, ISurrogateAttribute
{
Attribute[] ISurrogateAttribute.TargetAttributes => [
new DisplayAttribute { ResourceType = typeof(Translations), Name = translationKey }
];
}
so Cat
can be simplified:
class Cat
{
[RequiredWithLength(MinLength = 3)]
[Translation(nameof(Translations.CatName))]
public string Name { get; set; }
}
Attributes implementing ISurrogateAttribute
are replaced with their TargetAttributes
at build time using IL weaving.
Supported features
TODO
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.