2

I am trying to obtain all properties from a class returning constant values through reflection. I have seen this to be working for const fields, obviously, but not for properties.

Is there a way to determine if a property is actually returning a constant value?

Sample:

public class Foo
{
    public string Fox => GetSomething(); // should not be returned
    public string Foz {get;} // should not be returned
    public string Bar {get;set;} // should not be returned

    public string Baz => "hello there"; // SHOULD be returned
}

Any effort so far failed. I tried to call GetConstantValue() on the property, but that throws an exception ("InvalidOperationException: 'Literal value was not found.'"). It doesn't seem to be the right method to do this.

Jham
  • 195
  • 12
  • Exception? SQL exception? Overflow Exception? – bichito Apr 09 '18 at 14:38
  • Do you hit the set block? In other words: place a break point inside it to make sure it is hit – bichito Apr 09 '18 at 14:40
  • @efekctive See the update. Not sure what you mean with "how you hit it". It is not relevant to the question. – Jham Apr 09 '18 at 14:41
  • Well, if you don't hit the set block of a property how do you expect it to have a value? First make sure it even executes – bichito Apr 09 '18 at 14:43
  • That is totally irrelevant as said. This is just a mock of a much more complex object. Setting a value is not an issue here. – Jham Apr 09 '18 at 14:45
  • As you please. My bad – bichito Apr 09 '18 at 14:46
  • I'm not sure what you want is achievable through reflection. A property getter is just a parameterless method, which returns an object of an specific type... what that method does is not in the metadata obtained via reflection, as far as I know – Jcl Apr 09 '18 at 14:46
  • Get a list of properties that need some work for a specific programming task I have been given on X million lines. I can narrow doen the properties that need changing very quickly with this option. – Jham Apr 09 '18 at 14:47
  • 2
    I do not think this will be possible without inspecting the IL of the property getter. There is no assembly metadata that specifies whether the return value is constant. When you use the `=> X` syntax the compiler simply generates a getter body that does `return X`. You will have to examine the IL to determine whether the body does anything more than return a value that does not depend on any external factors. This will get tricky. – TypeIA Apr 09 '18 at 14:48
  • That is what I thought too, hoped there was anything that could help here @TypeIA – Jham Apr 09 '18 at 14:48
  • To simplify your code: get { return "CONsTANT";} set{....} No need to to anything else – bichito Apr 09 '18 at 14:52
  • I agree with @TypeIA even though in code the property returns a string literal and it is a read-only. – ngeksyo Apr 09 '18 at 14:53
  • https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getconstantvalue(v=vs.110).aspx. This document does not state that it is returning the Property value. If you use GetValue(Foo instance) works like a charm. Read your docs first – bichito Apr 09 '18 at 15:34
  • Can you explain why you are trying to do this strange thing? This has the smell of an XY problem. – Eric Lippert Apr 09 '18 at 17:37
  • Yes, it is Eric. It was an idea to do a quick workaround to diagnose some (partial) class that holds automatically generated translation entries (file A) and manual entries needed to go in the automated process (file B). This class is derived in a next layer, so I had like to go over all those entries and filter out those I have entered manually. For now, I will just try to parse the C# file myself. – Jham Apr 11 '18 at 14:03

0 Answers0