Object.factory

DanO dsstruthers at nospamplease.yahoo.com
Tue Sep 25 22:31:52 PDT 2007


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



More information about the Digitalmars-d mailing list