public T GetHandler <T>(string filename) { List <FileHandlerInfo> fileHandlers = (List <FileHandlerInfo>)ConfigurationManager.GetSection("fileHandlers"); string ext = Path.GetExtension(filename).Substring(1); FileHandlerInfo resultHandler = null; foreach (FileHandlerInfo handlerInfo in fileHandlers) { bool supportsThisType = false; foreach (string tmp in handlerInfo.SupportedExtensions) { if (0 == string.Compare(tmp, ext, true)) { supportsThisType = true; break; } } if (true == supportsThisType) { resultHandler = handlerInfo; break; } } if (null == resultHandler) { resultHandler = fileHandlers[0]; } Type handlerType = null; if (true == HasInterface(resultHandler.ReaderType, typeof(T))) { handlerType = resultHandler.ReaderType; } if (true == HasInterface(resultHandler.WriterType, typeof(T))) { handlerType = resultHandler.WriterType; } if (null == handlerType) { throw new ApplicationException("Failed to get type from file handler"); } T handler = (T)Activator.CreateInstance(handlerType); return(handler); }
public object Create(object parent, object configContext, System.Xml.XmlNode section) { List <FileHandlerInfo> result = new List <FileHandlerInfo>(); foreach (XmlNode node in section.ChildNodes) { try { FileHandlerInfo handlerInfo = new FileHandlerInfo(node); result.Add(handlerInfo); } catch { } } return(result); }