Converting member variables to strings with using reflection from base class
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Dec 22 22:09:05 UTC 2017
On Fri, Dec 22, 2017 at 09:13:31PM +0000, kerdemdemir via Digitalmars-d-learn wrote:
> I want to make a logging function for member variables by using reflection.
[...]
> class B
> {
> void Log()
> {
> auto a = [__traits(derivedMembers, D)];
> foreach(memberName; a) {
> // Somehow write only member variables with their names
> // Result should be : a = 4.0, b = 3.0
Try this:
import std.traits : FieldNameTuple;
foreach (memberName; FieldNameTuple!B) {
writefln("%s = %s", memberName, mixin("this." ~ memberName));
}
T
--
People walk. Computers run.
More information about the Digitalmars-d-learn
mailing list