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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:23:55+00:00 2026-06-11T14:23:55+00:00

I’m not sure if this is possible, and I’m quite new to using assemblies

  • 0

I’m not sure if this is possible, and I’m quite new to using assemblies in C#.NET.

What I would like to do is to create an instance of a class when supplied the string name of that class. Something like this:

using MyAssembly;

namespace MyNameSpace
{
     Class MyClass
     {
          int MyValue1;
          int MyValue2;

          public MyClass(string myTypeName)
          {
               foreach(Type type in MyAssembly)
               {
                    if((string)type == myTypeName)
                    {
                         //create a new instance of the type
                    }
               }
               AssignInitialValues(//the type created above)
          }

          //Here I use an abstract type which the type above inherits from
          private void AssignInitialValues(AbstractType myClass)
          {
               this.value1 = myClass.value1;
               this.value2 = myClass.value2;
          }
      }
 }

Obviously you cannot compare strings to types but it illustrates what I’m trying to do: create a type found in a different assembly from a supplied string.

Any thoughts?


EDIT:

After attempting:

var myObject = (AbstractType) Activator.CreateInstance(null, myTypeName);
AssignInitialValues(myObject);

I get a number of errors:

  • Inconsistent accessibility: parameter type ‘MyAssembly.AbstractType’ is less accessible than method ‘MyNameSpace.MyClass.AssignInitialValues(MyAssembly.AstractType)’
  • ‘MyAssembly.AstractType’ is inaccessible due to it’s protection level
  • The type or namespace name ‘MyAssembly’ could not be found (are you missing a using directive or an assembly reference?)
  • The type or namespace name ‘AbstractType’ could not be found (are you missing a using directive or an assembly reference?)

Not exactly sure why it can’t find the assembly; I’ve added a reference to the assembly and I use a Using Directive for the namespace in the assembly. As for the protection level, it’s calling classes (or rather the constructors of classes) which can only be public.

Any clues on where the problem is?


UPDATE:

After looking through several articles on SO I came across this: https://stackoverflow.com/a/1632609/360627
Making the AbstractTypeclass public solved the issue of inconsistent accessibility.

The new compiler error is this:

Cannot convert type ‘System.Runtime.Remoting.ObjectHandle’ to ‘MyAssembly.AbstractType’

The line it references is this one:

var myObject = (AbstractType) Activator.CreateInstance(null, myTypeName);

Using .Unwrap() get’s me past this error and I think it’s the right way to do it (uncertain). However, when running the program I then get a TypeLoadException when this code is called.

TypeLoadException: Could not load type ‘AbstractType’ from assembly ‘MyNameSpace’…

Right away I can spot that the type its looking for is correct but the assembly it’s looking in is wrong. Looking up the Activator.CreateInstance(String, String) method revealed that the null as the first argument means that the method will look in the executing assembly. This is contrary to the required behavior as in the original post.

I’ve tried using MyAssembly as the first argument but this produces the error:

‘MyAssembly’ is a ‘namespace’ but is used like a ‘variable’

I think that this is caused by MyAssembly not being a string. However if I try 'MyAssembly' I get the following compiler errors:

  • Too many characters in character literal
  • The best overloaded method match for ‘System.Activator.CreateInstance(System.ActivationContext, string[])’ has some invalid arguments
  • Argument 1: cannot convert from ‘char’ to ‘System.ActivationContext’
  • Argument 2: cannot convert from ‘string’ to ‘string[]’

Seems to me like it’s trying to use the wrong overload.

Any thoughts on how to fix this?

  • 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-06-11T14:23:56+00:00Added an answer on June 11, 2026 at 2:23 pm

    Okay so this is the answer. I’ve tested it using real code and it works. I’m not sure if it’s the best way or the most efficient way. I’ve upvoted the other two answers because they helped me get onto the right track but here is the full answer.

    using MyAssembly;
    
    namespace MyNameSpace
    {
         Class MyClass
         {
              int MyValue1;
              int MyValue2;
    
              public MyClass(string myTypeName)
              {
                   string assemblyName = Assembly.GetAssembly(typeof(AbstractType)).ToString();
                   AbstractType selectedClass = (AbstractType)Activator.CreateInstance(assemblyName, myTypeName).Unwrap();
                   AssignInitialValues(selectedClass);
              }
    
              private void AssignInitialValues(AbstractType myClass)
              {
                   this.value1 = myClass.value1;
                   this.value2 = myClass.value2;
              }
          }
     }
    

    Took me three days of googling, searching through SO and MSDN but there it is.
    Note that when instantiating a class with Activator.CreateInstance(String, String) the assembly name (first parameter) must be the full assembly name not just the name of the .dll (or .exe).

    The assembly name can be retrieved by using string assemblyName = Assembly.GetAssembly(typeof(AbstractType)).ToString(); where AbstractType can be replaced by a type you know to be contained and defined in the assembly.

    The type name (second parameter) must be the fully qualified name, not just the class name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.