mardi 7 novembre 2017

Xamarin Reflection broke

I've got an issue when I want to load all classes inside a namespace. Since I updated to MonoFramework > 4.8 my code does not work anymore, but in earlier versions is does.

public class ClassReflection : IClassReflection
{
    public Type[] GetTypes(string ns)
    {
        Assembly asm = Assembly.GetExecutingAssembly();

        List<string> namespacelist = new List<string>();
        List<string> classlist = new List<string>();

        foreach (Type type in asm.GetTypes())
        {
            if (type.Namespace == ns)
                namespacelist.Add(type.Name);
        }

        return asm.GetTypes();
    }

    public Type[] GetTypes(AssemblyName name, string ns)
    {
        Assembly asm = Assembly.Load(name);

        List<string> namespacelist = new List<string>();
        List<string> classlist = new List<string>();

        var tps = asm.GetTypes();

        var types = asm.GetTypes().Where(t => t != null).Where(t => t.Namespace.StartsWith(ns));

        if (types == null)
        {
            Debug.WriteLine("Types is null");
            return null;
        }

        var arr = types.ToArray();
        return arr;
        //return types.Where(i => i != null).ToArray();
    }

    public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
    {
        return potentialDescendant.IsSubclassOf(potentialBase)
               || potentialDescendant == potentialBase;
    }
}

It throws an NullReferenceException

System.NullReferenceException: Object reference not set to an instance of an object

at the line

var arr = types.ToArray();

Does anyone know if this is a Bug, or does the code needs to be customized a little?

Thanks for your help!





Aucun commentaire:

Enregistrer un commentaire