C# GetProperties 특정 Property 제외하고 가져오기

Featured image

Git Source


특정 Property 제외 👓

class SkipPropertyAttribute : Attribute
{

}

class Person
{
	[SkipProperty]
	public string PersonalID { get; set; }
	public string Name { get; set; }
	public int Age { get; set; }
	public string Address { get; set; } 
}
//main
static void Main(string[] args)
{
	var properties = typeof(Person).GetProperties();

	foreach (var property in properties)
	{
		Console.WriteLine(property.Name);
	}

	Console.WriteLine("");

	var skipProperties = typeof(Person).GetProperties().Where(property => !Attribute.IsDefined(property, typeof(SkipPropertyAttribute)));

	foreach (var skip in skipProperties)
	{
		Console.WriteLine(skip.Name);
	}
}

결과

skipproperty