www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - reflection: instantiation of objects via class names at runtime

reply =?UTF-8?B?VGhvbWFzIEvDvGhuZQ==?= <thomas-dloop kuehne.cn> writes:
Content-Type: multipart/mixed;
 boundary="------------090200080307090204020806"

This is a multi-part message in MIME format.
--------------090200080307090204020806
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

The attached code allows the instantiation of objects at runtime
requiring only the fully qualified class name.

sample:
#
# import cn.kuehne.reflection;
# ...
# Object o =3D new_Instance("std.stream.File");
#

limitations:
1) Windows isn't yet supported.
2) The class has to have an explicit "this()" constructor.

compilation:
1) gcc -m32 -c elf.c
2) dmd -c reflection.d
3) dmd <what ever> elf.o reflection.o

Thomas

--------------090200080307090204020806
Content-Type: application/zip;
 name="reflection.zip"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filename="reflection.zip"

UEsDBBQAAgAIALqDgzXFgok6HgsAAJweAAAMABUAcmVmbGVjdGlvbi5kVVQJAANw7XJFcO1y
RVV4BADoA2QAvVltcxu3Ef5M/gqUreOjQp0k27U9opVEpuSM2sb2WPKMxxzNzfEIkmfdWwGc
JLa1f1m/9Y/12QXujaRTT9ppkmEOwO6zL9hdLKCDPaHkIpGRifPMn4v9faFNORP5ojUvdFkU
uTJikSthVlKciULlSxWmaZwtRRJmyzJcyn5fiFupNHE8Ojx8un/0aP/wMc1O8mKt4uXKCG8y
5DVxtcrTUIs//+ufq0yKF4aHP92UEkM/yn4gtqtVrIXOF+YuVFLgG1Jv47mci4eh3o/1w5G4
i8FZGhFmayHvCyW1FlAyTosklnNggFWFmVn7QlxkIsuFvJWZAV+SsClhCQClxUyKlUzmIonD
WSLZUsKchyks0wAKVazJ2oXKU+YstSQ3mbaSPqn9Vqo01uwGLC1JPFQ2OQHmMBZfxNthrAUW
pSpyLUfAibMoKeckM8rTVKooDhMRFrAsCmlb9AgcFjgxEkYbHis5j7VR8aw0cJoB0EJJmaxH
2MbZJ+wocZABizxJ8jv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5ifkmUV6Am4OuUP4rMSJnicZguHI7eDiE8u8RyQBVB50sMGixO
z86H6+hP80E+1PIJRjqal8PMDvalqV1PQhEj87tbv7/B9d2uj7fCF3QX08KtycuWuIQyanAE
OkEyF7Gzsok3urepMAE3IVdoUyspo3H6jaKdq69ML/vOcTh+hrsVWpVvl+WJHY4bL5JfKjgm
Arbbo/xdxQs7XAClOLjAAMOb1rfHLv5R2n1srXvUWn/EPw9/9+wHVlnLW+lwalfz+b5q3iCr
oLeYWJiz8GCnzf3KgZxXa7Rm13zS9dudL0a4uda2pV29NCZgQ4mIvGjuFBVoj1u56b99Ozw/
RSC7L4zzdyq2j8+PRqOd7dqIsW02vzFM9q61Z5fzLNrbdj08mxgA4+USLb+GtRtevi6fnf1m
98dn//Z/nf1m94dn5ye9QiX+Y8bRKt7uDPLFNe47qsWkot3tf8nfeaYeTKHaaricfDq5vDj/
bNJqyshstlSbuofFu8WqyiL1Mn/Ivu5J+U6spzRf2besQfLCRAnHfGfDwK65uCHhH/bF03yA
llL/dcJ3PsLvU9M1ip8ZH89OGo3cY9ydufQdM2SL/cKxBn6OsM5qIXihZNtAXc/KqqrsQINK
LV7ydq2RYA1XOPWO6RT2wnMmyrT+F1BLAQIXAxQAAgAIALqDgzXFgok6HgsAAJweAAAMAA0A
AAAAAAEAAACwgQAAAAByZWZsZWN0aW9uLmRVVAUAA3DtckVVeAAAUEsBAhcDFAACAAgAQgZu
Na+uZ86zBwAAmBIAAAUADQAAAAAAAQAAAKCBXQsAAGVsZi5jVVQFAAOrBFlFVXgAAFBLBQYA
AAAAAgACAIcAAABIEwAAAAA=
--------------090200080307090204020806--
Dec 03 2006
parent Lionello Lunesu <lio lunesu.remove.com> writes:
Awesome!
Dec 04 2006