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 731685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:03:19+00:00 2026-05-14T07:03:19+00:00

sTypeName = … //do some string stuff here to get the name of the

  • 0
sTypeName = ... //do some string stuff here to get the name of the type

/*
The Assembly.CreateInstance function returns a type
of System.object. I want to type cast it to 
the type whose name is sTypeName.

assembly.CreateInstance(sTypeName)

So, in effect I want to do something like:

*/

assembly.CreateInstance(sTypeName) as Type.GetType(sTypeName);

How do I do that? And, what do I take on the left side of the assignment expression, assuming this is C# 2.0. I don’t have the var keyword.

  • 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-14T07:03:19+00:00Added an answer on May 14, 2026 at 7:03 am

    Usually you let all classes, you want to instantiate this dynamically, implement a common interface, lets say IMyInterface. You can create an instance from the classname string like this:

    Assembly asm = Assembly.GetExecutingAssembly();
    string classname = "MyNamespace.MyClass";
    Type classtype = asm.GetType(classname);
    
    // Constructor without parameters
    IMyInterface instance = (IMyInterface)Activator.CreateInstance(classtype);
    
    // With parameters (eg. first: string, second: int):
    IMyInterface instance = (IMyInterface)Activator.CreateInstance(classtype, 
                            new object[]{
                                (object)"param1",
                                (object)5
                            });
    

    Even if you dont have a common interface, but know the name of the method (as string) you can invoke your methods like this (very similar for properties, event and so on):

    object instance = Activator.CreateInstance(classtype);
    
    int result = (int)classtype.GetMethod("TwoTimes").Invoke(instance, 
                            new object[] { 15 });
    // result = 30
    

    The example class:

    namespace MyNamespace
    {
        public class MyClass
        {
            public MyClass(string s, int i) { }
    
            public int TwoTimes(int i)
            {
                return i * 2;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This can create an array dynamically: Assembly asm = object.GetType().Assembly; string sTypeName = Company.Namespace.ClassName;
Consider: template < typename Something > boost::function<void()> f() { typedef typename Something::what type; return
Degrafa newbie here :-). I was able to get com.degrafa.skins.CSSSkin to create linear gradient
Suppose we only have a string string typename = int How can we get
How do i get the file type in Mono? i.e. *.txt => Text Document
following background: i have a Table-valued Function that returns a Table that is a
Is it possible to get typename of a member variable? For example: struct C
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Would you please help me getting back the typename of the original object put
public void valueChanged(TreeSelectionEvent event) { //Add images depending on selection. String selection = navigation.getLastSelectedPathComponent().toString();

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.