I need to set and ready the "description" of a property class.

Example:

public class MyClass
{
	[Description("Description for a property")]
	public string Note { get;set; }
}

 

 

private List<TemplateField> GetPropertiesWithDescription<T>()
{
	Type t = typeof(T);
	var properties = t.GetProperties();

	List<TemplateField> result = new List<TemplateField>();

	foreach (var property in properties)
	{
		var attributes = property.GetCustomAttributes(false);
		if (attributes.FirstOrDefault() == null)
			continue;
		var descriptionAttribute = attributes.First() as DescriptionAttribute;
		if (descriptionAttribute == null)
			continue;

		string propertyName = t.Name + "." + property.Name;

		TemplateField item = new TemplateField();
		item.Name = propertyName;
		item.PlaceHolder = $"{_placeHolderPrefix}{propertyName}{_placeHolderSuffix}";
		item.Description = descriptionAttribute.Description;
		result.Add(item);
	}

	return result;
}

Usage:

List<TemplateField> result = GetPropertiesWithDescription<MyClass>();

Note: remember to add this using:

using System.ComponentModel;