ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToDictionario

Convierte una clase a un Dictionary

Source

public static Dictionario<string, object> ToDictionario<T>(this T source) {
	return (
		typeof(T)
		.GetProperties(BindingFlags.Instance | BindingFlags.Public)
		.ToDictionary(x => x.Name, x => x.GetValue(source, null))
	);
}

Example

Person p = new Person();
Dictionary<string, object> dict = p.ToDictionario();

Author: UlisexXx

Submitted on: 21 nov. 2023

Language: csharp

Type: System.Collections.Generic

Views: 1291