C + +, unlike C#,java, is inherently reflective, and can only be implemented at the code level.
1. Definition
1 classClassproperty;2 classObject3 {4 Public:5 intGetintvalue (inti) {returni +1; }6 Public:7 Virtual Constclassproperty*GetProperty () {8 returnGetclasspropertys ();9 }Ten Public: One Static Constclassproperty*Getclasspropertys (); A }; - structClassproperty - { the Private: -typedefvoid* (Object::* Func) (void*); - Public: - Classproperty (): Func (nullptr), desc (nullptr) {} +Template<typename R, TypeName C, TypeName ... Args> -Classproperty (R (C::* FUNCC) (Args ...)Const=0,Const Char* desc =0) : + DESC (desc) { Atypedef R (Object::*Objfunc) (Args ...); atFunc =(Func) (Objfunc) FUNCC; - } -Template<typename R, TypeName C, TypeName ... Args> -Classproperty (R (C::* FUNCC) (Args ...) =0,Const Char* desc =0) : - DESC (desc) { -typedef R (Object::*Objfunc) (Args ...); inFunc =(Func) (Objfunc) FUNCC; - } to Public: +Template<typename R, TypeName ... Args>r invoke (object* obj, Args&& .... Args)Const - { thetypedef R (Object::*Objfunc) (Args ...); *Objfunc MF =(Objfunc) func; $ return(OBJ->*MF) (std::forward<args>(args) ...);Panax Notoginseng } - func func; the Const Char*desc; + }; A Constclassproperty*Object::getclasspropertys () the { + Const StaticClassproperty prop[] = {{&object::getintvalue,"Getintvalue"},classproperty ()}; - returnprop; $}
1. Role
1object* obj =NewObject ();2 Constclassproperty* prop = obj->GetProperty ();3 while(Prop->func! =nullptr) {4Std::cout << Prop->desc <<Std::endl;5 intRET = prop->invoke<int,int> (obj,2);6Std::cout << ret <<Std::endl;7prop++;8 }9 DeleteObj
3. Results
Getintvalue
2
Properties && Reflections