Thread overview |
---|
June 25, 2009 Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
The Dmd2.030 spec. said: static Object factory(string classname); Create instance of class specified by classname. The class must either have no constructors or have a default constructor. and said also: class ClassInfo static ClassInfo find(in char[] classname); Search all modules for ClassInfo corresponding to classname. Returns: null if not found Object create(); Create instance of Object represented by 'this'. In below code snippet: class Dog { public void bark(){} } int main(string[] args) { auto obj=Object.factory("Dog"); Dog doggie=cast(Dog)obj; doggie.bark; return 0; } Compiled successfully but failed when run: Object.Error:Access Violation. I also tried ClassInfo.find(string classname),ClassInfo.create() but caused the same error. In the above code snippet,if I wrote as below: class Dog{...} int main(string[] args) { Object obj=Object.factory("Dog"); obj=new Dog; //1 Dog doggie=cast(Dog)obj;//2 doggie.bark; return 0; } Everything works fine.But it seems that this does not make sense to dynamic create class instance.It is just the same as below: Object obj; obj=new Dog; (cast(Dog)obj).bark; So is there any other options can I reach the goal,i.e.,dynamically create an class instance and invoke its method? Thanks in advance. Regards, Sam |
June 25, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu Wrote:
> The Dmd2.030 spec. said:
>
> static Object factory(string classname); Create instance of class specified by classname. The class must either have no constructors or have a default constructor.
>
> and said also:
> class ClassInfo
> static ClassInfo find(in char[] classname);
> Search all modules for ClassInfo corresponding to classname.
>
> Returns:
> null if not found
>
> Object create(); Create instance of Object represented by 'this'.
>
> In below code snippet:
>
> class Dog
> {
> public void bark(){}
> }
> int main(string[] args)
> {
> auto obj=Object.factory("Dog");
> Dog doggie=cast(Dog)obj;
> doggie.bark;
>
> return 0;
> }
>
> Compiled successfully but failed when run: Object.Error:Access Violation.
>
> I also tried ClassInfo.find(string classname),ClassInfo.create() but caused the same error.
>
> In the above code snippet,if I wrote as below:
> class Dog{...}
> int main(string[] args)
> {
> Object obj=Object.factory("Dog");
> obj=new Dog; //1
> Dog doggie=cast(Dog)obj;//2
> doggie.bark;
>
> return 0;
> }
> Everything works fine.But it seems that this does not make sense to dynamic create class instance.It is just the same as below:
> Object obj;
> obj=new Dog;
> (cast(Dog)obj).bark;
>
> So is there any other options can I reach the goal,i.e.,dynamically create an class instance and invoke its method?
>
> Thanks in advance.
>
> Regards,
> Sam
>
>
BTW,why __traits has no isStaticFunction?
Thanks in advance.
|
June 25, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu Wrote:
>
> In below code snippet:
>
> class Dog
> {
> public void bark(){}
> }
> int main(string[] args)
> {
> auto obj=Object.factory("Dog");
> Dog doggie=cast(Dog)obj;
> doggie.bark;
>
> return 0;
> }
>
> Compiled successfully but failed when run: Object.Error:Access Violation.
>
> I also tried ClassInfo.find(string classname),ClassInfo.create() but caused the same error.
>
Object.factory requires a fully qualified class name, so if class Dog resides in module animals, call Object.factory("animals.Dog").
|
June 26, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C Wrote:
> Object.factory requires a fully qualified class name, so if class Dog resides in module animals, call Object.factory("animals.Dog").
It works now.Great!Thanks so much!
But...may I ask how do you know that?(requires full name).I went through the object.di but nothing relative found.
|
June 26, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu escribió: > John C Wrote: > >> Object.factory requires a fully qualified class name, so if class Dog resides in module animals, call Object.factory("animals.Dog"). > > It works now.Great!Thanks so much! > > But...may I ask how do you know that?(requires full name).I went through the object.di but nothing relative found. There: http://d.puremagic.com/issues/show_bug.cgi?id=3093 |
June 26, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig Wrote:
> Sam Hu escribi?
> > John C Wrote:
> >
> >> Object.factory requires a fully qualified class name, so if class Dog resides in module animals, call Object.factory("animals.Dog").
> >
> > It works now.Great!Thanks so much!
> >
> > But...may I ask how do you know that?(requires full name).I went through the object.di but nothing relative found.
>
> There: http://d.puremagic.com/issues/show_bug.cgi?id=3093
Thanks a lot.
I found the reflection(object module + __traits) of the current version of D2 is very powerful already .Wondering how come some ppl here felt that D is still lack of reflection?
|
June 26, 2009 Re: Object.factory create instance failed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sam Hu Wrote:
> John C Wrote:
>
> > Object.factory requires a fully qualified class name, so if class Dog resides in module animals, call Object.factory("animals.Dog").
>
> It works now.Great!Thanks so much!
>
> But...may I ask how do you know that?(requires full name).I went through the object.di but nothing relative found.
>
Common sense, to be honest.
|
Copyright © 1999-2021 by the D Language Foundation