Here is the solution I came up with:
public static string GetElementName(BaseClass target)
{
XmlRootAttribute attribute = target.GetType().GetCustomAttribute<XmlRootAttribute>();
return attribute == null ? null : attribute.ElementName;
}
And the usage looks like this:
BaseClass baseClass = new BaseClass();
BaseClass child1 = new ChildClass1();
BaseClass child2 = new ChildClass2();
Console.WriteLine(GetElementName(baseClass)); // empty string
Console.WriteLine(GetElementName(child1)); // ChildClass1
Console.WriteLine(GetElementName(child2)); // ChildClass2