Created
October 25, 2022 11:07
-
-
Save jskeet/3b9ce9f6c8f8b2e3ef4895dcf99b8a94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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