locked
Visual Studio 2015, C#, What has changed with AppDomain.CurrentDomain.GetAssemblies(). RRS feed

  • Question

  • After migrating my project from VS 2013 to VS 2015 - I am faced with a few object reference errors.

    I will identify the problem alongwith an example.

    I have two classes, with the same name StatusList - they are under different namespaces.

    a. TestNS.Interop.Cache.CacheItems

    b. TestNS.Interop.Enquiry

    namespace TestNS.Interop.Cache.CacheItems
    {
     public class StatusList
     {    
       public string Message { get; set; }    
       public StatusList()    
       {        Message = "I am a cache statuslist"; }
     }
    }

    namespace TestNS.Interop.Enquiry
    {
     public class StatusList
     {
        public string Message { get; set; }
        public StatusList()
        {
            Message = "I am an enquiry statuslist";
        }
     }   
    }   

    The main program makes a call to AppDomain.CurrentDomain.GetAssemblies() and looks for the first StatusList.

    var manyitems = AppDomain.CurrentDomain.GetAssemblies().SelectMany(o => o.GetTypes()); var typeServerCacheItem = manyitems.FirstOrDefault(o => o.Name == name);

    class Program
    {    
       static void Main(string[] args)    
       {            PrintMessagefromAssembly();    }    
       private static void PrintMessagefromAssembly() 
       {        
         const string name = "StatusList";        
         var manyitems = AppDomain.CurrentDomain.GetAssemblies().SelectMany(o => o.GetTypes());        
         var typeServerCacheItem = manyitems.FirstOrDefault(o => o.Name == name);        
        
         if (typeServerCacheItem == null)        
          {     Console.WriteLine("No item found");             
    return;        
          }        
    Console.WriteLine(typeServerCacheItem.FullName);        Console.ReadKey();    
         }
    }

    If you perform a clean and build for this project using VS2013 the typeServerCacheItem returned is the class under CacheItems.

    If you perform a clean and build using VS 2015 the typeServerCacheItem returned is the class under Enquiry.

    I do realize that the code should be fixed, there is a logical error in the code - a filter criteria should exist for CacheItems. However I am trying to understand what changed with the way AppDomain.CurrentDomain.GetAssemblies() works?

    Many Thanks Rajesh

    Thursday, April 28, 2016 11:30 AM

Answers

  • Hi Ragz R,

    I've tested on my side, yes, the same result as yours.

    In VS2013, if you change the namespace, VS2013 will get the first namespace from top to bottom.

    In VS2015, the Order is the opposite of VS2013.

    In above scenario, all project target is set to .Net framework 4.5.

    From the result, per my understanding, the main issue is the different MSbuild version in VS2013 and vs2015.  MSBuild is a build tool that helps automate the process of creating a software product, including compiling the source code, packaging, testing. I wonder if it is different in VS2015.

    Since we don't know how it implements internally, we cannot give a 100% right answer. Thanks for your understanding. 

    >>However I am trying to understand what changed with the way AppDomain.CurrentDomain.GetAssemblies() works?

    AppDomain.CurrentDomain.GetAssemblies(), I think these code are OK, because I build the same target .net framework version, and also tested in different platforms. All the results are same. So it is not a code issue.

    If you want to know more, For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:

    Best regards,

    Kristin


    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.



    • Edited by Kristin Xie Friday, April 29, 2016 6:18 AM
    • Proposed as answer by Kristin Xie Wednesday, May 4, 2016 8:21 AM
    • Marked as answer by DotNet Wang Friday, May 6, 2016 1:49 AM
    Friday, April 29, 2016 6:18 AM

All replies

  • Hi,

    first of all you should always be careful: If no order is guaranteed, then you should not rely on an order. You create code that might work or not work depending on things that you cannot predict.

    What did you change in the projects? Did you change anything on the projects e.g. the target framework? Or id everything unchanged and just the IDE was changed? (This should not change the behaviour because the compiler is part of the .Net framework and that wasn't changed.

    Or did you change anything else? Something that might have changed the order in which the assemblies was loaded?

    And what kind of project do have? (So for example Asp.Net behaves different - it preloads assemblies)

    With kind regards,

    Konrad

    Thursday, April 28, 2016 12:30 PM
  • Hi Ragz R,

    I've tested on my side, yes, the same result as yours.

    In VS2013, if you change the namespace, VS2013 will get the first namespace from top to bottom.

    In VS2015, the Order is the opposite of VS2013.

    In above scenario, all project target is set to .Net framework 4.5.

    From the result, per my understanding, the main issue is the different MSbuild version in VS2013 and vs2015.  MSBuild is a build tool that helps automate the process of creating a software product, including compiling the source code, packaging, testing. I wonder if it is different in VS2015.

    Since we don't know how it implements internally, we cannot give a 100% right answer. Thanks for your understanding. 

    >>However I am trying to understand what changed with the way AppDomain.CurrentDomain.GetAssemblies() works?

    AppDomain.CurrentDomain.GetAssemblies(), I think these code are OK, because I build the same target .net framework version, and also tested in different platforms. All the results are same. So it is not a code issue.

    If you want to know more, For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:

    Best regards,

    Kristin


    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.



    • Edited by Kristin Xie Friday, April 29, 2016 6:18 AM
    • Proposed as answer by Kristin Xie Wednesday, May 4, 2016 8:21 AM
    • Marked as answer by DotNet Wang Friday, May 6, 2016 1:49 AM
    Friday, April 29, 2016 6:18 AM