none
Could not load type from assembly

    Question

  • Hi,

    I'm using the following c# code to cenvert a string to a class name, but I'm getting an error:

    c# code:

    Type hai = Type.GetType("QReporting.Projects.Cy.ByStartDateAndEndDate.Default", true);
                Object o = System.Activator.CreateInstance(Type.GetType(dll));

    Error message:

    Could not load type 'QReporting.Projects.Cy.ByStartDateAndEndDate.Default' from assembly 'App_Web_bc1b3avx, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

    Please, can you explain to me what I should do in this case?

    Regards,

    Bader

    Sunday, February 26, 2012 9:18 AM

Answers

  • Sorry, the Assembly.LoadForm method will be easier to use than the Assembly.Load in my eyes, so I used it to demo to you: 

                Assembly assembly = Assembly.LoadFrom(@"D:\Projects\visual studio 2010\Console\ConsoleApplication32\ConsoleApplication32\bin\Debug\ConsoleApplication32.exe");
                Type hai = assembly.GetType("ConsoleApplication32.NextDerivedClass", true);
                Object o = System.Activator.CreateInstance(hai);
    
    
    
    //++++++++++++++++++++++++++++++++++++++++
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace ConsoleApplication32
    {
        class Program
        {
            public static void MethodABC() { }
            static void Main(string[] args)
            {
                var test = new NextDerivedClass();
            }
        }
    
        internal class BaseClass 
        {
            internal string BaseClassProperty { get; set; }
            
            internal string BaseClassProperty1 { get; set; }
            internal string BaseClassProperty2 { get; set; }
            internal string BaseClassProperty3 { get; set; }
        }
    
        internal class NextDerivedClass
        {
            internal string NextDerivedClassProperty { get; set; }
            internal string NextDerivedClassProperty1 { get; set; }
            internal string NextDerivedClassProperty2 { get; set; }
            internal string NextDerivedClassProperty3 { get; set; }
        }
    }
     

    The error you got is because of the parameter should be the full identity for that assembly. http://msdn.microsoft.com/en-us/library/ky3942xh.aspx , you will need to install the assembly to GAC first.

    So for easier test, I just used the Assembly.LoadForm method.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us




    Tuesday, February 28, 2012 1:16 PM
    Moderator

All replies

  • http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx

    You will need to use the Assembly.Load method to load the assembly which contains the "QReporting.Projects.Cy.ByStartDateAndEndDate.Default" class if you have not loaded it.

    And please also ensure your class name is correct.

     

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 6:44 AM
    Moderator
  • Hi,

    Thank you for your reply,

    Here is my modified code:

    Object o = new Object();
                Assembly asm = Assembly.Load("QReporting");
                if (asm != null)
                {
                    Type hai = asm.GetType();
                    o = System.Activator.CreateInstance(hai); 
                }

    But I'm getting a Server Error: No parameterless constructor defined for this object.

    Note: The "QReporting" is an assembly that contains some namespaces, such as, 'QReporting.Projects.Cy.ByStartDateAndEndDate'. 

    I looked alot on the internet for a solution but I didn't found any thing which help me,

    So, can you please explain to me why I get the above error and how to fix It?

    Another question, can you please tell me how can I refere to 'QReporting.Projects.Cy.ByStartDateAndEndDate' as an object, for example, I need to create an instance of it as bellow:

    new QReporting.Projects.Cy.ByStartDateAndEndDate.Default()

    Please note that Default is a Report object.

    Please, I need an immediate help,

    Regards,

    Bader


    • Edited by Nadim2522 Tuesday, February 28, 2012 1:29 PM
    Monday, February 27, 2012 9:51 AM
  • Sorry, the Assembly.LoadForm method will be easier to use than the Assembly.Load in my eyes, so I used it to demo to you: 

                Assembly assembly = Assembly.LoadFrom(@"D:\Projects\visual studio 2010\Console\ConsoleApplication32\ConsoleApplication32\bin\Debug\ConsoleApplication32.exe");
                Type hai = assembly.GetType("ConsoleApplication32.NextDerivedClass", true);
                Object o = System.Activator.CreateInstance(hai);
    
    
    
    //++++++++++++++++++++++++++++++++++++++++
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace ConsoleApplication32
    {
        class Program
        {
            public static void MethodABC() { }
            static void Main(string[] args)
            {
                var test = new NextDerivedClass();
            }
        }
    
        internal class BaseClass 
        {
            internal string BaseClassProperty { get; set; }
            
            internal string BaseClassProperty1 { get; set; }
            internal string BaseClassProperty2 { get; set; }
            internal string BaseClassProperty3 { get; set; }
        }
    
        internal class NextDerivedClass
        {
            internal string NextDerivedClassProperty { get; set; }
            internal string NextDerivedClassProperty1 { get; set; }
            internal string NextDerivedClassProperty2 { get; set; }
            internal string NextDerivedClassProperty3 { get; set; }
        }
    }
     

    The error you got is because of the parameter should be the full identity for that assembly. http://msdn.microsoft.com/en-us/library/ky3942xh.aspx , you will need to install the assembly to GAC first.

    So for easier test, I just used the Assembly.LoadForm method.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us




    Tuesday, February 28, 2012 1:16 PM
    Moderator
  • Hi,

    Thnak you for your help,

    1) As I explained above, I need to load an assemby of a Class Library (Not a WinForm application), which contains reports objects. It is the same idea?

    2) I see that you are loading an exe file from a physical location. I need to use that code on a web application, so what I should do in this case?

    Regards,

    Bader

    Tuesday, February 28, 2012 1:57 PM
  • All the assembly can use the same codes.

    The codes also can used into the web application. If it cannot work in your scenario, then please clarify your problem in detail, where's the assembly which you want load, and what your project like.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, February 29, 2012 11:59 AM
    Moderator