To expand on my comment, this is a way to achieve your task with a different approach. You say you want to "indicate related properties in a class", and that you "would like to use lambda expressions so that I can pass a strong type into my attribute's constructor, and not a "magic string". This way I can exploit compiler type checking".
Here then is a way of indicating related properties that is compile-time typed and doesn't have any magic strings:
public class MyClass
{
public int EmployeeId { get; set; }
public int EmployeeNumber { get; set; }
}
This is the class under consideration. We want to indicate that EmployeeId
and EmployeeNumber
are related. For a bit of code conciseness, let's put this type alias up at the top of the code file. It's not necessary at all but it does make the code less intimidating:
using MyClassPropertyTuple =
System.Tuple<
System.Linq.Expressions.Expression<System.Func<MyClass, object>>,
System.Linq.Expressions.Expression<System.Func<MyClass, object>>
>;
This makes MyClassPropertyTuple
an alias for a Tuple
of two Expression
s, each of which captures the definition of a function from a MyClass
to an object. For example, property getters on MyClass
are such functions.
Now let's capture the relationship. Here I've made a static propery on MyClass
, but this list could be defined anywhere:
public class MyClass
{
public static List<MyClassPropertyTuple> Relationships
= new List<MyClassPropertyTuple>
{
new MyClassPropertyTuple(c => c.EmployeeId, c => c.EmployeeNumber)
};
}
The C# compiler knows we're constructing a Tuple
of Expression
s, so we don't need any explicit casts in front of those lambda expressions - they are automatically made into Expression
s.
That's basically it in terms of definition - those EmployeeId
and EmployeeNumber
mentions are strongly typed and enforced at compile time, and refactoring tools that do property renames should be able to find those usages during a rename (ReSharper definitely can). There are no magic strings here.
But of course we also want to be able to interrogate relationships at runtime (I assume!). I don't know exactly how you want to be doing this so this code is just illustrative.
class Program
{
static void Main(string[] args)
{
var propertyInfo1FromReflection = typeof(MyClass).GetProperty("EmployeeId");
var propertyInfo2FromReflection = typeof(MyClass).GetProperty("EmployeeNumber");
var e1 = MyClass.Relationships[0].Item1;
foreach (var relationship in MyClass.Relationships)
{
var body1 = (UnaryExpression)relationship.Item1.Body;
var operand1 = (MemberExpression)body1.Operand;
var propertyInfo1FromExpression = operand1.Member;
var body2 = (UnaryExpression)relationship.Item2.Body;
var operand2 = (MemberExpression)body2.Operand;
var propertyInfo2FromExpression = operand2.Member;
Console.WriteLine(propertyInfo1FromExpression.Name);
Console.WriteLine(propertyInfo2FromExpression.Name);
Console.WriteLine(propertyInfo1FromExpression == propertyInfo1FromReflection);
Console.WriteLine(propertyInfo2FromExpression == propertyInfo2FromReflection);
}
}
}
The code for propertyInfo1FromExpression
and propertyInfo2FromExpression
here I worked out with judicious use of the Watch window while debugging - this is usually how I work out what an Expression
tree actually contains.
Running this will produce
EmployeeId
EmployeeNumber
True
True
showing that we can successfully extract the details of the related properties, and (crucially) they are reference-identical to the PropertyInfo
s obtained by other means. Hopefully you can use this in conjunction with whatever approach you are actually using to specify properties of interest at runtime.