Tôi vừa mới bắt đầu chơi xung quanh với khung công tác Mở rộng được quản lý. Tôi có một lớp được xuất và một câu lệnh nhập:MEF với ImportMany và ExportMetadata
[Export(typeof(IMapViewModel))]
[ExportMetadata("ID",1)]
public class MapViewModel : ViewModelBase, IMapViewModel
{
}
[ImportMany(typeof(IMapViewModel))]
private IEnumerable<IMapViewModel> maps;
private void InitMapView()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ZoneDetailsViewModel).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
foreach (IMapViewModel item in maps)
{
MapView = (MapViewModel)item;
}
}
Điều này chỉ hoạt động tốt. IEnumerable có được các lớp được xuất khẩu. Không, tôi cố gắng để thay đổi điều này để sử dụng danh sách Lazy và bao gồm các siêu dữ liệu để tôi có thể lọc ra các lớp học mà tôi cần (giống xuất khẩu như trước đây)
[ImportMany(typeof(IMapViewModel))]
private IEnumerable<Lazy<IMapViewModel,IMapMetaData>> maps;
private void InitMapView()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ZoneDetailsViewModel).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
foreach (Lazy<IMapViewModel,IMapMetaData> item in maps)
{
MapView = (MapViewModel)item.Value;
}
}
Sau này IEnumerable không có yếu tố này. Tôi nghi ngờ rằng tôi đã tạo ra một sai lầm rõ ràng và ngu ngốc ở một nơi nào đó ..
Giao diện siêu dữ liệu của bạn trông như thế nào? –
Bạn không biết bạn có thể làm một ImportMany bao gồm siêu dữ liệu. tốt đẹp! – juFo