using System.Collections.Generic;
public static void Main()
var path = @"X:\some dir\another folder\filename.eml";
var emailModel = EmailModelFactory.CreateEmailModel(path);
Console.WriteLine(emailModel.SayHello());
Console.WriteLine(emailModel.SayExtension());
foreach(var a in emailModel.Attachments){
Console.WriteLine($"Attachment {i}: {a}");
Console.WriteLine("Done");
public static class EmailModelFactory
private const string EmlExt = ".eml";
private const string MsgExt = ".msg";
private static bool IgnoreCaseEquals(string a, string b) =>
a.Equals(b, StringComparison.InvariantCultureIgnoreCase);
private static bool IsEml(string ext) => IgnoreCaseEquals(ext, EmlExt);
private static bool IsMsg(string ext) => IgnoreCaseEquals(ext, MsgExt);
public static IEmailModel CreateEmailModel(string path)
var ext = Path.GetExtension(path);
_ when IsEml(ext) => new EmailEmlModel(path),
_ when IsMsg(ext) => new EmailMsgModel(path),
_ => throw new NotImplementedException($"IEmailModel not implemented for extension: {ext}")
public interface IEmailModel
IEnumerable<string> Attachments{get;}
public abstract class EmailModelBase : IEmailModel
private readonly string _ext;
public EmailModelBase(string path)
_ext = Path.GetExtension(path);
public string SayExtension(){
public abstract IEnumerable<string> Attachments {get;}
public class EmailEmlModel : EmailModelBase
private List<string> _attachments;
public EmailEmlModel(string path) : base(path)
_attachments = path.Split(@"\").ToList();
public override IEnumerable<string> Attachments => _attachments;
public class EmailMsgModel : EmailModelBase
private List<string> _attachments;
public EmailMsgModel(string path) : base(path)
_attachments = path.Split(@"\").ToList();
public override IEnumerable<string> Attachments => _attachments;