none
LoadFrom causes InvalidCastException or MissingMethodException? RRS feed

  • Question

  • There is article on MSDN "Best Practices for assembly loading". Link: http://msdn.microsoft.com/en-us/library/dd153782.aspx

    I confused by one sentence in "Load-From Context" paragraph, it says: "If an assembly is loaded with LoadFrom, and the probing path includes an assembly with the same identity but in a different location, an InvalidCastException, MissingMethodException, or  other unexpected behavior can occur".

    Here is my questions:

    1. If there this an assembly with the same identity locates under probing path, then this assembly will be loaded into "Default load context". Event you given an different path (Not included in probing path) to LoadFrom. But we did load the correct identity assembly into ApplicationDomain(Default Load context). We didn't load same assembly with same identity into multiple contexts or load assembly with different versions into same context. Then how this invalidCastException or MissingMethodException could happen?

    2. I don't think it's possible to load the same assembly with same identity into multiple contexts through LoadFrom method. Is it possible?

    Monday, December 22, 2014 2:50 AM

Answers

  • Hello BlueNight,

    >>Then how this invalidCastException or MissingMethodException could happen?

    One possible is that when we are trying to call a method of the assembly in the load-from context, while we type the method name which belongs to the assembled in the default context, and their return types are not same, so the invalidCastException would also occurs. For example, we write two assemblies but with different methods and return type:

    This would causes MissingMethodException:

    namespace DllA
    
    {
    
        public class Class1
    
        {
    
            public int HelloWorld1()
    
            {
    
                return 1;
    
            }
    
        }
    
    }
    
    namespace DllA
    
    {
    
        public class Class1
    
        {
    
            public int HelloWorld2()
    
            {
    
                return 1;
    
            }
    
        }
    
    }
    

    This would cause invalidCastException:

    namespace DllA
    
    {
    
        public class Class1
    
        {
    
            public int HelloWorld1()
    
            {
    
                return 1;
    
            }
    
        }
    
    }
    
    namespace DllA
    
    {
    
        public class Class1
    
        {
    
            public string HelloWorld1()
    
            {
    
                return “2”;
    
            }
    
        }
    
    }
    

    >> I don't think it's possible to load the same assembly with same identity into multiple contexts through LoadFrom method. Is it possible?

    As it says, if an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified, so it is not possible.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 23, 2014 8:02 AM
    Moderator