SurrogateAttribute.Fody 0.6.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package SurrogateAttribute.Fody --version 0.6.7
                    
NuGet\Install-Package SurrogateAttribute.Fody -Version 0.6.7
                    
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.6.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SurrogateAttribute.Fody" Version="0.6.7" />
                    
Directory.Packages.props
<PackageReference Include="SurrogateAttribute.Fody" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SurrogateAttribute.Fody --version 0.6.7
                    
#r "nuget: SurrogateAttribute.Fody, 0.6.7"
                    
#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.
#addin nuget:?package=SurrogateAttribute.Fody&version=0.6.7
                    
Install SurrogateAttribute.Fody as a Cake Addin
#tool nuget:?package=SurrogateAttribute.Fody&version=0.6.7
                    
Install SurrogateAttribute.Fody as a Cake Tool

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

Icon

A Fody add-in that allows creating C# attributes composed of other attributes, thus making C#'s attributes a bit more useful.

In a nutshell

Instead of having a mess like this:

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 have:

class Cat
{
    [RequiredWithLength(MinLength = 3)]
    [Translation(nameof(Translations.CatName))]
    public string Name { get; set; }
}

By creating custom attributes that implement ISurrogateAttribute:

[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 }
    ];
}

Usages of those custom attributes are replaced by their corresponding TargetAttributes at build time using IL weaving.

Installation

Install Fody and SurrogateAttribute.Fody to each project in which you want to use surrogate attributes:

Install-Package Fody
Install-Package SurrogateAttribute.Fody

And make sure both dependencies have PrivateAssets="All" like so, because they are only needed during build:

<PackageReference Include="Fody" Version="6.8.1" PrivateAssets="All" />
<PackageReference Include="SurrogateAttribute.Fody" Version="0.6.7" PrivateAssets="All" />

A FodyWeavers.xml file will be added automatically to the project on rebuild. If not, create the file with the following content:

<Weavers>
  <SurrogateAttribute />
</Weavers>

This includes SurrogateAttribute Fody add-in to the IL weaving process.

In case your project (most likely a library) has no surrogate attribute usages but has surrogate attribute implementations, you may want to install SurrogateAttribute.Core instead, which has the required types for implementing surrogate attributes but does not include Fody as a dependency:

Install-Package SurrogateAttribute.Core

See Samples for a working solution.

Features

TODO

Supported targets for surrogate attributes

The following table displays what's currently supported. The support will be extended to cover all targets in the future:

AttributeTarget Supported currently?
Assembly 🚫
Module 🚫
Class
Struct 🚫
Enum 🚫
Constructor 🚫
Method 🚫
Property
Field 🚫
Event 🚫
Interface
Parameter 🚫
Delegate 🚫
ReturnValue 🚫
GenericParameter 🚫
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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.

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.8 124 3/16/2025
0.6.7 123 3/16/2025
0.6.6 168 3/11/2025
0.6.3 141 8/11/2024
0.6.2 104 7/14/2024
0.6.1 104 7/14/2024
0.5.28 95 7/13/2024
0.5.27 151 7/11/2024
0.5.26 99 7/11/2024
0.5.25 100 7/11/2024