Skip to content

Instantly share code, notes, and snippets.

@VictorHaine
Created May 25, 2020 06:14
Show Gist options
  • Select an option

  • Save VictorHaine/cf548fb2a07d85ff2b6a4a29bb3dc10c to your computer and use it in GitHub Desktop.

Select an option

Save VictorHaine/cf548fb2a07d85ff2b6a4a29bb3dc10c to your computer and use it in GitHub Desktop.
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