예제 #1
0
        public Type GetImplementation <T>() where T : IItemWrapper
        {
            var typeOfT = typeof(T);

            if (!typeOfT.IsInterface)
            {
                throw new Exception("Fortis: An interface implementing IITemWrapper must be passed as the generic argument to get the corresponding implementation. " + typeOfT.Name + " is not an interface.");
            }

            if (!InterfaceTemplateMap.ContainsKey(typeOfT))
            {
                throw new Exception("Fortis: Type " + typeOfT.Name + " does not exist in interface template map");
            }

            var templateId = InterfaceTemplateMap[typeOfT];

            if (!TemplateMap.ContainsKey(templateId))
            {
                throw new Exception("Fortis: Template ID " + templateId + " does not exist in template map");
            }

            foreach (var type in TemplateMap[templateId])
            {
                if (typeOfT.IsAssignableFrom(type))
                {
                    return(type);
                }
            }

            return(null);
        }
예제 #2
0
        public bool IsCompatibleTemplate(Guid templateId, Type template)
        {
            // template Type must at least implement IItemWrapper
            if (!template.ImplementsInterface(typeof(IItemWrapper)) || !TemplateMap.ContainsKey(templateId))
            {
                return(false);
            }

            if (template.IsInterface)
            {
                return(TemplateMap[templateId].Any(type => type.ImplementsInterface(template)));
            }
            else
            {
                // Get interface of template to check
                var templateType  = TemplateMap.FirstOrDefault(p => p.Value.Contains(template)).Key;
                var interfaceType = InterfaceTemplateMap.FirstOrDefault(i => i.Value == templateType).Key;

                // Get interface of item to chack
                var interfaceToCheck = InterfaceTemplateMap.FirstOrDefault(i => i.Value == templateId).Key;

                return(interfaceToCheck == interfaceType || interfaceToCheck.ImplementsInterface(interfaceType));
            }
        }