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

  • Home
  • SEARCH
  • 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

In C++0x I would like to write a function like this: template <typename... Types>
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
template<typename T, size_t n> size_t array_size(const T (&)[n]) { return n; } The part
The following code: template <typename S, typename T> struct foo { void bar(); };
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
I have code like this: template <typename T, typename U> struct MyStruct { T
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
Is the following code portable? template<typename In> struct input_sequence_range : public pair<In,In> { input_sequence_range(In
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
Is it possible to typedef long types that use templates? For example: template <typename

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.