Assuming I have a class MyDto
that will be mapped like this:
public class MyDto
{
public string PropertyA { get; }
public int PropertyB { get; }
public bool PropertyC { get; }
}
public class MyMapper
{
public MyResult Map(MyDto objectToMap) =>
new MyResult { Bla = objectToMap.PropertyA, Blub = objectToMap.PropertyC };
}
As you can see, MyMapper
only uses PropertyA
and PropertyC
, but not PropertyB
. How can I resolve this programmatically?
Or in other words: how can I determine all property references of MyDto
like this:
var namesOfUsedProperties = typeof(MyDto).FindAllPropertyReferences();
namesOfUsedProperties.Should().Contain("PropertyA", "PropertyC");
namesOfUsedProperties.ShouldNot().Contain("PropertyB");
Thank you!
PS: I'm using C# 11 and .NET 7.