SurrogateAttribute.Fody 0.5.26

There is a newer version of this package available.
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                
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="SurrogateAttribute.Fody" Version="0.5.26" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SurrogateAttribute.Fody --version 0.5.26                
#r "nuget: SurrogateAttribute.Fody, 0.5.26"                
#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 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                

NuGet version (SurrogateAttribute.Fody) Build, Test and Deploy to NuGet.org

Icon

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

  • .NETStandard 2.0

  • .NETStandard 2.1

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.3 135 8/11/2024
0.6.2 102 7/14/2024
0.6.1 100 7/14/2024
0.5.28 92 7/13/2024
0.5.27 146 7/11/2024
0.5.26 96 7/11/2024
0.5.25 96 7/11/2024