Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:36:39+00:00 2026-05-16T03:36:39+00:00

Is there a way to dynamically create an object using a string as the

  • 0

Is there a way to dynamically create an object using a string as the class name?

I’ve been off VB for several years now, but to solve a problem in another language, I’m forced to develop a wrapper in this one. I have a factory method to dynamically create and return an object of a type based on input from elsewhere. The provided input is meant to be the class name from which to create an object from. Normal syntax means that the entire class has to be explicitly spelled out. To do it this way, there could literally be hundreds of if/then’s or cases to handle all the available class/object choices within the referenced libs:

If c_name = "Button" then obj = new System.Windows.Forms.Button
If c_name = "Form" then obj = new System.Windows.Forms.Form
....

I’m hoping instead to reduce all this case handling to a single line: IE…

my_class_name = "whateverclass"
obj = new System.Windows.Forms.my_class_name()

In PHP, this is handled like so…

$my_class_name = "whateverclass";
$obj = new $my_class_name();

Edit: Looking at some of the answers, I think I’m in way over my head here. I did manage to get it working using this CreateInstance method variation of the Assembly class, even though I’m more interested in this variation giving more options, including supplying construct parameters…

my_type_name = "System.Windows.Forms.Button"
asmb_name = "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
button1 = Reflection.Assembly.Load(asmb_name).CreateInstance(my_type_name)

In other words, it takes a method to do this, and not any inherent language syntax? This Activator variation also worked when the full assembly string and class path is used. I’m suspicious CreateInstance may not have the full ability to let me treat objects as if they were called normally, ie obj = new System.Windows.Forms.Button. This is why I can’t use simply CreateObject. If there is no natural language feature allowing you to substitute a class name for a string, does anyone have any insight into what sort of limitations I can expect from using CreateInstance?

Also, is there even a difference between basic Activator.CreateInstance (after Unwrap) and Assembly.CreateInstance methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:36:40+00:00Added an answer on May 16, 2026 at 3:36 am

    This will likely do what you want / tested working; switch the type comment at the top to see.

    Imports System.Reflection
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '            Dim fullyQualifiedClassName as String = "System.Windows.Forms.TextBox"
        Dim fullyQualifiedClassName As String = "System.Windows.Forms.Button"
        Dim o = fetchInstance(fullyQualifiedClassName)
        ' sometime later where you can narrow down the type or interface...
        Dim b = CType(o, Control)
        b.Text = "test"
        b.Top = 10
        b.Left = 10
        Controls.Add(b)
    End Sub
    
    Private Function fetchInstance(ByVal fullyQualifiedClassName As String) As Object
        Dim nspc As String = fullyQualifiedClassName.Substring(0, fullyQualifiedClassName.LastIndexOf("."c))
        Dim o As Object = Nothing
        Try
            For Each ay In Assembly.GetExecutingAssembly().GetReferencedAssemblies()
                If (ay.Name = nspc) Then
                    o = Assembly.Load(ay).CreateInstance(fullyQualifiedClassName)
                    Exit For
                End If
            Next
        Catch
        End Try
        Return o
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a (better) way to dynamically create Objects? Right now I'm using a
Is there a way to dynamically and conditionally create a class definition in PHP,
Is there a way to dynamically create a selectItem list? I dont really want
Is there a way to dynamically create a kind of sub-domain that goes between
Is there a way to automatically/dynamically set all the attributes for a datamapper object
Is there a way to create a object without the use of new keyword
Is there a way to create a colored fill pattern dynamically in Cocoa? In
Question: Is there a way to make a function object in python using strings?
Is there a way to create WCF proxy objects on the fly without using
Is there a way to mock object construction using JMock in Java? For example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.