Recently, in one of my projects, I had to extract properties and values of a C# object to send as key-value pairs to a third-party REST API. Instead of manually extracting each property, I wanted a way to convert a Plain Old CLR Object (POCO) into a Dictionary<string, object>. After doing some research, I discovered multiple approaches to achieve this in C#. In this article, we shall look at different approaches to converting a POCO to a dictionary in C#.
Disclaimer. The approaches discussed in this post work well for simple objects with primitive data types (e.g., strings, integers). If you have complex objects, such as nested classes or collections, additional handling will be required.
Method 1. Using Reflection
Reflection is a powerful feature in C# that allows you to inspect and interact with object types and members at runtime. You can use it to iterate over the properties of a POCO object and create a dictionary from them.
using System;
using System.Collections.Generic;
using System.Reflection;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
}
public static class ObjectExtensions
{
public static Dictionary<string, object> ToDictionary<T>(this T obj)
{
var dictionary = new Dictionary<string, object>();
foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
dictionary[property.Name] = property.GetValue(obj, null);
}
return dictionary;
}
}
class Program
{
static void Main()
{
var person = new Person
{
Name = "John",
Age = 30,
Occupation = "Software Developer"
};
Dictionary<string, object> dictionary = person.ToDictionary();
foreach (var kvp in dictionary)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
Explanation
- Reflection: The code uses the
GetPropertiesmethod to obtain all public properties of the object.BindingFlags.PublicandBindingFlags.Instanceare used to specify that only instance properties should be included. - Property Values: The
GetValuemethod retrieves the value of each property for the given object.
Output

Method 2. Using Newtonsoft.Json
Newtonsoft.Json is a popular JSON library for .NET, which can be used to serialize and deserialize JSON. It can also be leveraged to convert a POCO object into a dictionary.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
}
class Program
{
static void Main()
{
var person = new Person
{
Name = "John",
Age = 30,
Occupation = "Software Developer"
};
var json = JsonConvert.SerializeObject(person);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
foreach (var kvp in dictionary)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
Explanation
- Serialization: The
SerializeObjectmethod converts the POCO object into a JSON string. - Deserialization: The
DeserializeObjectmethod then converts the JSON string into a dictionary.
Output

Method 3. Using System.Text.Json (C# 8.0+)
For .NET Core and .NET 5/6/7 projects, you can use System.Text.Json a lightweight JSON library included in the .NET framework.
using System;
using System.Collections.Generic;
using System.Text.Json;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
}
class Program
{
static void Main()
{
var person = new Person
{
Name = "John",
Age = 30,
Occupation = "Software Developer"
};
var json = JsonSerializer.Serialize(person);
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
foreach (var kvp in dictionary)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
Explanation
- System.Text.Json: The
JsonSerializerclass provides methods to serialize and deserialize objects, similar to Newtonsoft.Json but built into the .NET framework. - Serialization and Deserialization: The steps are the same as using Newtonsoft.Json but with the built-in
JsonSerializer.
Output

Method 4. Using LINQ and Custom Logic
If you prefer a more custom approach, you can map properties manually using LINQ.
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
}
public static class ObjectExtensions
{
public static Dictionary<string, object> ToDictionary<T>(this T obj)
{
return typeof(T)
.GetProperties()
.ToDictionary(prop => prop.Name, prop => prop.GetValue(obj, null));
}
}
class Program
{
static void Main()
{
var person = new Person
{
Name = "John",
Age = 30,
Occupation = "Software Developer"
};
Dictionary<string, object> dictionary = person.ToDictionary();
foreach (var kvp in dictionary)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
}
Explanation
- LINQ: The
ToDictionarymethod from LINQ is used to create a dictionary by iterating over each property of the object and capturing the name and value.
Output



Join the conversation! Your thoughts help the community grow.