public async Task <object> ResolveCustomersServiceAsync(Type serviceType) { var type = Type.GetType($"WasmReload.Shared.Interfaces.{serviceType.Name}, WasmReload.Shared"); var props = type.GetMethods(); var expando = new ExpandoObject() as IDictionary <string, object>; foreach (var prop in props) { var parameters = prop.GetParameters(); if (parameters.Length == 0) { expando.Add(prop.Name, Return <Task <dynamic> > .Arguments(async() => await InvokeRemoteMethodAsync(prop.Name, 0))); } if (parameters.Length == 1) { //expando.Add(prop.Name, Return<Task<Customer>>.Arguments<dynamic>(async (p1) => await InvokeRemoteMethodAsync(prop.Name, 1, p1))); expando.Add(prop.Name, Return <Task <dynamic> > .Arguments <dynamic>(async(p1) => await InvokeRemoteMethodAsync(prop.Name, 1, p1))); } if (parameters.Length == 2) { expando.Add(prop.Name, Return <Task <dynamic> > .Arguments <dynamic, dynamic>(async(p1, p2) => await InvokeRemoteMethodAsync(prop.Name, 2, p1, p2))); } if (parameters.Length == 3) { expando.Add(prop.Name, Return <Task <dynamic> > .Arguments <dynamic, dynamic, dynamic>(async(p1, p2, p3) => await InvokeRemoteMethodAsync(prop.Name, 3, p1, p2, p3))); } } dynamic myInterface = Impromptu.DynamicActLike(expando, type); InstanceId = await client.GetStringAsync($"Services?Service={serviceType.Name.TrimStart('I')}&InstanceId={Guid.NewGuid()}"); return(myInterface); }
static void Main(string[] args) { Bar b = new Bar(); b.DoSomethingWithFoo(new { Foobar = Return <string> .Arguments <string>(r => "foo") }.ActLike <IFoo>()); }
public void TestDynamicMethodCurry() { var tNewObj = Build.NewObject(Add: Return <int> .Arguments <int, int>((x, y) => x + y)); var tCurry = Impromptu.Curry(tNewObj).Add(4); var tResult = tCurry(10); Assert.AreEqual(14, tResult); //Test cached invocation; var tResult2 = tCurry(30); Assert.AreEqual(34, tResult2); }
public static IRepository <User> GetRepository() { var repo = new { Query = Return <IQueryable <User> > .Arguments(() => new[] { new User() { Name = "Jack Sparrow" } }.AsQueryable()) }.ActLike <IRepository <User> >(); return(repo); }
public void can_get_an_expando_object_to_act_as_an_interface() { dynamic expando = Build <ExpandoObject> .NewObject( Prop1 : "Test", Prop2 : 42L, Prop3 : Guid.NewGuid(), Meth1 : Return <bool> .Arguments <int>(it => it > 5) ); IMyInterface myInterface = Impromptu.ActLike(expando); Assert.Equal("Test", myInterface.Prop1); Assert.Equal(true, myInterface.Meth1(10)); }
public void NormalUsage() { var anon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid(), Meth1 = Return <bool> .Arguments <int>(it => it > 5) }; var myInterface = anon.ActLike <IMyInterface>(); Assert.Equal(myInterface.Prop1, anon.Prop1); }
public void TestAnonInterface() { var tInterface = Impromptu.Create <Get, ICollection>(new { CopyArray = ReturnVoid.Arguments <Array, int>((ar, i) => Enumerable.Range(1, 10)), Count = 10, IsSynchronized = false, SyncRoot = this, GetEnumerator = Return <IEnumerator> .Arguments(() => Enumerable.Range(1, 10).GetEnumerator()) }); Assert.AreEqual(10, tInterface.Count); Assert.AreEqual(false, tInterface.IsSynchronized); Assert.AreEqual(this, tInterface.SyncRoot); Assert.AreEqual(true, tInterface.GetEnumerator().MoveNext()); }
public void can_get_an_anonymous_class_to_act_as_an_interface() { //Anonymous Class var anon = new { Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid(), Meth1 = Return <bool> .Arguments <int>(it => it > 5) }; var myInterface = anon.ActLike <IMyInterface>(); Assert.Equal("Test", myInterface.Prop1); Assert.Equal(true, myInterface.Meth1(10)); }
public void DynamicAnnonymousWrapper() { var tData = new Dictionary <int, string> { { 1, "test" } }; dynamic tDyn = new Get(new { Test1 = 1, Test2 = "2", IsGreaterThan5 = Return <bool> .Arguments <int>(it => it > 5), ClearData = ReturnVoid.Arguments(() => tData.Clear()) }); Assert.AreEqual(1, tDyn.Test1); Assert.AreEqual("2", tDyn.Test2); Assert.AreEqual(true, tDyn.IsGreaterThan5(6)); Assert.AreEqual(false, tDyn.IsGreaterThan5(4)); Assert.AreEqual(1, tData.Count); tDyn.ClearData(); Assert.AreEqual(0, tData.Count); }
public void TestAnonInterface() { dynamic tInterface = new DynamicObjects.Get(new { CopyArray = ReturnVoid.Arguments <Array, int>( (ar, i) => Enumerable.Range(1, 10)), Count = 10, IsSynchronized = false, SyncRoot = this, GetEnumerator = Return <IEnumerator> .Arguments( () => Enumerable.Range(1, 10).GetEnumerator()) }); Dynamic.ApplyEquivalentType(tInterface, typeof(ICollection), typeof(IEnumerable)); Assert.AreEqual(10, tInterface.Count); Assert.AreEqual(false, tInterface.IsSynchronized); Assert.AreEqual(this, tInterface.SyncRoot); Assert.That((object)tInterface.GetEnumerator(), Is.InstanceOf <IEnumerator>()); }
public void TestBuilderWithClay() { var New = Builder.New <Clay>() .ObjectSetup( Return <object[]> .Arguments( () => new object[] { new IClayBehavior[] { new InterfaceProxyBehavior(), new PropBehavior(), new ArrayPropAssignmentBehavior(), new NilResultBehavior() } })) .ArraySetup( Return <object[]> .Arguments( () => new object[] { new IClayBehavior[] { new InterfaceProxyBehavior(), new PropBehavior(), new ArrayPropAssignmentBehavior(), new ArrayBehavior(), new NilResultBehavior() } })); var directory = New.Array( New.Person().Name("Louis").Aliases(new[] { "Lou" }), New.Person().Name("Bertrand").Aliases("bleroy", "boudin") ).Name("Orchard folks"); Assert.AreEqual(2, directory.Count); Assert.AreEqual("Orchard folks", directory.Name); Assert.AreEqual("Louis", directory[0].Name); Assert.AreEqual(1, directory[0].Aliases.Count); Assert.AreEqual("Lou", directory[0].Aliases[0]); Assert.AreEqual("Bertrand", directory[1].Name); Assert.AreEqual(2, directory[1].Aliases.Count); Assert.AreEqual("bleroy", directory[1].Aliases[0]); Assert.AreEqual("boudin", directory[1].Aliases[1]); }
private static void Main(string[] args) { var wfst = new WorkflowSimpleTest(); wfst.Run(); //return; var tpp = new TypeProjectionPlayground(); tpp.Stuff(); Catalog.Services.Register(typeof(IStructuredDataStorage <>), typeof(StructuredDataStorage <>)); Catalog.Services.Register <IStructuredDataServer>(_ => new StructuredDataServer()); Catalog.Services.Register("CacheClient", _ => { return(Catalog.Preconfigure().Add( StructuredDataClientLocalConfig.Inbox, new MemoryMappedTransferInbox (Guid.NewGuid().ToString())) .Add( StructuredDataClientLocalConfig.Server, new MemoryMappedTransferOutbox("memserver")) .ConfiguredCreate <IStructuredDataClient>( () => new StructuredDataClient())); }); Catalog.Services.Register <IMessageBusSpecifier>(_ => new IpcMessageBus()); Catalog.Services.Register <IMessageListener>(_ => new IpcMessageListener()); Catalog.Services.Register <IMessagePublisher>(_ => new IpcMessagePublisher()); var sds = Catalog.Preconfigure() .Add(StructuredDataStorageLocalConfig.Store, new MemoryPersistentStore <string>()) .Add(StructuredDataStorageLocalConfig.Cloner, Return <string> .Arguments((string skey) => string.Copy(skey))) .Add(StructuredDataStorageLocalConfig.Comparer, StringComparer.CurrentCulture) .ConfiguredResolve <IStructuredDataStorage <string> >(); var sdServer = Catalog.Preconfigure() .Add(StructuredDataServerLocalConfig.Store, sds) .Add(StructuredDataServerLocalConfig.Inbox, new MemoryMappedTransferInbox("memserver")) .Add(StructuredDataServerLocalConfig.OutboxFactory, Return <IMessageOutbox> .Arguments <string>(name => new MemoryMappedTransferOutbox(name))) .ConfiguredResolve <IStructuredDataServer>(); var sdsClient = Catalog.Factory.Resolve <IStructuredDataClient>("CacheClient"); var cts = new CancellationTokenSource(); var declType = typeof(TableDecl); var runServer = Task.Factory.StartNew(() => sdServer.Run(declType, cts.Token)); var captains = sdsClient.OpenTable <Captain>(TableDecl.Captains); var ships = sdsClient.OpenTable <Ship>(TableDecl.Ships); using (sdsClient.BeginTransaction()) { captains.AddNew("kirk", new Captain { Name = "Kirk", Braveness = 5 }); captains.AddNew("picard", new Captain { Name = "Picard", Braveness = 6 }); sdsClient.Commit(); } var data = captains.Fetch("kirk", "picard"); var kirk = data.Single(d => d.Key == "kirk"); kirk.Data.Braveness = 6; using (sdsClient.BeginTransaction()) { captains.Update("kirk", kirk.Data, kirk.ETag); sdsClient.Commit(); } var qServerDataStorage = Catalog.Preconfigure() .Add(StructuredDataStorageLocalConfig.Store, new MemoryPersistentStore <string>()) .Add(StructuredDataStorageLocalConfig.Cloner, Return <string> .Arguments((string skey) => string.Copy(skey))) .Add(StructuredDataStorageLocalConfig.Comparer, StringComparer.CurrentCulture) .ConfiguredResolve <IStructuredDataStorage <string> >(); var qServer = Catalog.Preconfigure() .Add(StructuredDataServerLocalConfig.Store, qServerDataStorage) .Add(StructuredDataServerLocalConfig.Inbox, new MemoryMappedTransferInbox("qserver")) .Add(StructuredDataServerLocalConfig.OutboxFactory, Return <IMessageOutbox> .Arguments <string>(name => new MemoryMappedTransferOutbox(name))) .ConfiguredResolve <IStructuredDataServer>(); var runQServer = Task.Factory.StartNew(() => qServer.Run(typeof(AMQPDeclarations), cts.Token)); var specifier = Catalog.Preconfigure() .Add(MessageBusSpecifierLocalConfig.HostConnectionString, "qserver") .ConfiguredResolve <IMessageBusSpecifier>(); specifier.DeclareExchange("msgExchange", ExchangeTypes.Direct) .SpecifyExchange("msgExchange").DeclareQueue("testq", "testroute"); var listener = Catalog.Preconfigure() .Add(MessageListenerLocalConfig.HostConnectionString, "qserver") .Add(MessageListenerLocalConfig.ExchangeName, "msgExchange") .Add(MessageListenerLocalConfig.QueueName, "testq") .ConfiguredResolve <IMessageListener>(); listener.Listen(new KeyValuePair <Type, Action <object, CancellationToken, IMessageAcknowledge> > (typeof(Captain), ShowCaptain)); var sender = Catalog.Preconfigure() .Add(MessagePublisherLocalConfig.HostConnectionString, "qserver") .Add(MessagePublisherLocalConfig.ExchangeName, "msgExchange") .ConfiguredResolve <IMessagePublisher>(); sender.Send(kirk.Data, "testroute"); Console.ReadLine(); cts.Cancel(); }
public Func <dynamic> CreateFactoryWithInterfaces(params Type[] interfaces) { return(Return <dynamic> .Arguments(CreateInstanceWithInterfaces(interfaces))); }
private static T Create <T>(string path, int level, IEnumerable <string> tags, Dictionary <Type, Type> deserializationmappings, string defaultlevelqueryparametername, string defaultexcludtagsqueryparametername, int defaulttimeoutinseconds, bool filter = true) where T : class { dynamic dexp = new ExpandoObject(); IDictionary <string, object> exp = (IDictionary <string, object>)dexp; dexp.DYN_defaultlevelqueryparametername = defaultlevelqueryparametername; dexp.DYN_defaultexcludtagsqueryparametername = defaultexcludtagsqueryparametername; dexp.DYN_level = level; dexp.DYN_tags = tags; dexp.DYN_deserializationmappings = deserializationmappings; dexp.DYN_defaulttimeoutinseconds = defaulttimeoutinseconds; if (filter && deserializationmappings != null) { foreach (Type t in deserializationmappings.Keys) { if (!t.IsAssignableFrom(deserializationmappings[t])) { throw new ArgumentException("The mapping type '" + deserializationmappings[t].Name + "' is not child of '" + t.Name + "'"); } } } bool hasfilterinterface = (typeof(T).GetInterfaces().Any(a => a.Name == typeof(Interfaces.IFilter <>).Name)); List <Type> ifaces = new List <Type>() { typeof(T) }; ifaces.AddRange(typeof(T).GetInterfaces()); foreach (MethodInfo m in ifaces.SelectMany(a => a.GetMethods())) { List <Annotations.Atributes.Rest> rests = m.GetCustomAttributes <Annotations.Atributes.Rest>().ToList(); if (rests.Count > 0) { MethodDefinition defs = new MethodDefinition(); defs.RestAttribute = rests[0]; defs.BasePath = path; defs.Parameters = m.GetParameters().Select(a => new Tuple <string, Type>(a.Name, a.ParameterType)).ToList(); defs.ReturnType = m.ReturnType; if (hasfilterinterface && (m.Name == "FilterWithLevel" || m.Name == "FilterWithTags" || m.Name == "FilterWithLevelAndTags")) { continue; } if (m.IsAsyncMethod()) { switch (defs.Parameters.Count) { case 0: exp[m.Name] = Return <dynamic> .Arguments(() => DoAsyncClient(dexp, defs)); break; case 1: exp[m.Name] = Return <dynamic> .Arguments <dynamic>((a) => DoAsyncClient(dexp, defs, a)); break; case 2: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic>((a, b) => DoAsyncClient(dexp, defs, a, b)); break; case 3: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic>((a, b, c) => DoAsyncClient(dexp, defs, a, b, c)); break; case 4: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic>((a, b, c, d) => DoAsyncClient(dexp, defs, a, b, c, d)); break; case 5: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e) => DoAsyncClient(dexp, defs, a, b, c, d, e)); break; case 6: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f) => DoAsyncClient(dexp, defs, a, b, c, d, e, f)); break; case 7: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f, g) => DoAsyncClient(dexp, defs, a, b, c, d, e, f, g)); break; case 8: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f, g, h) => DoAsyncClient(dexp, defs, a, b, c, d, e, f, g, h)); break; default: throw new NotImplementedException("It only support till 8 parameters feel free to add more here :O"); } } else { switch (defs.Parameters.Count) { case 0: exp[m.Name] = Return <dynamic> .Arguments(() => DoSyncClient(dexp, defs)); break; case 1: exp[m.Name] = Return <dynamic> .Arguments <dynamic>((a) => DoSyncClient(dexp, defs, a)); break; case 2: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic>((a, b) => DoSyncClient(dexp, defs, a, b)); break; case 3: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic>((a, b, c) => DoSyncClient(dexp, defs, a, b, c)); break; case 4: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic>((a, b, c, d) => DoSyncClient(dexp, defs, a, b, c, d)); break; case 5: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e) => DoSyncClient(dexp, defs, a, b, c, d, e)); break; case 6: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f) => DoSyncClient(dexp, defs, a, b, c, d, e, f)); break; case 7: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f, g) => DoSyncClient(dexp, defs, a, b, c, d, e, f, g)); break; case 8: exp[m.Name] = Return <dynamic> .Arguments <dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>((a, b, c, d, e, f, g, h) => DoSyncClient(dexp, defs, a, b, c, d, e, f, g, h)); break; default: throw new NotImplementedException("It only support till 8 parameters feel free to add more here :O"); } } } } T inter = Impromptu.ActLike <T>(dexp); if (hasfilterinterface) { if (filter) { exp["FilterWithLevel"] = Return <T> .Arguments <int>((a) => Create <T>(path, a, null, deserializationmappings, defaultlevelqueryparametername, defaultexcludtagsqueryparametername, defaulttimeoutinseconds, false)); exp["FilterWithTags"] = Return <T> .Arguments <IEnumerable <string> >((a) => Create <T>(path, int.MaxValue, a, deserializationmappings, defaultlevelqueryparametername, defaultexcludtagsqueryparametername, defaulttimeoutinseconds, false)); exp["FilterWithLevelAndTags"] = Return <T> .Arguments <int, IEnumerable <string> >((a, b) => Create <T>(path, a, b, deserializationmappings, defaultlevelqueryparametername, defaultexcludtagsqueryparametername, defaulttimeoutinseconds, false)); } else { exp["FilterWithLevel"] = Return <T> .Arguments <int>((a) => inter); exp["FilterWithTags"] = Return <T> .Arguments <IEnumerable <string> >((a) => inter); exp["FilterWithLevelAndTags"] = Return <T> .Arguments <int, IEnumerable <string> >((a, b) => inter); } } return(inter); }