jeudi 24 septembre 2020

Assemblie.load.getType doesn't work for types with?

I have a huge like 110000+ line codefile that I'm reading out for some specific types to get information about them. I get those types from a .json file.

The reading out part is done via reflection (the assemblie is in a other solution):

First I load the files into the memory:

public static List<Assembly> LoadAssemblies(List<string> loadedPaths)
    {
        foreach (string loadPath in loadedPaths)
        {
            List<AssemblyName> referencedAssemblyNames = Assembly.LoadFrom(loadPath)
                                     .GetReferencedAssemblies()
                                     .Where(a => a.FullName.StartsWith(Constants.NameSpace))
                                     .ToList();
        }
        return AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith(Constants.NameSpace)).ToList();
    }

After that I read out the specific Types and its customattributes that I want information about:

  public static Dictionary<string, string> GetCustomAttributes(List<Type> streamDesign, List<Type> enums)
    {
        List<PropertyInfo> propertyInfos = new List<PropertyInfo>();
        streamDesign.ForEach(sd => propertyInfos.AddRange(sd.GetProperties().ToList()));

        Dictionary<string, string> dbUsageMapping = new Dictionary<string, string>();

        foreach (Type t in enums)
        {

            var property = propertyInfos
                .Where(p => p.PropertyType.FullName != null ? p.PropertyType.FullName.Equals(t.ToString(),
                                        StringComparison.CurrentCultureIgnoreCase) : false)
                .ToList();

            foreach (PropertyInfo p in property)
            {
                var tmp = p.GetCustomAttributes()
                                .Where(a => a is ColumnAttribute)
                                .FirstOrDefault() as ColumnAttribute;
                if (tmp != null)
                {
                    dbUsageMapping.Add(t.ToString(), tmp.Name);
                    break;
                }
            }

        }
        return dbUsageMapping;
    }

The types I get with this one:

  public static List<Type> GetStreamDesignTypes(string loadedPath)
        {
            return AppDomain.CurrentDomain.GetAssemblies().Where(a => a.Location.Equals(loadedPath)).FirstOrDefault().GetTypes().ToList();
        }

It works fine for types without "?", but types like this -->

public global::JobTemplateType? TemplateType {}

aren't in there.

Do I have to do something specific to get those types? And if I search for types with "?", it returns null;

Any ideas?

Edit: As requested, original types -->

public enum JobTemplateType
    {
        /// <summary>
        /// Constant to represent None.
        /// </summary>
        None = 0,

        /// <summary>
        /// Constant to represent a normal job.
        /// </summary>
        Normal = 1,
...
    }

The original types are Enums and I want to match the Enums with the CustomAttributes "name" (if exists) to get the occurences in the database. (for documentation purposes)





Aucun commentaire:

Enregistrer un commentaire