Object.factory
Daniel Keep
daniel.keep.lists at gmail.com
Tue Sep 25 23:40:21 PDT 2007
DanO wrote:
> Sean Kelly Wrote:
>
>> DanO wrote:
>>>
>>> I have found that this works quite well for non-templated classes, but templated classes don't seem to behave. I am using the ClassInfo's name property to get the string, and that doesn't work.
>>>
>>> <code>
>>> class TDict(T)
>>> {
>>> T[char[]] dict;
>>> }
>>>
>>> char[] tInfoName = TDict!(int).classinfo.name; // returns TDict!(int).TDict
>>>
>>> Object o = Object.factory(tInfoName); // returns null
>>> </code>
>>>
>>> I have not tried 'Object.factory("TDict!(int)")'; it may work just fine.
>>>
>>> Anyone have any idea if this is supposed to work or is supported?
>> Try calling:
>>
>> TDict!(int).classinfo.create();
>>
>> Instead. ClassInfo also has a static find(char[]) method to perform
>> lookups without going through Object. Personally, I've never seen a
>> reason to have the Obejct.factory() method, given what's in ClassInfo.
>>
>>
>> Sean
>
> Your solution doesn't help, since I really need to be able to use the class name to instantiate the object. I am doing serialization and I don't know the object's class. I have tried the ClassInfo.find(str).create() approach, and the doesn't work either. I'm sure Object and ClassInfo are using the same database.
>
> Whatever the problem, I was just trying to remove the need to register my own factory function for those types, but unless someone replies with some more help, I will just use my tried and true method.
>
> -DanO
This seems to be a limitation with templated classes, so have you tried
non-templated?
class TDict!(T) { ... }
class IntDict : TDict!(int) { mixin TDict!(int).ctors; }
auto instance = Object.factory("IntDict");
This might be a pain in the arse, but it might work. The other thing to
remember is that TDict *does not* generate any code. Only specific
instances do. So even if you could instantiate a templated class via
Object.factory, you'd still likely need to manually specify a finite
list of classes that should be instantiated.
-- Daniel
More information about the Digitalmars-d
mailing list