public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { object conversion = null; if (culture == null) { culture = CultureInfo.CurrentCulture; } BasicComponent component = value as BasicComponent; if (basicComponent != null) { if (destinationType == typeof(InstanceDescriptor)) { // Note that we convert the blobs to an array as this makes for nicer persisted code output. // Without it, we might just get a resource blob which is not human-readable. conversion = new InstanceDescriptor( typeof(BasicComponent).GetConstructor(new Type[] { typeof(IEnumerable<Blob>) }), new object[] { basicComponent.Blobs.ToArray() }, true); } } if (conversion == null) { conversion = base.ConvertTo(context, culture, value, destinationType); } return conversion; }
/// <summary> /// 关闭弹出功能 /// </summary> /// <param name="winName"></param> private void closeWindow(string winName) { BasisComponent _basFunc; ExtendFunction _extfunc; if (Enum.TryParse(winName, out _extfunc)) { //调用扩展功能 SessionExtend.CloseFunction(); } else if (Enum.TryParse(winName, out _basFunc)) { //调用基础功能扩展组件 BasicComponent.CloseFunction(); } else { if (tool != null) { if (tool.IsDisposed) { tool.Close(); } } } }
public void RemoveAllTest() { var x = new EventExchange(new BasicLogExchange()); var parent = new BasicComponent(); var child1 = new BasicComponent(); var child2 = new BasicComponent(); parent.AddHandler <BasicEvent>(_ => { }); child1.AddHandler <BasicEvent>(_ => { }); child2.AddHandler <BasicEvent>(_ => { }); parent.AddChild(child1); x.Attach(parent); parent.AddChild(child2); x.Raise(new BasicEvent(), this); Assert.Equal(1, parent.Handled); Assert.Equal(1, child1.Handled); Assert.Equal(1, child2.Handled); parent.RemoveAll(); x.Raise(new BasicEvent(), this); Assert.Equal(2, parent.Handled); Assert.Equal(1, child1.Handled); Assert.Equal(1, child2.Handled); var nonChild = new BasicComponent(); parent.RemoveChild(nonChild); }
public void RaiseTest() { var c1 = new BasicComponent(); c1.AddHandler <BasicEvent>(_ => { }); var c2 = new BasicComponent(); c2.AddHandler <BasicEvent>(_ => { }); var e = new BasicEvent(); var ee = new EventExchange(new BasicLogExchange()); ee.Attach(c1); ee.Attach(c2); Assert.Equal(0, c1.Handled); Assert.Equal(0, c2.Handled); c1.Raise(e); Assert.Equal(0, c1.Handled); // Components shouldn't see their own events. Assert.Equal(1, c2.Handled); var recipients = ee.EnumerateRecipients(typeof(BasicEvent)).ToList(); Assert.Collection(recipients, x => Assert.Equal(x, c1), x => Assert.Equal(x, c2)); }
public void DetachedChildTest() { var x = new EventExchange(new BasicLogExchange()); var parent = new BasicComponent(); var child = new BasicComponent(); parent.AddHandler <BasicEvent>(_ => { }); child.AddHandler <BasicEvent>(_ => { }); parent.AddChild(child); x.Attach(parent); child.Remove(); x.Raise(new BasicEvent(), this); Assert.Equal(1, parent.Handled); Assert.Equal(0, child.Handled); parent.IsActive = false; x.Raise(new BasicEvent(), this); Assert.Equal(1, parent.Handled); Assert.Equal(0, child.Handled); parent.IsActive = true; x.Raise(new BasicEvent(), this); Assert.Equal(2, parent.Handled); Assert.Equal(0, child.Handled); }
public void ChildTest() { var x = new EventExchange(new BasicLogExchange()); var parent = new BasicComponent(); var child1 = new BasicComponent(); var child2 = new BasicComponent(); parent.AddHandler <BasicEvent>(_ => { }); child1.AddHandler <BasicEvent>(_ => { }); child2.AddHandler <BasicEvent>(_ => { }); parent.AddChild(child1); x.Attach(parent); x.Raise(new BasicEvent(), this); Assert.Equal(1, parent.Handled); Assert.Equal(1, child1.Handled); Assert.Equal(0, child2.Handled); parent.RemoveChild(child1); parent.AddChild(child2); x.Raise(new BasicEvent(), this); Assert.Equal(2, parent.Handled); Assert.Equal(1, child1.Handled); Assert.Equal(1, child2.Handled); parent.RemoveAll(); x.Raise(new BasicEvent(), this); Assert.Equal(3, parent.Handled); Assert.Equal(1, child1.Handled); Assert.Equal(1, child2.Handled); parent.AddChild(child1); parent.AddChild(child2); child2.IsActive = false; x.Raise(new BasicEvent(), this); Assert.Equal(4, parent.Handled); Assert.Equal(2, child1.Handled); Assert.Equal(1, child2.Handled); parent.IsActive = false; x.Raise(new BasicEvent(), this); Assert.Equal(4, parent.Handled); Assert.Equal(2, child1.Handled); Assert.Equal(1, child2.Handled); parent.IsActive = true; x.Raise(new BasicEvent(), this); Assert.Equal(5, parent.Handled); Assert.Equal(3, child1.Handled); Assert.Equal(1, child2.Handled); parent.IsActive = true; child2.IsActive = true; x.Raise(new BasicEvent(), this); Assert.Equal(6, parent.Handled); Assert.Equal(4, child1.Handled); Assert.Equal(2, child2.Handled); }
public void DisableHandlerTest() { var c = new BasicComponent(); var e = new BasicEvent(); var x = new EventExchange(new BasicLogExchange()); c.AddHandler <BasicEvent>(_ => { }); c.Attach(x); Assert.Equal(0, c.Handled); c.Receive(e, this); Assert.Equal(1, c.Handled); c.AddHandler <BasicEvent>(_ => { }); c.Receive(e, this); Assert.Equal(2, c.Handled); c.RemoveHandler <BasicEvent>(); c.Receive(e, this); Assert.Equal(2, c.Handled); c.AddHandler <BasicEvent>(_ => { }); c.Receive(e, this); Assert.Equal(3, c.Handled); c.AddHandler <BasicEvent>(_ => throw new InvalidOperationException()); // Registering a handler for an event that's already handled should be a no-op c.Receive(e, this); Assert.Equal(4, c.Handled); }
public void BasicComponentTest() { var c = new BasicComponent(); c.AddHandler <BasicEvent>(_ => { }); var e = new BasicEvent(); var x = new EventExchange(new BasicLogExchange()); Assert.Equal(0, c.Handled); Assert.True(c.IsActive); c.Receive(e, this); Assert.Equal(0, c.Handled); // Handlers don't fire if component isn't attached c.Attach(x); c.Receive(e, this); Assert.Equal(1, c.Handled); c.Attach(x); c.Attach(x); c.Attach(x); c.Attach(x); c.Receive(e, this); Assert.Equal(2, c.Handled); c.Remove(); c.Receive(e, this); Assert.Equal(2, c.Handled); c.Remove(); c.Remove(); c.Receive(e, this); Assert.Equal(2, c.Handled); }
public void ResolveTest() { var c = new BasicComponent(); var x = new EventExchange(new BasicLogExchange()); x.Attach(c); Assert.Null(c.CallResolve <IBasicInterface>()); var imp = new BasicImplementation(); x.Register <IBasicInterface>(imp); Assert.Equal(imp, c.CallResolve <IBasicInterface>()); x.Unregister(imp); Assert.Null(c.CallResolve <IBasicInterface>()); }
public void EnqueuedEventTest() { var x = new EventExchange(new BasicLogExchange()); var c1 = new BasicComponent(); var c2 = new BasicComponent(); c1.AddHandler <BasicEvent>(_ => { }); c2.AddHandler <BasicEvent>(_ => { }); x.Attach(c1); x.Attach(c2); c1.Enqueue(new BasicEvent()); Assert.Equal(0, c1.Handled); Assert.Equal(0, c2.Handled); x.FlushQueuedEvents(); Assert.Equal(0, c1.Handled); Assert.Equal(1, c2.Handled); }
/// <summary> /// 打开扩展功能 /// </summary> /// <param name="winName">扩展功能控件名</param> /// <param name="winLabel">标签名称</param> /// <param name="paramList">参数</param> /// <returns></returns> private bool openWindow(string winName, string winLabel, string userID, params object[] paramlist) { bool rtn = false; BasisComponent _basFunc; ExtendFunction _extfunc; if (Enum.TryParse(winName, out _extfunc)) { //调用扩展功能 rtn = SessionExtend.ShowFunction(userID, _extfunc); } else if (Enum.TryParse(winName, out _basFunc)) { //调用基础功能扩展组件 rtn = BasicComponent.ShowFunction(userID, _basFunc); } else { try { switch (winName) { case "Setup": tool = new FormBase(userID, winName, "系统设置"); break; case "Tools": tool = new FormBase(userID, winName, "常用工具包"); break; } tool.Show(); rtn = true; } catch { rtn = false; } } return(rtn); }
public static void PerformChanges(IRepository pcmRepository, ISystem0 system, IResourceEnvironment resourceEnvironment, Allocation allocation, Model model, string target) { var last = false; var i = 0; ModelChangeRecorder recorder = null; StartNextRecorder(model, target, last, ref i, ref recorder); var container = new ResourceContainer { Id = Guid.NewGuid().ToString(), EntityName = "A random new container", }; resourceEnvironment.ResourceContainer_ResourceEnvironment.Add(container); StartNextRecorder(model, target, last, ref i, ref recorder); // next, we allocate an assembly context to this container var allocationCtx = allocation.AllocationContexts_Allocation[0]; var oldContainer = allocationCtx.ResourceContainer_AllocationContext; allocationCtx.ResourceContainer_AllocationContext = container; StartNextRecorder(model, target, last, ref i, ref recorder); // to repair the situation, we create a link between the old container and the new one var link = new LinkingResource { Id = Guid.NewGuid().ToString(), EntityName = "SnailConnection" }; link.ConnectedResourceContainers_LinkingResource.Add(oldContainer); link.ConnectedResourceContainers_LinkingResource.Add(container); resourceEnvironment.LinkingResources__ResourceEnvironment.Add(link); StartNextRecorder(model, target, last, ref i, ref recorder); // create a new dummy interface var dummyInterface = new OperationInterface { Id = Guid.NewGuid().ToString(), EntityName = "Foobar" }; pcmRepository.Interfaces__Repository.Add(dummyInterface); StartNextRecorder(model, target, last, ref i, ref recorder); // create a dummy component var dummyComponent = new BasicComponent { Id = Guid.NewGuid().ToString(), EntityName = "Dummy" }; var realAssembly = allocationCtx.AssemblyContext_AllocationContext; var realComponent = realAssembly.EncapsulatedComponent__AssemblyContext; var realProvided = realComponent.ProvidedRoles_InterfaceProvidingEntity[0] as IOperationProvidedRole; var realInterface = realProvided.ProvidedInterface__OperationProvidedRole; var requireReal = new OperationRequiredRole { Id = Guid.NewGuid().ToString(), EntityName = "Required", RequiredInterface__OperationRequiredRole = realInterface }; dummyComponent.RequiredRoles_InterfaceRequiringEntity.Add(requireReal); var requireFake = new OperationRequiredRole { Id = Guid.NewGuid().ToString(), EntityName = "Fake", RequiredInterface__OperationRequiredRole = dummyInterface }; dummyComponent.RequiredRoles_InterfaceRequiringEntity.Add(requireFake); pcmRepository.Components__Repository.Add(dummyComponent); StartNextRecorder(model, target, last, ref i, ref recorder); // create an assembly for the dummy var dummyAssembly = new AssemblyContext { Id = Guid.NewGuid().ToString(), EntityName = "DummyAssembly", EncapsulatedComponent__AssemblyContext = dummyComponent }; system.AssemblyContexts__ComposedStructure.Add(dummyAssembly); // create a connector from the dummy to a real assembly var connector = new AssemblyConnector { Id = Guid.NewGuid().ToString(), EntityName = "A connector", ProvidedRole_AssemblyConnector = realProvided, ProvidingAssemblyContext_AssemblyConnector = realAssembly, RequiredRole_AssemblyConnector = requireFake, RequiringAssemblyContext_AssemblyConnector = dummyAssembly }; system.Connectors__ComposedStructure.Add(connector); StartNextRecorder(model, target, last, ref i, ref recorder); // fix the connector connector.RequiredRole_AssemblyConnector = requireReal; last = true; StartNextRecorder(model, target, last, ref i, ref recorder); }
public void RaiseAsyncTest() { var c1 = new BasicComponent(); var c2 = new BasicComponent(); var c3 = new BasicComponent(); var pending2 = new Queue <Action>(); var pending3 = new Queue <Action <bool> >(); c1.AddHandler <BoolAsyncEvent>(_ => { }); c1.AddHandler <BasicAsyncEvent>(_ => { }); c2.AddAsyncHandler <BasicAsyncEvent>((_, c) => { pending2.Enqueue(c); return(true); }); c3.AddAsyncHandler <BoolAsyncEvent, bool>((_, c) => { pending3.Enqueue(c); return(true); }); void Check(int seen1, int seen2, int seen3, int handled1, int handled2, int handled3) { Assert.Equal(seen1, c1.Seen); Assert.Equal(seen2, c2.Seen); Assert.Equal(seen3, c3.Seen); Assert.Equal(handled1, c1.Handled); Assert.Equal(handled2, c2.Handled); Assert.Equal(handled3, c3.Handled); } var boolEvent = new BoolAsyncEvent(); var nullEvent = new BasicAsyncEvent(); var ee = new EventExchange(new BasicLogExchange()); ee.Attach(c1); ee.Attach(c2); ee.Attach(c3); Check(0, 0, 0, 0, 0, 0); ee.Raise(boolEvent, this); Check(1, 0, 1, 1, 0, 0); Assert.Empty(pending2); Assert.Collection(pending3, _ => { }); pending3.Dequeue()(true); Check(1, 0, 1, 1, 0, 1); var recipients = ee.EnumerateRecipients(typeof(BoolAsyncEvent)).ToList(); Assert.Collection(recipients, x => Assert.Equal(x, c1), x => Assert.Equal(x, c3)); recipients = ee.EnumerateRecipients(typeof(BasicAsyncEvent)).ToList(); Assert.Collection(recipients, x => Assert.Equal(x, c1), x => Assert.Equal(x, c2)); int total = 0; int totalTrue = 0; void BoolContinuation(bool x) { total++; if (x) { totalTrue++; } } void PlainContinuation() => total++; Assert.Equal(1, ee.RaiseAsync(boolEvent, this, BoolContinuation)); // 1 async handler, 1 sync Check(2, 0, 2, 2, 0, 1); pending3.Dequeue()(true); Check(2, 0, 2, 2, 0, 2); Assert.Equal(1, total); Assert.Equal(1, totalTrue); Assert.Equal(1, ee.RaiseAsync(boolEvent, this, BoolContinuation)); // 1 async handler, 1 sync Check(3, 0, 3, 3, 0, 2); pending3.Dequeue()(false); Assert.Equal(2, total); Assert.Equal(1, totalTrue); Check(3, 0, 3, 3, 0, 3); Assert.Equal(1, c1.RaiseAsync(nullEvent, PlainContinuation)); // 1 async handlers, 1 sync but c1 is raising so shouldn't receive it. Check(3, 1, 3, 3, 0, 3); Assert.Empty(pending3); pending2.Dequeue()(); Check(3, 1, 3, 3, 1, 3); Assert.Equal(3, total); }
public void HandlerFormatTest() { var c = new BasicComponent(); Assert.Equal("H<BasicComponent, BasicEvent>", new Handler <BasicEvent>(_ => { }, c).ToString()); }