约定扩展的示例
self.port.on("myCustomEvent", function(message) {
self.port.emit("backEvent", "newMessage");
});
我得到的每种类型都应该用这种方法提供复杂的启动
kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.WithAttribute<SomeAttribute>()
.BindBase();
});
答案 0 :(得分:0)
创建public static void main(String[] args) throws Exception {
CompletableFuture<String> future = new CompletableFuture<String>();
CompletableFuture<?> done = future.whenCompleteAsync((r, t) -> {
System.out.println("before sleep, executed in thread " + Thread.currentThread());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after sleep, executed in thread " + Thread.currentThread());
});
System.out.println(Thread.currentThread());
future.complete("completed");
System.out.println("done");
done.get();
}
:
Thread[main,5,main]
done
before sleep, executed in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]
after sleep, executed in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]
并按如下方式使用:
BiConsumer
答案 1 :(得分:0)
在搜索了一些例子后,我写了这个解决方案。我做 不知道它是最好的解决方案,但它对我有用。如果强> 有人会改进我的代码或为我的问题编写最佳实践 将不胜感激: - )
UnitTest我想要的地方获取继承自IStepContext的类型, 哪个有MarkAttribute
[TestClass]
public class Test
{
private readonly IKernel _kernel = new StandardKernel();
[TestInitialize]
public void Startup()
{
_kernel.Bind(a =>
{
a.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IStepContext>()
.WithAttribute<MarkAttribute>(x => x.Type == Inheritance.Derived).BindWith<SettingsBindGenerator>();
});
}
[TestMethod]
public void BaseClass()
{
BaseClass1 derived1 = _kernel.Get<BaseClass1>();
Type res = derived1.WhoIAm(); //-- - > "BaseClass1"
Assert.AreEqual(res, typeof (BaseClass1));
}
[TestMethod]
public void DerivedClass()
{
IStepContextA derived = _kernel.Get<IStepContextA>();
Type res = derived.WhoIAm(); //-- - > "DerivedClass"
Assert.AreEqual(res, typeof (DerivedClass));
}
[TestMethod]
public void DerivedClassA1()
{
IStepContext derived2 = _kernel.Get<BaseClassA>();
Type res = derived2.WhoIAm(); //-- - > "DerivedClass"
Assert.AreEqual(res, typeof (DerivedClassA1));
}
}
自定义BindGenerator
public class SettingsBindGenerator : IBindingGenerator
{
public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
{
Func<Type, IBindingWhenInNamedWithOrOnSyntax<object>> func =
t => bindingRoot.Bind(t).ToMethod(ctx => GetInstance(ctx, type));
var bindings = new List<IBindingWhenInNamedWithOrOnSyntax<object>>();
// if type inherited from interface
Type[] interfs = type.GetInterfaces();
if (interfs.Length > 1)
{
// skip base interface (IStepContext)
interfs = interfs.Take(interfs.Length - 1).ToArray();
bindings = interfs.Select(x => func(x)).ToList();
}
// if type inherited from baseType
Type baseType = type.BaseType;
if (baseType != null)
{
bindings.Add(func(baseType));
if (ShouldBeBound(baseType))
{
var ancestor = baseType.BaseType;
while (ancestor != null && ShouldBeBound(ancestor))
{
bindings.Add(func(ancestor));
ancestor = ancestor.BaseType;
}
}
}
return bindings;
}
private static bool ShouldBeBound(Type type)
{
return type.IsClass && type != typeof (object);
}
private object GetInstance(IContext ctx, Type type)
{
MethodInfo method = typeof(PageService).GetMethod("Create");
IPage page = method.MakeGenericMethod(type).Invoke(null, new object[] { null });
return page;
}
}
帮助程序类
public class BaseClass : AbstractStepContext
{
public override Type WhoIAm()
{
return GetType();
}
}
public class AbstractStepContext : IStepContext
{
public virtual Type WhoIAm()
{
return GetType();
}
}
public class BaseClass1 : IBase
{
public virtual Type WhoIAm()
{
return GetType();
}
}
[Mark(Inheritance.Base)]
public class BaseClass1Derivied : BaseClass1
{
public override Type WhoIAm()
{
return GetType();
}
}
public class BaseClassA : AbstractStepContext
{
public virtual Type WhoIAm()
{
return GetType();
}
}
[Mark(Inheritance.Derived)]
public class DerivedClass : IStepContextA
{
public Type WhoIAm()
{
return GetType();
}
}
[Mark(Inheritance.Derived)]
public class DerivedClassA1 : BaseClassA
{
public override Type WhoIAm()
{
return GetType();
}
}
public interface IStepContext
{
Type WhoIAm();
}
public interface IStepContextA : IStepContext
{
}
public class MarkAttribute : Attribute
{
public Inheritance Type ;
public MarkAttribute(Inheritance type)
{
Type = type;
}
}