Skip to content

Instantly share code, notes, and snippets.

@jskeet
Created October 25, 2022 11:07
Show Gist options
  • Save jskeet/3b9ce9f6c8f8b2e3ef4895dcf99b8a94 to your computer and use it in GitHub Desktop.
Save jskeet/3b9ce9f6c8f8b2e3ef4895dcf99b8a94 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
[System.AttributeUsage(System.AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class CustomDateTimeAttribute : System.Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string positionalString;
// This is a positional argument
public CustomDateTimeAttribute(string positionalString)
{
this.positionalString = positionalString;
}
public string PositionalString
{
get { return positionalString; }
}
// This is a named argument
public int NamedInt { get; set; }
}
public class CardSummaryDto
{
[CustomDateTime("dd/mm/yyyy")]
public string cardsentdate { get; set; }
}
class Program
{
static void Main()
{
var prop = typeof(CardSummaryDto).GetProperty(nameof(CardSummaryDto.cardsentdate));
var attr = prop.GetCustomAttribute<CustomDateTimeAttribute>();
Console.WriteLine(attr.PositionalString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment