by Zoran Horvat
Reflection lets us access members of an object, including property getters and setters. Through specialized methods we can read values from properties and write values back into them.
LINQ to Objects does not add any special ability regarding getting and setting property values. But LINQ makes it easy to filter the type members according to various criteria.
The following piece of code selects all read/write properties of integer type and converts them to name/value pairs. Name in each pair is the property name, while value is the actual value of the property. Here is the code:
IEnumerable<Tuple<string, int>> DehydrateObject(object obj)
{
IEnumerable<Tuple<string, int>> values =
(from property in obj.GetType().GetProperties()
where property.PropertyType == typeof(int) &&
property.CanRead &&
property.CanWrite
select new Tuple<string, int>(property.Name,
(int)property.GetValue(obj)));
return values;
}
Below is the source code of a console application which demonstrates the DehydrateObject method. When function listed above is applied to an instance of the Rectangle structure, it extracts four integer properties that hold rectangle's location and size:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace PropertyGettingDemo
{
class Program
{
static IEnumerable<Tuple<string, int>> DehydrateObject(object obj)
{
IEnumerable<Tuple<string, int>> values =
(from property in obj.GetType().GetProperties()
where property.PropertyType == typeof(int) &&
property.CanRead &&
property.CanWrite
select new Tuple<string, int>(property.Name,
(int)property.GetValue(obj)));
return values;
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle(20, 30, 150, 90);
IEnumerable<Tuple<string, int>> values = DehydrateObject(rect);
Console.WriteLine("Rectangle={0}", rect);
foreach (Tuple<string, int> value in values)
Console.WriteLine("{0}={1}", value.Item1, value.Item2);
Console.Write("Press ENTER to continue... ");
Console.ReadLine();
}
}
}
The code produces output like this:
Rectangle={X=20,Y=30,Width=150,Height=90}
X=20
Y=30
Width=150
Height=90
Press ENTER to continue...
If you wish to learn more, please watch my latest video courses
In this course, you will learn the basic principles of object-oriented programming, and then learn how to apply those principles to construct an operational and correct code using the C# programming language and .NET.
As the course progresses, you will learn such programming concepts as objects, method resolution, polymorphism, object composition, class inheritance, object substitution, etc., but also the basic principles of object-oriented design and even project management, such as abstraction, dependency injection, open-closed principle, tell don't ask principle, the principles of agile software development and many more.
More...
In this course, you will learn how design patterns can be applied to make code better: flexible, short, readable.
You will learn how to decide when and which pattern to apply by formally analyzing the need to flex around specific axis.
More...
This course begins with examination of a realistic application, which is poorly factored and doesn't incorporate design patterns. It is nearly impossible to maintain and develop this application further, due to its poor structure and design.
As demonstration after demonstration will unfold, we will refactor this entire application, fitting many design patterns into place almost without effort. By the end of the course, you will know how code refactoring and design patterns can operate together, and help each other create great design.
More...
In four and a half hours of this course, you will learn how to control design of classes, design of complex algorithms, and how to recognize and implement data structures.
After completing this course, you will know how to develop a large and complex domain model, which you will be able to maintain and extend further. And, not to forget, the model you develop in this way will be correct and free of bugs.
More...
Zoran Horvat is the Principal Consultant at Coding Helmet, speaker and author of 100+ articles, and independent trainer on .NET technology stack. He can often be found speaking at conferences and user groups, promoting object-oriented and functional development style and clean coding practices and techniques that improve longevity of complex business applications.