public static CodeMemberEvent AddEvent(this CodeTypeDeclaration classCode, Type delegateType, MemberAttributes ma, string name) { var ev = Define.Event(delegateType, CorrectAttributes(classCode, ma), name); classCode.Members_Add(ev); return(ev); }
public void TestCustomEvent() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass(Define.Class("ee", TypeAttributes.Public, true) .AddMember(Define.Event(new CodeTypeReference(typeof(EventHandler)), MemberAttributes.Family, "Event1", Define.GetProperty("", default(MemberAttributes), "", Emit.stmt(() => Console.WriteLine("add")) ), Define.GetProperty("", default(MemberAttributes), "", Emit.stmt(() => Console.WriteLine("remove")) ), Define.Method(default(MemberAttributes), (object sender, EventArgs args) => "", Emit.stmt(() => Console.WriteLine("raise")) ) )) ); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB)); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp)); var ass = c.Compile(null, CodeDomGenerator.Language.VB); Assert.IsNotNull(ass); Type eeClass = ass.GetType("Samples.ee"); Assert.IsNotNull(eeClass); ass = c.Compile(null, CodeDomGenerator.Language.CSharp); Assert.IsNotNull(ass); eeClass = ass.GetType("Samples.ee"); Assert.IsNotNull(eeClass); }
public void Builder_Event() { var c = new CodeDomGenerator(); c.AddNamespace("Samples").AddClass(Define.Class("TestClass") .AddDelegate(typeof(int), MemberAttributes.Public, (string s) => "xxxDelegate") .AddEvents( Define.Event("xxxDelegate", MemberAttributes.Public, "Event1") ) ); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp)); Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB)); var ass = c.Compile(); Assert.IsNotNull(ass); Type TestClass = ass.GetType("Samples.TestClass"); Assert.IsNotNull(TestClass); }
public static void Definitions() { // Events var Start = Define.Event("Start"); var IncrementBy = Define.EventWithData <int>("IncrementBy"); // Actions var SideEffect1 = Define.Action.SideEffect(() => Console.WriteLine("I'm a Side Effect")); var SideEffect2 = Define.Action.SideEffectWithContext <FetchContext>(context => Console.WriteLine($"I have access to the context {context.Retries}")); var SideEffect3 = Define.Action.SideEffectWithContextAndData <FetchContext, int>((context, amount) => Console.WriteLine($"I have access to the context {context.Retries} and some data {amount}")); var Assign1 = Define.Action.Assign <FetchContext>(context => context.Retries = 0); var Assign2 = Define.Action.AssignWithData <FetchContext, int>((context, amount) => context.Retries += amount); // Services var TaskService = Define.Service.Task(token => Task.Delay(TimeSpan.FromSeconds(3), token)); var ActivityService = Define.Service.Activity(() => Console.WriteLine("started"), () => Console.WriteLine("stopped")); // Root Statenode var CompoundRoot = Define.Statechart .WithInitialContext(new NoContext()) .WithRootState( "example" .AsCompound() .WithInitialState("first") .WithStates("first", "second")); var OrthogonalRoot = Define.Statechart .WithInitialContext(new NoContext()) .WithRootState( "example" .AsOrthogonal() .WithStates("a", "b")); // Transitions var TransitionsExample = "example".WithTransitions( On("EventName").TransitionTo.Self, On(Start).TransitionTo.Self, On(IncrementBy).TransitionTo.Self, On("dummy").If <FetchContext>(context => context.Retries > 10).TransitionTo.Self, On(IncrementBy).If <FetchContext>((_, amount) => amount > 5).TransitionTo.Self, Ignore("EventName"), Ignore(Start), Ignore(IncrementBy), Immediately.TransitionTo.Self, After(3.Seconds()).TransitionTo.Self, On("dummy").TransitionTo.Self, On("dummy").TransitionTo.Child("child", "even", "deep", "children"), On("dummy").TransitionTo.Sibling("sibling", "even", "children", "of", "siblings"), On("dummy").TransitionTo.Target(Sibling("sibling")), On("dummy").TransitionTo.Target(Child("child")), On("dummy").TransitionTo.Absolute("rootstatenode", "children", "deeper", "..."), On("dummy").TransitionTo.Multiple(Child("paralle", "child1"), Child("parallel", "child2")), On("dummy").TransitionTo.Self.WithActions(SideEffect1, Log("and another one"))); // Actions var ActionsExample = "example".WithEntryActions <FetchContext>( Run(() => Console.WriteLine("some arbitrary action")), Run <FetchContext>(context => Console.WriteLine($"some arbitrary action with {context}")), Log("logging a label"), Log <FetchContext>(context => $"logging some context {context}"), Assign <FetchContext>(context => context.Retries = 0)); // Statenodes OnDone var OnCompoundDoneExample = "example" .AsCompound() .WithInitialState("first") .WithStates("first".AsFinal()) .OnDone.TransitionTo.Sibling("sibling"); var OnOrthogonalDoneExample = "example" .AsOrthogonal() .WithStates("first".AsFinal(), "second".AsFinal()) .OnDone.TransitionTo.Sibling("sibling"); // Services OnSuccess (OnError is currently missing :/) var TaskServiceExample = "example" .WithInvocations(TaskService.OnSuccess.TransitionTo.Sibling("sibling")); }