TypeInfo_Interface from runtime string?
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 22 08:43:08 PDT 2016
On Wednesday, 22 June 2016 at 15:15:51 UTC, Thalamus wrote:
> Hi everyone,
>
> My project includes lots of .Net interop via C linkage. One of
> the things I need to do is refer in C# to an interface declared
> in the D code, and then to actually work with the interface
> concretely in the D layer. So, I need to get a
> TypeInfo_Interface object from a string passed in from C#.
>
> The problem isn't in marshaling the string between C# and D,
> but rather what to do with the string once I have it in D.
>
> So in the D code, where interfaceName is the fully qualified
> name of an interface, e.g. "MyPackage.MyModule.MyInterface",
> what I would like is something like:
>
>
> TypeInfo_Interface theInterface = new
> TypeInfo_Interface(interfaceName);
>
>
> But there's no such constructor.
No need for a constructor. typeid() returns a static instance
that's pre-allocated.
> Apologies if this seems like it should be obvious, but I
> couldn't find anything in the forums or the wider web. :)
No problem. What you need to do is to create a registry with all
the possible TypeInfo_Interfaces. This registry will have the
form of an associative array. Each TypeInfo_Interface will be
selectable with the fully qualified string, for example:
__gshared TypeInfo_Interface[string] registry;
interface Foo{}
interface Bar{}
static this()
{
registry[typeid(Foo).toString] = typeid(Foo);
registry[typeid(Bar).toString] = typeid(Bar);
}
That's the basic idea but with introspection (foreach(member;
traits....) you should be able to automate the creation of the
registry, in a smarter way.
More information about the Digitalmars-d-learn
mailing list