我正试图获得石油换粮食部,以便与它所知道的在我更新出口情况时的所有部分进行对比。 基本上,我想让教育、青年和体育部更新我的所有部分,这些部分在改变时进口了连接面的配置价值。 一切都看好我想要改变这种局面。 如果我尝试对更新价值的缔约方进行认证,似乎在集装箱中增加第二例,然后更新我的进口,但取消。
谁能指出我究竟在哪里错了? 或者,我是否甚至试图以这种方式使用紧急部队?
我正在使用MEF预告9,并以因特网框架3.5和WPF为对象。
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using Shouldly;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
MainClass main = new MainClass();
main.RunTest();
}
}
public class MainClass
{
[ImportMany]
public IEnumerable<Settings> Settings { get; set; }
public void RunTest()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Settings).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
container.SatisfyImportsOnce(this);
Config cfg = new Config
{
Settings = new Settings { ConnectionString = "Value1" },
};
// result is returned with a null settings value
UsesSettings result = container.GetExportedValue<UsesSettings>();
// this recomposes everything with the new value, result changes to have settings of Value1
container.ComposeParts(cfg);
// this line results in my import many enumerable returning 2 parts the Value1 setting and null
container.SatisfyImportsOnce(this);
result.TheSettings.ConnectionString.ShouldBe("Value1");
cfg.Settings = new Settings { ConnectionString = "Value2" };
// how do I tell the container to recompose now I have changed the config object,
// or how do I replace the part value with the new value?
// this line causes the result.Settings to return null
container.ComposeParts(cfg);
// this updates the ImportMany to 3 values, Value1, Value2 and null
container.SatisfyImportsOnce(this);
}
}
public class Settings
{
public string ConnectionString = "default value";
}
public class Config
{
[Export(typeof(Settings))]
public Settings Settings { get; set; }
}
[Export(typeof(UsesSettings))]
public class UsesSettings
{
[Import(typeof(Settings), AllowRecomposition = true)]
public Settings TheSettings { get; set; }
}
}