none
System.Refelection.Assembly.CreateInstance broken in .NET 4.0? RRS feed

  • Question

  • We upgraded from .NET 3.5 to .NET 4.0 and now System.Refelection.Assembly.CreateInstance does not appear to be working. Has anyone else had this problem? Is there a way to fix it? Below is an example of how we are loading the asembly.

    Assembly assembly = Assembly.LoadWithPartialName("AssemblyName");
          object instance = assembly.CreateInstance("Namespace.Class",
                            false,
                            BindingFlags.CreateInstance,
                            null,
                            null, null, null);
    
    
    Friday, January 14, 2011 9:25 PM

Answers

  • Apparently the most obvious problem cannot be seen all the time. My classes were removed from the project file during the .NET 4.0 upgrade. Sorry for the time you spent on this.
    Friday, January 21, 2011 8:36 PM

All replies

  • It would be helpful if you supply some more information:

    - what does it mean exactly "not working"? you get a null return value? exception?

    - what object are you trying to create? is this a com object? is this a .net object registered as com? which CLR created this object? etc..

    Due to side by side execution feature of .NET 4 there are obviously some changes to how assemblies are loaded in .NET 4.

     


    Arik Poznanski
    blogs.microsoft.co.il/blogs/arik

    Friday, January 14, 2011 11:42 PM
  • It returns a null value. No exception. It's a .NET Assembly registered in the GAC. It is not a COM object.

    Friday, January 14, 2011 11:44 PM
  •  

    The assembly  variable is null? or the instance variable is null?

     

    You code snippet works well on my side (VS2010 + .NET4.0), the instance is not null. By the way, the LoadWithPartialName is obsoleted, you may use Load method against a full assembly name as an alternative.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 17, 2011 6:33 AM
  • The instance is null the assembly is created just fine. It's very strange!
    Monday, January 17, 2011 9:36 AM
  • You code snippet works well on my side (win7 32bit + VS2010 + .NET 4.0), could you please try your code in a newly created C# console application, will the issue reproduce?

    Any step/hint that can help to reproduce this issue is appreciated.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 17, 2011 1:15 PM
  • I narrowed down the cause of the problem. My class A that I am trying to create inherits from class B. Class B is defined as public abstract class B. Class B contains most of the logic with one abstract method that class A defines. Similarly I have another class C that inherits from Class B that has a different definition for the method. Basically refactoring to share common logic. This worked in .NET 3.5 but in .NET 4.0 I finally narrowed down the exception to be "{"Cannot create an abstract class."}".

     

    public abstract class A
    {
      public string InvokeUI() 
     {
       //some logic
       DisplayUI();
     }
     
     protected abstract void DisplayUI();
    }
    
    public class B : A
    {
      protected override DisplayUI()
      {
        Some logic;
      }
    
    }
    
    

     

     

    Tuesday, January 18, 2011 9:59 PM
  • Which type (A or B?) is passed into the assembly.CreateInstance() method?

    If I use

                Assembly assembly = Assembly.LoadWithPartialName("ClassLibrary1");

     

                object instance = assembly.CreateInstance("ClassLibrary1.A",

                                  false,

                                  BindingFlags.CreateInstance,

                                  null,

                                  null, null, null);

    there will be an exception saying “Constructor on type 'ClassLibrary1.A' not found.”, both .NET 3.5 and .NET 4.0 have same reuslt.

    If I use

                Assembly assembly = Assembly.LoadWithPartialName("ClassLibrary1");

     

                object instance = assembly.CreateInstance("ClassLibrary1.B",

                                  false,

                                  BindingFlags.CreateInstance,

                                  null,

                                  null, null, null);

    the instance variable is not null.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 19, 2011 2:41 AM
  • The second scenario is exactly what I am seeing but in my case the instance variable is null. Seems like i'm still missing a piece of the puzzle.
    Wednesday, January 19, 2011 2:58 AM
  • Could you create a new project and try it again? Here is the definition of Class A and Class B:

    namespace ClassLibrary1

    {

        public abstract class A

        {

            public string InvokeUI()

            {

                DisplayUI();

                return "";

            }

     

            protected abstract void DisplayUI();

        }

     

        public class B : A

        {

            protected override void DisplayUI()

            {

                //Some logic;

            }

        }

    }


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 19, 2011 4:04 AM
  • Apparently the most obvious problem cannot be seen all the time. My classes were removed from the project file during the .NET 4.0 upgrade. Sorry for the time you spent on this.
    Friday, January 21, 2011 8:36 PM