Robware Software by Rob

Welcome

Hello, I'm Rob. I'm a senior software engineer at On The Beach. Professionally my focus has been in backend development primarily using C#. In my spare time I spend my time riding bikes or making stuff, typically involving an Arduino.

This website is primarily an outlet for me to write about things which have been technically challenging, either in a professional or personal capacity, though not limited to that.

If you wish to get in contact, then get in touch via my LinkedIn profile.

Latest code commit

Repositorywebsite
Branchmaster
SHA5966641b751fefa7f837659977833e21c73ef5eb
MessageGet branch details and sort by modification date
Timestamp21:24:30 on Saturday the 5th of July 2025

Latest Blog Post

Casting between types that share a parent in C#

Sometimes you want to cast between two types that share the same parent. In one of my resent cases I had two settings objects which shared a parent and I wanted to create a second object from the first and keep the parent data.

Take the following code:

record Parent {
	public string Surname { get; init; }
	public string Location { get; init; }
}

record Son : Parent {
	public string Forename { get; init; }
}

record Daughter : Parent {
	public int Age { get; init; }
}

Say you had a Son object and wanted it to be a Daughter and keep the Surname and Location from Parent. You would have to map the fields manually. Fine if you have the one instance of this behaviour with one set of objects, but what if you want to do this repeatedly using different types? I had an ever increasing set of cases where I wanted to move data between object types whilst keeping parent data, so I wrote this method:

public static class ObjectExtensions
{
	public static TChild CastToTypeWithParentProperties<TParent, TChild>(this TParent source) where TChild : TParent, new()
	{
		if (source is null)
		{
			throw new ArgumentNullException(nameof(source), "Source object cannot be null");
		}

		var parentType = typeof(TParent);
		var properties = parentType.GetProperties().Where(property => property is {CanRead: true, CanWrite: true});

		var target = new TChild();

		foreach (var prop in properties)
		{
			var value = prop.GetValue(source);
			prop.SetValue(target, value);
		}

		return target;
	}
}

This allows you to do var daughter = son.CastToTypeWithParentProperties<Parent, Daughter>(parent); and daugher would share the same Surname and Location values as son.

Posted on Monday the 7th of July 2025

View more