Tôi cố gắng dành thời gian để hoàn thành công việc bằng MEF nhưng bây giờ, tôi gặp phải vấn đề cần được giúp đỡ.Nhập tài sản luôn rỗng (vấn đề nhập MEF)
Mô tả: Tôi có 2 tệp DLL và một tệp EXE. ClassLibrary1 (LoggerImpl.cs, SomeClass.cs) ClassLibrary2 (ILogger.cs) WindowsApplicationForms1 (WindowsApplicaitonForms1.cs, Program.cs)
Tôi cần bất kỳ sự giúp đỡ hoặc hướng tại sao điều này không hoạt động?
// ClassLibrary1.dll
//SomeClass.cs
public class SomeClass
{
[Import("Logging", typeof(ILogger))]
public ILogger Log { get; set; } <-- ALWAYS NULL ???
public void Print()
{
Log.Print();
}
}
// ClassLibrary1.dll
// LoggerImpl.cs
namespace ClassLibrary1
{
[Export("Logging", typeof (ILogger))]
public class LoggerImpl : ILogger
{
public void Print()
{
Console.WriteLine("print called");
}
}
}
// ClassLibrary2.dll
// ILogger.cs
namespace LogNamespace
{
public interface ILogger
{
void Print();
}
}
// WindowsFormsApplication1.exe
// WindowsFormsApplication1.cs
namespace WindowsFormsApplication1
{
[Export("Form1",typeof(Form1))]
public partial class Form1 : Form
{
[Import("Logging", typeof(ILogger))]
public ILogger Log { set; get; }
private CompositionContainer _container;
public Form1()
{
InitializeComponent();
Compose();
Log.Print();
SomeClass c = new SomeClass();
c.Print();
}
private void Compose()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("."));
catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
_container = new CompositionContainer(catalog);
try
{
_container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
MessageBox.Show(compositionException.ToString());
}
}
}
}
'SomeClass' của bạn không tham gia vào sáng tác, vì vậy sẽ không bao giờ được nhập. – dtb