mercredi 26 décembre 2018

Compare TypeOf "List

I am building a code refactoring tool in which I get two variable types using tokens/nodes from the Roslyn API.

I need to compare and validate whether the two types are the same. Some other questions like this which work in case you have objects, however I need to work with strings and compare types in this case.Heres my method which works with typeName = "int", however when typeName="List<int>" I get null

 public static Type GetType(string typeName)
    {
        string clrType = null;

        if (CLRConstants.TryGetValue(typeName, out clrType))
        {
            return Type.GetType(clrType);
        }


        var type = Type.GetType(typeName);

        if (type != null) return type;
        foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
        {
            type = a.GetType(typeName);
            if (type != null)
                return type;
        }
        return null;
    }
    private static Dictionary<string, string> CLRConstants { get{
            var dct =  new Dictionary<string, string>();
            dct.Add("int", "System.Int32");
            return dct;

        } }





Aucun commentaire:

Enregistrer un commentaire