저는 MEF에 완전히 익숙하며 현재 개발중인 프로젝트에서 사용하고 싶습니다.MEF ImportMany 유형 매개 변수 : 우아한 해결책?
아이디어 : T
을 형식으로 입력하려고 시도하는 FileLoader<T>
을 작성하고 싶습니다.이 형식으로로드 할 수있는 파일 확장명이 지원되는 데이터를로드하고 제공합니다. 형식 로더는 솔루션에 균등하게 분산 된 클래스로 Func<string, T>
으로 제공되며 MEF가 IEnumerable
으로 수집해야합니다. 지원되는 파일 형식은 메타 데이터에서 수집됩니다.
은 이제 내가 그것을하려고했던 방법은 다음과 같습니다
[Export(typeof(IFileLoader<>))]
class FileLoader<TData> : IFileLoader<TData>
{
static FileLoader()
{
SimpleIoc.Default.Register(() => { return new FileLoader<Model.CompositeSignal<Model.SignalBase>>(); });
}
[ImportMany(typeof(Func<string, TData>))] // Problem here!!!
private IEnumerable<Func<string, TData>> Loaders { get; set; }
public FileLoader()
{
//TODO: Compose here
}
public IEnumerable<FileExtensionFilter> GetSupportedFiletypes()
{
throw new NotImplementedException();
}
public TData LoadFromFile(string filename)
{
throw new NotImplementedException();
}
}
//////////////// Somewhere else in a class //////////////////////////////////////
[Export(typeof(Func<string, CompositeSignal<SignalBase>>))]
[SupportedFileType(FileTypeDescription="Signal interpolation file", FileTypeMask="*.interpol.xml")]
public CompositeSignal<InterpolatedSignal> InterpolationFileLoader(string filename)
{
throw new NotImplementedException(); //TODO
}
////////////////////////////////////////////////////////////////////////////////
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Method, AllowMultiple=true)]
public class SupportedFileTypeAttribute : Attribute
{
public string FileTypeDescription { get; set; }
public string FileTypeMask { get; set; }
public SupportedFileTypeAttribute()
: base()
{ }
}
문제는 내가 ImportMany 속성에 대해서 typeof에서 일반 매개 변수를 가질 수있다. 이제는 물론 모든 유형의 로더를 가져 와서 반복 해 볼 수도 있지만 그게 우아한 솔루션이라고 상상할 수는 없습니다.
가장 좋은 방법은 무엇일까요?
미리 감사드립니다.
'ImportMany'에서 타입을 지정하지 않고 타입 자체를 매치 시키면 어떻게 될까요? 나는 이런 상황에서 결코 그것을 시도한 적이 없지만 그것이 효과가 있는지 보는 데 관심이 있습니다. 나는 아래의 Adi의 답변도 좋아합니다. – burnttoast11