Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cloning Objects
Message
De
16/01/2004 05:51:32
 
 
À
15/01/2004 12:32:09
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Divers
Thread ID:
00867149
Message ID:
00867396
Vues:
17
Hi Bonnie

That seems a bit overkill for what I want to do, in a nutshell all I want to do is request (from a class) a new instance of that class, not even a clone, just a new instance, but without needing to know the class.

Something like:
public class ParentClass
     {
          public ParentClass GetNewInstance()
               {
                    return new blah blah ;
               }
     }
Then any derived class will inherit this feature but will return a new instance of the current class rather than the parent.

The situation is that I might have a collection of classes based on ParentClass and I want to grab new instances of them, I won't know at runtime what the classes will be as there will be a range from them, so I would like just to be able to call GetNewInstance() on the class in the collection.

Hope that makes sense.

Thanks
Kev


>Kevin,
>
>If I understand your question correctly, you need to use Reflection, something like this:
>
>string DLLName = "MyDLL";
>string ClassName = "MyClass";
>System.Reflection.Assembly oDLL;
>object oClass;
>
>// This "loads" the .DLL
>oDLL = System.Reflection.Assembly.LoadFrom(DLLName);
>
>// This creates the class instance
>oClass = oDLL.CreateInstance(ClassName);
>
>
>~~Bonnie
>
>
>>Hi
>>
>>I have a derived class and I want the parent class to be ICloneable, the only problem is, how do I clone a derived class if I don't know what the type might be? With VFP (as an example) I could do:
>>
>>
>>RETURN CREATEOBJECT(this.Class)
>>
>>
>>Any ideas?
>>Thanks
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform