I struggled with all these lambdas and cool System :: Linq spaces (including the CLinq library and other things) for several hours, and then ... I just thought: why not use a purely static solution?
We just announce
/// Convert X to "X", the classical preprocessing trick
and then we can do
Console::WriteLine( GetPropName(TimeZone, Id) );
to get the "Id" printed on the screen.
Yes ... Now the interesting part. Type of security. I hear the storm of comment coming to this decision ("NO! It's not good, it does not check if ThePropertyName is in the class!")
OK Solution: create meaningless code using a macro that uses the value ThePropertyName in a dummy instance of TheClassName.
/// This macro will produce the compilation error if ThePropertyName is not in the class named TheClassName #define CheckForPropertyExistence(TheClassName, ThePropertyName) \ /* Create an array of Objects which will be converted to string and ignored*/ \ (gcnew array<System::Object^> { (gcnew TheClassName())->ThePropertyName })->ToString() /// We get the property name using the "dinosaur strategy" - good old macro concatenated with the empty string which in turn is formed in CheckFor() macro #define GetPropertyName(TheClassName, ThePropertyName) \ (gcnew System::String(
Now we can give a complete sample:
using namespace System;
source share