Walczyłem z tymi wszystkimi lambdami i fajnymi przestrzeniami nazw System :: Linq (łącznie z biblioteką CLinq i innymi rzeczami) na kilka godzin, a potem ... Pomyślałem: dlaczego nie zastosować decyzji czysto statycznej?
Po prostu zadeklarować
/// Convert X to "X", the classical preprocessing trick
#define GetPropName(TheClassName, ThePropertyName) #ThePropertyName
i wtedy możemy zrobić
Console::WriteLine(GetPropName(TimeZone, Id));
dostać "ID" drukowane na ekranie.
Taa ... Teraz ta interesująca część. Bezpieczeństwo typu. Słyszałem burzę komentarzy dotyczącą tego rozwiązania ("NIE, to nie jest dobre, nie sprawdza, czy nazwaPropertyName jest w klasie!")
OK. Rozwiązanie: utwórzmy trochę bezsensownego kodu, używając makra, które używa nazwy ThePropertyName w fikcyjnej instancji 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(#ThePropertyName)) + CheckForPropertyExistence(TheClassName, ThePropertyName)->Substring(0,0)
Teraz możemy dać kompletny próbki:
using namespace System;
/// Sample class
public ref class TheTimeZone
{
public:
TheTimeZone()
{
_Id = 0;
_DisplayName = "tmp";
}
property int Id
{
public:
int get() {return _Id;}
void set(int v) { _Id = v; }
}
property String^ DisplayName
{
public:
String^ get() { return _DisplayName; }
void set(String^ v) { _DisplayName = v; }
}
private:
int _Id;
String^ _DisplayName;
};
/// This macro will produce the 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(#ThePropertyName)) + \
CheckForPropertyExistence(TheClassName, ThePropertyName)->Substring(0,0)
/// To get properties from objects with no default constructor
#define GetPropertyNameForObject(TheObject, ThePropertyName) \
(gcnew System::String(#ThePropertyName)) + \
(gcnew array<System::Object^> { (TheObject)-> ThePropertyName })->ToString()->Substring(0,0)
/// Test for our macros
int main(array<System::String ^> ^args)
{
/// Prints "Length"
/// We cannot use default constructor here
Console::WriteLine(GetPropertyNameForObject (gcnew System::String("test"), Length));
/// Prints "Id"
Console::WriteLine(GetPropertyName (TheTimeZone, Id));
/// Uncomment and get the error
//Console::WriteLine(GetPropertyName (TheTimeZone, Id23));
return 0;
}
Całkiem piękne/hacky ... Nie możesz udekorować CheckForPropertyExistence z '1? NULL: yourcode', tak aby w czasie wykonywania były tworzone dwa obiekty mniej? – xanatos
@xanatos: Tak, wydaje się to dobrym pomysłem. Minęły trzy lata, nie pamiętam dokładnego rozumowania, aby stworzyć obiekt. Może po prostu "znalezione rozwiązanie!" podniecenie przeszkodziło mi w robieniu tego, co proponujesz. –