Created
May 25, 2020 06:14
-
-
Save VictorHaine/cf548fb2a07d85ff2b6a4a29bb3dc10c 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.Collections.Generic; | |
| using System.Linq; | |
| using System.Reflection; | |
| using System.ComponentModel; | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| var dto = new ExampleDTO { | |
| Guid = Guid.NewGuid(), | |
| Name = "Example", | |
| Address = "Somewhere" | |
| }; | |
| var properties = dto.GetType().GetProperties(); | |
| Console.WriteLine($"Found {properties.Count()} properties"); | |
| foreach(var p in properties) { | |
| var displayNameAttribute = (DisplayNameAttribute)p.GetCustomAttribute(typeof(DisplayNameAttribute)); | |
| Console.WriteLine(p.Name); | |
| Console.WriteLine(displayNameAttribute.DisplayName); | |
| Console.WriteLine(p.GetValue(dto)); | |
| Console.WriteLine(""); | |
| } | |
| } | |
| } | |
| public class ExampleDTO { | |
| [DisplayName("id")] | |
| public Guid Guid {get;set;} | |
| [DisplayName("name")] | |
| public string Name {get;set;} | |
| [DisplayName("addr")] | |
| public string Address {get;set;} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment