Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CreateObject Equivalent
Message
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00635294
Message ID:
00635678
Vues:
16
Rodman!

The short answer is that you need to use late binding to do this. Here's the long answer < s >:

Although you normally access VB.NET objects via strongly typed variables (early binding), VB.NET also allows you to access objects that are late bound by means of variables that are of no specific type (other than Object). For example:

Dim x As Object = New Customer()
Customer.New()

When specified this way, the compiler doesn’t check to see if the Customer class really possesses a New() method. However, even though the compiler doesn’t complain, obviously if the Customer object *doesn’t* have a New() method, you *will* get an error at run time.

You can *instantiate* a late bound object by means of reflection. Basically, reflection requires that you:

1. Import the System.Reflection namespace
2. Load the assembly that contains the class to be instantiated
3. Run CreateInstance to instantiate the object

For example:

Imports System.Reflection

Dim myObj As Object
Dim myDll as Reflection.Assembly

myDll = System.Reflection.Assembly.LoadFrom("..\MyGroovyDll.dll")
myObj = myDll.CreateInstance("MyGroovyDll.MyHappeningClass")

Note that you should use Assembly.Load() if the assembly containing the class you want to instantiate is already referenced in your project, and you can use Assembly.LoadFrom() to reference any assembly that exists on your file system.

The down-side of late binding is a loss of performance since discovery and invokation occurs at runtime rather than compile time.

Often, when I'm trying to give an end user flexibility in classes that are instantiated, a will provide a virtual factory method that can be overridden in derived classes. You can use either abstract base classes or interfaces when creating a family of classes, which still allows you to use strong typing.

Regards,
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform