4

Please imagine having following two properties inside a class:

public string Category { get; set; }
public string DisplayCategory => "SomeCategory"

Now I just want to collect all PropertyInfo objects where the property itself is not computed

var properties = type.GetProperties();
var serializables = properties.Where(p => p.CanRead, true));

How do I find out via Reflection if a property is a computed one so I can ignore it?

The reason I want to do this, is because I use Expression Trees to automatically create queries that are processed via Entity Framework 6. Entity Framework only creates columns for non-computed properties and therefore one can't query the computed ones.

See This Article

6
  • 4
    What exactly would count as "computed"? Anything that isn't a compile-time constant? What about if it were something like reading a static readonly field which isn't a constant, but is initialized just once?
    – Jon Skeet
    Sep 26, 2016 at 11:30
  • @Jon Every getter that has some kind of logic behind it instead behaving like a Field. I just want the remaining Properties which can be mapped by Entity Framework and also queried by using the Expression Framework Sep 26, 2016 at 11:32
  • 5
    What counts as "some kind of logic"? Would two properties that both retrieve the same field count as non-computed, even though they reflect a single piece of state? What about a property where the getter just reads a field, but the setter performs validation? What about a property where the getter reads a field and then takes a property from that, e.g. public int FooLength => foo.Length;? What about a property which just reads another property instead of a field? There are all kinds of subtleties possible here...
    – Jon Skeet
    Sep 26, 2016 at 11:34
  • Why shouldn´t EF handle properties with a "complex" body? I guess as long as its is a property (eventually decorated with attributes, I´m no expert on EF) EF can handle it, can´t it? Sep 26, 2016 at 11:35
  • Which of the two properties should be matched? If both could you provide an example of a property that should not be matched? Sep 26, 2016 at 12:08

2 Answers 2

2

The below worked for me.

I was trying to see if I could find a difference between a computed property, and a property with no Set method:

public int Computed => 2;
public int NoSet { get; }

PropertyInfo.SetMethod returns null for both.

Looking at the generated IL of the get method, There is a CompilerGeneratedAttribute on the NoSet property, but not on the Computed property:

.method public hidebysig specialname instance int32
  get_Computed() cil managed
{
  .maxstack 8

  // [215 32 - 215 33]
  IL_0000: ldc.i4.2
  IL_0001: ret
}

.method public hidebysig specialname instance int32
  get_NoSet() cil managed
{
  .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
    = (01 00 00 00 )
  .maxstack 8

  // [217 28 - 217 32]
  IL_0000: ldarg.0      // this
  IL_0001: ldfld        int32 TestNamespace.TestClass::'<NoSet>k__BackingField'
  IL_0006: ret

}

So the check becomes:

if (property.SetMethod == null && property.GetMethod.GetCustomAttribute(typeof(CompilerGeneratedAttribute)) == null)
    return "Computed Property";
1

I use DelegateDecompiler and currently I have to manually add each computed property name to a custom configuration.

It would be nice if the ShouldDecompile method below could automatically determine which properties should be decompiled:

  public class CustomDelegateDecompilerConfiguration : Configuration {
    public static CustomDelegateDecompilerConfiguration Instance { get; } = new CustomDelegateDecompilerConfiguration();
    public static void Enable() => Configuration.Configure(Instance);

    public CustomDelegateDecompilerConfiguration() {
      RegisterDecompileableMember<Person, string>(x => x.Name);

      RegisterDecompileableMembers(typeof(string), nameof(string.IsNullOrWhiteSpace));

      RegisterDecompileableMembers(typeof(CustomComputedMethods), new[] {
        nameof(CustomComputedMethods.PersonName),
        nameof(CustomComputedMethods.MonthInteger),
        nameof(CustomComputedMethods.WholeMonthsBetween),
        nameof(CustomComputedMethods.WholeYearsBetween)
      });

    }

    public HashSet<MemberInfo> DecompileableMembers { get; } = new HashSet<MemberInfo>();

    public override bool ShouldDecompile(MemberInfo memberInfo) => memberInfo.GetCustomAttributes(typeof(DecompileAttribute), true).Length > 0
      || memberInfo.GetCustomAttributes(typeof(ComputedAttribute), true).Length > 0
      || memberInfo.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length > 0
      || DecompileableMembers.Contains(memberInfo)
      //***  Would be nice if auto detection was possible with non-existing methods below ***
      //|| memberInfo.AutoProperty      (One with a backing field automatically generated by the compiler)
      //|| memberInfo.HasExpressionBody (One implemented using the => (lambda) syntax)
      //|| memberInfo.HasFunctionBody   (One implemented using the normal {...} syntax)
      ;

    public override void RegisterDecompileableMember(MemberInfo prop) => DecompileableMembers.Add(prop);

    public void RegisterDecompileableMember<T, TResult>(Expression<Func<T, TResult>> expression) where T : class => RegisterDecompileableMember(expression.Body.GetMemberInfo());

    public void RegisterDecompileableMembers(Type type, params string[] memberNames) {
      foreach(var tmi in type.GetMembers().Where(mi => memberNames.Contains(mi.Name))) {
        DecompileableMembers.Add(tmi);
      }
    }

    public void RegisterDecompileableMembers<T>(params string[] memberNames) where T : class => RegisterDecompileableMembers(typeof(T), memberNames);

  }

An example class:

  public class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string AlternativeFirstName { get; set; }

    public string Name => string.Concat(AlternativeFirstName == string.Empty ? FirstName : AlternativeFirstName, " ", LastName);
  }

Some example extension Methods:

  public static class CustomComputedMethods {
    public static string PersonName(string firstName, string lastName, string knownAs) => (knownAs ?? firstName).Trim() + " " + lastName.Trim();
    public static long MonthInteger(this DateTime d) => checked(d.Year * 12 + d.Month);
    public static int WholeMonthsBetween(this DateTime d, DateTime maxDate) => (int)(maxDate.MonthInteger() - d.MonthInteger() - (d.Day > maxDate.Day ? 1 : 0));
    public static int WholeYearsBetween(this DateTime d, DateTime maxDate) => d.WholeMonthsBetween(maxDate) / 12;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.