Example #1
0
        public List <FileFormatInfo> GetFormatInfos(Type baseFormatType)
        {
            // TODO: very naive, only picks up formats in same assembly as base format type, file extension extraction from regex is iffy, etc...

            List <FileFormatInfo> infos = new List <FileFormatInfo>();

            foreach (Type type in Assembly.GetAssembly(baseFormatType).GetExportedTypes().Where(x => x != baseFormatType && x.BaseType == baseFormatType))
            {
                FileFormat instance = (FileFormat)Activator.CreateInstance(type);

                string description = (instance.GetFormatDescription() ?? string.Format("{0} Format", type.Name)), extension = ".*";

                var fnPatternAttrib = type.GetCustomAttributes(typeof(FilenamePatternAttribute), false).FirstOrDefault();
                if (fnPatternAttrib != null)
                {
                    string pattern = (fnPatternAttrib as FilenamePatternAttribute).Pattern;
                    extension = Path.GetExtension(pattern).Replace("$", "");
                }

                infos.Add(new FileFormatInfo(description, extension, type));
            }

            return(infos);
        }