I need runtime reflection

bitwise bitwise.pvt at gmail.com
Fri Sep 29 17:32:06 UTC 2017


On Friday, 29 September 2017 at 16:40:38 UTC, Gheorghe Gabriel 
wrote:
> On Friday, 29 September 2017 at 16:24:32 UTC, bitwise wrote:
>> On Friday, 29 September 2017 at 11:05:00 UTC, Gheorghe Gabriel 
>> wrote:
>>> [...]
>>> If i compile this script to a .dll
>>
>> DLL support for D is currently very spotty. Before investing 
>> too much time, I would suggest confirming that DLLs are even 
>> properly supported for your target platform at all.
>
> So, could you upload a copy of your own reflection module? I 
> want to try it and then I will send you my feedback.

I'm reworking how properties/fields are set, and how functions 
are called. I'm not sure how long it will take. I'll throw a copy 
up when it's done.

Simple reflection is not that hard though:

`
ClassReflection[string] _reflections;

abstract class ClassReflection
{
     Object create() const;
     string name() const;
}

class ClassReflectionImpl(T) : ClassReflection
{
     import std.traits : fullyQualifiedName;
     static this() {
         _reflections[fullyQualifiedName!T] = new typeof(this);
     }

     override Object create() const { return new T; }
     override string name() const { return T.stringof; }
}

template registerClass(T) {
     alias registerClass = ClassReflectionImpl!T;
}

export extern(C) ClassReflection reflectionOf(string name) {
     ClassReflection* pRefl = name in _reflections;
     return pRefl ? *pRefl : null;
}

class Player {
     void speak() { writeln("hello world"); }
}

alias registration = registerClass!Player;

int main(string[] argv)
{
     ClassReflection playerRefl = reflectionOf("main.Player");
     ClassReflection missingRefl = 
reflectionOf("main.MissingClass");
     assert(playerRefl !is null);
     assert(missingRefl is null);

     Player player = cast(Player)playerRefl.create();
     assert(player !is null);
     player.speak();

     return 0;
}
`

You can use __traits and std.traits to build whatever you need 
into ClassReflection.




More information about the Digitalmars-d mailing list