jeudi 28 avril 2016

C#, What has changed with AppDomain.CurrentDomain.GetAssemblies().SelectMany()?

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





Aucun commentaire:

Enregistrer un commentaire