本文整理汇总了C#中Binder.Bind方法的典型用法代码示例。如果您正苦于以下问题:C# Binder.Bind方法的具体用法?C# Binder.Bind怎么用?C# Binder.Bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Binder
的用法示例。
在下文中一共展示了Binder.Bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestDuplicateUnbinding
public void TestDuplicateUnbinding()
{
Binder binder = new Binder();
var equivalent = new EventEquivalent();
List<Handler> handlerChain = new List<Handler>();
binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
binder.Bind(new SampleEvent1 { Foo = 2 }, new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1));
binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);
// with EventSink
var sink = new SampleEventSink();
sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
sink.Unbind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);
}
示例2: TestContainsByType
public void TestContainsByType()
{
var binder = new Binder();
binder.Bind<MockClassToDepend>().ToSelf();
binder.Bind<IMockInterface>().To<MockIClassWithAttributes>();
var contains = binder.ContainsBindingFor(typeof(IMockInterface));
Assert.AreEqual(true, contains);
}
示例3: TestGetAll
public void TestGetAll()
{
var binder = new Binder();
binder.Bind(typeof(IMockInterface)).To<MockIClassWithAttributes>();
binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("test");
var bindings = binder.GetBindings();
Assert.AreEqual(2, bindings.Count);
}
示例4: TestContainsByIdentifier
public void TestContainsByIdentifier()
{
var binder = new Binder();
binder.Bind<IMockInterface>().To<MockIClass>();
binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("Identifier");
var contains = binder.ContainsBindingFor("Identifier");
Assert.AreEqual(true, contains);
}
示例5: TestBinder
public void TestBinder()
{
var model = new ModelTestClass
{
Number = 1,
Text = "First"
};
var control = new ControlTestClass
{
Text = "Wrong",
Number = -1
};
var textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
int[] controlCounts = { 0 };
textProp.PropertyChanged += (sender, args) => controlCounts[0]++;
int[] modelCounts = { 0 };
model.PropertyChanged += (sender, args) => modelCounts[0]++;
var binder = new Binder<ModelTestClass, ControlTestClass, string, string>(m => m.Text, () => DataConverter<string>.EmptyConverter);
var bInfo = binder.Bind(model, textProp, BindingMode.OneTime);
TestOneTimeBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
bInfo.Unbind();
model.Text = "First";
modelCounts[0] = 0;
control.Text = "Wrong";
controlCounts[0] = 0;
textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
bInfo = binder.Bind(model, textProp, BindingMode.OneWay);
TestOneWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
bInfo.Unbind();
model.Text = "Wrong";
control.Text = "First";
modelCounts[0] = 0;
controlCounts[0] = 0;
textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
bInfo = binder.Bind(model, textProp, BindingMode.OneWayToSource);
TestOneWayToSourceBindingInfo(bInfo, model, control, modelCounts, controlCounts);
bInfo.Unbind();
model.Text = "First";
modelCounts[0] = 0;
control.Text = "Wrong";
controlCounts[0] = 0;
textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action());
bInfo = binder.Bind(model, textProp, BindingMode.TwoWay);
TestTwoWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]);
bInfo.Unbind();
}
示例6: TestGetBindingsForGenerics
public void TestGetBindingsForGenerics()
{
var binder = new Binder();
binder.Bind<MockClassToDepend>().ToSelf();
binder.Bind(typeof(IMockInterface)).To<MockIClassWithAttributes>();
binder.Bind<IMockInterface>().To<MockIClassWithAttributes>().As("test");
var bindings = binder.GetBindingsFor<IMockInterface>();
Assert.AreEqual(2, bindings.Count);
Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
Assert.IsNull(bindings[0].identifier);
Assert.AreEqual(typeof(IMockInterface), bindings[1].type);
Assert.AreEqual("test", bindings[1].identifier);
}
示例7: TestBindNotAssignableInstanceTypeToInstance
public void TestBindNotAssignableInstanceTypeToInstance()
{
var binder = new Binder();
var instance = new MockClassToDepend();
binder.Bind<MockClassToDepend>().To(typeof(MockClassVerySimple), instance);
}
示例8: TestBindNotAssignableKeyTypeToInstance
public void TestBindNotAssignableKeyTypeToInstance()
{
var binder = new Binder();
var instance = new MockClassToDepend();
binder.Bind<IMockInterface>().To<MockClassToDepend>(instance);
}
示例9: TestBinding
public void TestBinding()
{
Binder binder = new Binder();
var equivalent = new EventEquivalent();
binder.Bind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));
var e1 = new SampleEvent1();
var e2 = new SampleEvent1 { Foo = 1 };
var e3 = new SampleEvent1 { Foo = 1, Bar = "bar" };
List<Handler> handlerChain = new List<Handler>();
handlerChain.Clear();
Assert.AreEqual(1, binder.BuildHandlerChain(e1, equivalent, handlerChain));
Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
handlerChain.Clear();
Assert.AreEqual(1, binder.BuildHandlerChain(e2, equivalent, handlerChain));
Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
handlerChain.Clear();
Assert.AreEqual(1, binder.BuildHandlerChain(e3, equivalent, handlerChain));
Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
binder.Unbind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(e1, equivalent, handlerChain));
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(e2, equivalent, handlerChain));
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(e3, equivalent, handlerChain));
}
示例10: TestAfterInject
public void TestAfterInject()
{
var eventCalled = false;
IReflectionCache cache = new ReflectionCache();
IBinder binder = new Binder();
IInjector injector = new Injector(cache, binder);
var instanceToInject = new MockClassVerySimple();
injector.afterInject += delegate(IInjector source, ref object instance, ReflectedClass reflectedClass) {
//The if below is just to avoid checking when injecting on MockIClass.
if (reflectedClass.type != typeof(MockClassVerySimple)) return;
Assert.AreEqual(injector, source);
Assert.AreEqual(instanceToInject, instance);
Assert.AreEqual(typeof(MockIClass), instanceToInject.field.GetType());
eventCalled = true;
};
binder.Bind<IMockInterface>().To<MockIClass>();
injector.Inject(instanceToInject);
Assert.IsTrue(eventCalled);
}
示例11: TestAfterResolve
public void TestAfterResolve()
{
var eventCalled = false;
IReflectionCache cache = new ReflectionCache();
IBinder binder = new Binder();
IInjector injector = new Injector(cache, binder);
IMockInterface resolvedInstance = null;
injector.afterResolve += delegate(IInjector source,
Type type,
InjectionMember member,
object parentInstance,
object identifier,
ref object resolutionInstance) {
Assert.AreEqual(injector, source);
Assert.AreEqual(typeof(IMockInterface), type);
Assert.AreEqual(InjectionMember.None, member);
Assert.IsNull(parentInstance);
Assert.IsNull(identifier);
Assert.IsNotNull(resolutionInstance);
resolvedInstance = (IMockInterface)resolutionInstance;
eventCalled = true;
return false;
};
binder.Bind<IMockInterface>().To<MockIClass>();
var instance = injector.Resolve<IMockInterface>();
Assert.IsTrue(eventCalled);
Assert.AreEqual(typeof(MockIClass), instance.GetType());
Assert.AreEqual(resolvedInstance, instance);
}
示例12: TestBindingToMultipleSingletonByInstance
public void TestBindingToMultipleSingletonByInstance()
{
var binder = new Binder();
binder.Bind<MockIClassWithAttributes>().To(new MockIClassWithAttributes());
binder.Bind<IMockInterface>().ToSingleton<MockIClassWithAttributes>();
var bindings1 = binder.GetBindingsFor<MockIClassWithAttributes>();
var bindings2 = binder.GetBindingsFor<IMockInterface>();
Assert.AreEqual(1, bindings1.Count);
Assert.AreEqual(1, bindings2.Count);
Assert.AreEqual(BindingInstance.Singleton, bindings1[0].instanceType);
Assert.AreEqual(BindingInstance.Singleton, bindings2[0].instanceType);
Assert.AreEqual(typeof(MockIClassWithAttributes), bindings1[0].type);
Assert.AreEqual(typeof(IMockInterface), bindings2[0].type);
Assert.AreEqual(bindings1[0].value, bindings1[0].value);
}
示例13: TestBindingToFactoryTypeFromInterface
public void TestBindingToFactoryTypeFromInterface()
{
var binder = new Binder();
var type = typeof(MockFactory);
binder.Bind<IMockInterface>().ToFactory(type);
var bindings = binder.GetBindingsFor<IMockInterface>();
Assert.AreEqual(1, bindings.Count);
Assert.AreEqual(BindingInstance.Factory, bindings[0].instanceType);
Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
Assert.AreEqual(type, bindings[0].value);
}
示例14: TestBindingToInstanceFromInterface
public void TestBindingToInstanceFromInterface()
{
var binder = new Binder();
var instance = new MockIClassWithAttributes();
binder.Bind<IMockInterface>().To<MockIClassWithAttributes>(instance);
var bindings = binder.GetBindingsFor<IMockInterface>();
Assert.AreEqual(1, bindings.Count);
Assert.AreEqual(BindingInstance.Singleton, bindings[0].instanceType);
Assert.AreEqual(typeof(IMockInterface), bindings[0].type);
Assert.AreEqual(instance, bindings[0].value);
}
示例15: TestDuplicateBinding
public void TestDuplicateBinding()
{
Binder binder = new Binder();
List<Handler> handlerChain = new List<Handler>();
binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, handlerChain));
// with EventSink
var sink = new SampleEventSink();
sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
sink.Unbind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
handlerChain.Clear();
Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, handlerChain));
}