private static T ReadMember <T>(this IDictionary <string, object> cache, string key, Type defaultType, Func <Assembly> assemblyLoader) where T : MemberInfo { // first check if this an abbreviated version of member object cacheValue = cache.ReadValue <object>(key); IDictionary <string, object> cacheValueAsDictionary = cacheValue as IDictionary <string, object>; if (cacheValueAsDictionary != null) { return(ReadMemberCore <T>(cacheValueAsDictionary, defaultType)); } else if ((cacheValue != null) && (defaultType != null)) { int metadataToken = (int)cacheValue; return((T)ReflectionResolver.ResolveMember(defaultType.Assembly.ManifestModule, metadataToken)); } else if ((cacheValue != null) && (assemblyLoader != null)) { int metadataToken = (int)cacheValue; return((T)ReflectionResolver.ResolveMember(assemblyLoader.Invoke().ManifestModule, metadataToken)); } else { return(null); } }
/// <inheritdoc /> protected sealed override object Resolve(object source) { if (source is IProcess process) { var product = (process.Recipe as IProductRecipe)?.Product; return(product); } if (source is IActivity activity) { var product = (activity.Process.Recipe as IProductRecipe)?.Product; return(product); } if (source is ProductInstance instance) { return(instance.Type); } // If our shortcuts do not work, use ReflectionResolver instead // Due the protection level of Resolve in the base class and the implementation // of IBindingResolver.Resolve which calls the whole chain the call order is important here. // First the value of the replacement is called and then the replacement is placed into the chain. var replacement = new ReflectionResolver(_fallbackProperty); var resolvedValue = ((IBindingResolverChain)replacement).Resolve(source); this.Replace(replacement); return(resolvedValue); }
private static Module ReadModuleCore(this IDictionary <string, object> cache, Assembly assembly) { Assumes.NotNull(cache, assembly); int metadataToken = cache.ReadValue <int>(AttributedCacheServices.CacheKeys.MetadataToken); return(ReflectionResolver.ResolveModule(assembly, metadataToken)); }
public static Resolver UseReflection(this Resolver resolver, Action <ReflectionResolver> configure) { return(resolver.Use(r => { var rr = new ReflectionResolver(); configure(rr); return rr; })); }
private static T ReadMemberCore <T>(IDictionary <string, object> cache, Type defaultType) where T : MemberInfo { Assumes.NotNull(cache); int metadataToken = cache.ReadValue <int>(AttributedCacheServices.CacheKeys.MetadataToken); Assembly assembly = cache.ReadAssembly(defaultType == null ? null : defaultType.Assembly); Module module = cache.ReadModule(assembly); return((T)ReflectionResolver.ResolveMember(module, metadataToken)); }
private void Start() { using (var registry = new CachedSchemaRegistryClient(registryConfig)) { var typeResolver = new ReflectionResolver(resolveReferenceTypesAsNullable: true); var schemaBuilder = new SchemaBuilder(typeResolver: typeResolver); using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registry, schemaBuilder: schemaBuilder)) { var builder = new ProducerBuilder <TKey, TValue>(producerConfig); Task t = builder.SetAvroValueSerializer(serializerBuilder, this.topic, registerAutomatically: AutomaticRegistrationBehavior.Always); while (!t.IsCompletedSuccessfully) { ; } using (var producer = builder.Build()) { while (true) { if (KVPS.Count == 0) { Thread.Sleep(1); } else { lock (KVPS) { foreach (KeyValuePair <TKey, TValue> KVP in KVPS) { try { Task p = producer.ProduceAsync(topic, new Message <TKey, TValue> { Key = KVP.Key, Value = KVP.Value }); while (!t.IsCompleted) { Console.WriteLine(p.Status); } Console.WriteLine(p.Status); } catch (Exception e) { System.Console.WriteLine(e); } } KVPS.Clear(); } } } } } } }
private static Assembly ReadAssemblyCore(this IDictionary <string, object> cache) { Assumes.NotNull(cache); // NOTE : we load assembly with the full name AND codebase // This will force the loader to look for the default probing path - local directories, GAC, etc - and only after that fallback onto the path // if we want the reverse behavior, we will need to try loading from codebase first, and of that fails load from the defualt context string assemblyName = cache.ReadValue <string>(AttributedCacheServices.CacheKeys.AssemblyFullName); string codeBase = cache.ReadValue <string>(AttributedCacheServices.CacheKeys.AssemblyLocation); return(ReflectionResolver.ResolveAssembly(assemblyName, codeBase)); }
public void NullByUnknownProperty() { // Arrange var obj = new SomeClass(); IBindingResolver reflectionResolver = new ReflectionResolver("SomeUnknownProperty"); // Act var result = reflectionResolver.Resolve(obj); // Assert Assert.IsNull(result); }
public void SelectedTypes() { var schema = new RecordSchema(nameof(EventContainer), new[] { new RecordField("event", new UnionSchema(new[] { new RecordSchema(nameof(OrderCreatedEvent), new[] { new RecordField("timestamp", new StringSchema()), new RecordField("total", new BytesSchema() { LogicalType = new DecimalLogicalType(5, 2) }) }), new RecordSchema(nameof(OrderCancelledEvent), new[] { new RecordField("timestamp", new StringSchema()) }) })) }); var codec = new BinaryCodec(); var resolver = new ReflectionResolver(); var deserializer = new BinaryDeserializerBuilder(BinaryDeserializerBuilder.CreateBinaryDeserializerCaseBuilders(codec) .Prepend(builder => new OrderDeserializerBuilderCase(resolver, codec, builder))) .BuildDeserializer <EventContainer>(schema); var serializer = new BinarySerializerBuilder(BinarySerializerBuilder.CreateBinarySerializerCaseBuilders(codec) .Prepend(builder => new OrderSerializerBuilderCase(resolver, codec, builder))) .BuildSerializer <EventContainer>(schema); var creation = new EventContainer { Event = new OrderCreatedEvent { Timestamp = DateTime.UtcNow, Total = 40M } }; var cancellation = new EventContainer { Event = new OrderCancelledEvent { Timestamp = DateTime.UtcNow } }; Assert.IsType <OrderCreatedEvent>(deserializer.Deserialize(serializer.Serialize(creation)).Event); Assert.IsType <OrderCancelledEvent>(deserializer.Deserialize(serializer.Serialize(cancellation)).Event); }
public override List <ResultsGroup> Run() { List <ResultsGroup> results = new List <ResultsGroup>(); foreach (SnapshotConfig snapshotConfig in _vmConfig.Snapshots) { ResultsGroup group = new ResultsGroup( _vmConfig.Name, snapshotConfig.Name, snapshotConfig.Description); results.Add(group); DriverTaskInstance driverTaskInstance = new DriverTaskInstance( _config, _logpath, _simulationOnly, _vmConfig, _installersConfig, snapshotConfig); foreach (InstallerConfigProxy installerConfigProxy in _installersConfig) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler <ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions options = new DriverTaskInstance.DriverTaskInstanceOptions(); options.Install = _install & installerConfig.Install; options.Uninstall = _uninstall & installerConfig.UnInstall; options.PowerOff = snapshotConfig.PowerOff; if ((options.Install && installerConfig.Install) || (options.Uninstall && installerConfig.UnInstall)) { driverTaskInstance.InstallUninstall(installerConfig, options); group.AddRange(driverTaskInstance.Results); } else { ConsoleOutput.WriteLine("Skipping {0} of '{1}' on '{2}:{3}'", ProcedureString, installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } } } return(results); }
public override List<ResultsGroup> Run() { List<ResultsGroup> results = new List<ResultsGroup>(); foreach (SnapshotConfig snapshotConfig in _vmConfig.Snapshots) { ResultsGroup group = new ResultsGroup( _vmConfig.Name, snapshotConfig.Name, snapshotConfig.Description); results.Add(group); DriverTaskInstance driverTaskInstance = new DriverTaskInstance( _config, _logpath, _simulationOnly, _vmConfig, _installersConfig, snapshotConfig); foreach (InstallerConfigProxy installerConfigProxy in _installersConfig) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler<ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions options = new DriverTaskInstance.DriverTaskInstanceOptions(); options.Install = _install & installerConfig.Install; options.Uninstall = _uninstall & installerConfig.UnInstall; options.PowerOff = snapshotConfig.PowerOff; if ((options.Install && installerConfig.Install) || (options.Uninstall && installerConfig.UnInstall)) { driverTaskInstance.InstallUninstall(installerConfig, options); group.AddRange(driverTaskInstance.Results); } else { ConsoleOutput.WriteLine("Skipping {0} of '{1}' on '{2}:{3}'", ProcedureString, installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } } } return results; }
public void SimpleAssign() { const string str = "HelloWorld"; // Arrange var obj = new SomeHiddenPropertyClass(); IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SimpleString)); // Act var result = reflectionResolver.Update(obj, str); // Assert Assert.IsTrue(result); Assert.AreEqual(str, obj.SimpleString); }
public void AssignStringToInt() { const string number = "5"; // Arrange var obj = new SomeHiddenPropertyClass(); IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SimpleInt)); // Act var result = reflectionResolver.Update(obj, number); // Assert Assert.IsTrue(result); Assert.AreEqual(int.Parse(number), obj.SimpleInt); }
public void AssignDoubleToString() { const double value = 7.78; // Arrange var obj = new SomeHiddenPropertyClass(); IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SimpleString)); // Act var result = reflectionResolver.Update(obj, value); // Assert Assert.IsTrue(result); Assert.AreEqual(value, Convert.ToDouble(obj.SimpleString)); }
public void SimpleResolve(bool createDerived) { const string str = "HelloWorld"; // Arrange var obj = createDerived ? new SomeHiddenPropertyClass() : new SomeClass(); obj.SimpleString = str; IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SimpleString)); // Act var result = reflectionResolver.Resolve(obj); // Assert Assert.IsTrue(result is string); Assert.AreEqual(str, (string)result); }
public void HiddenPropertyByNewKeyword() { // Arrange IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SomeObject)); var obj = new SomeHiddenPropertyClass { SomeObject = new SomeImplementation() }; // Act // Should resolve the value of SomeObject object result = null; Assert.DoesNotThrow(delegate { result = reflectionResolver.Resolve(obj); }); // Assert Assert.NotNull(result); }
public void PartiallySelectedTypes() { var schema = new UnionSchema(new[] { new RecordSchema(nameof(OrderCancelledEvent), new[] { new RecordField("timestamp", new StringSchema()) }), new RecordSchema(nameof(OrderCreatedEvent), new[] { new RecordField("timestamp", new StringSchema()), new RecordField("total", new BytesSchema() { LogicalType = new DecimalLogicalType(5, 2) }) }) }); var codec = new BinaryCodec(); var resolver = new ReflectionResolver(); var deserializer = DeserializerBuilder.BuildDeserializer <OrderCreatedEvent>(schema); var serializer = new BinarySerializerBuilder(BinarySerializerBuilder.CreateBinarySerializerCaseBuilders(codec) .Prepend(builder => new OrderSerializerBuilderCase(resolver, codec, builder))) .BuildSerializer <OrderEvent>(schema); var value = new OrderCreatedEvent { Timestamp = DateTime.UtcNow, Total = 40M }; var result = deserializer.Deserialize(serializer.Serialize(value)); Assert.Equal(value.Timestamp, result.Timestamp); Assert.Equal(value.Total, result.Total); }
private static Func <MemberInfo[]> ReadFastAccessorsCore(int[] metadataTokens, Lazy <Type> defaultType) { Assumes.NotNull(metadataTokens); return(() => { MemberInfo[] accessors = new MemberInfo[metadataTokens.Length]; Type type = defaultType.Value; Assumes.NotNull(type); for (int i = 0; i < metadataTokens.Length; i++) { if (metadataTokens[i] != 0) { accessors[i] = (MemberInfo)ReflectionResolver.ResolveMember(type.Assembly.ManifestModule, metadataTokens[i]); } else { accessors[i] = null; } } return accessors; }); }
public override List<ResultsGroup> Run() { List<ResultsGroup> results = new List<ResultsGroup>(); foreach (SnapshotConfig snapshotConfig in _vmConfig.Snapshots) { ResultsGroup group = new ResultsGroup( _vmConfig.Name, snapshotConfig.Name, snapshotConfig.Description); results.Add(group); DriverTaskInstance driverTaskInstance = new DriverTaskInstance( _config, _logpath, _simulationOnly, _vmConfig, _installersConfig, snapshotConfig); List<InstallerConfigProxy> uninstallConfigs = new List<InstallerConfigProxy>(); foreach (InstallerConfigProxy installerConfigProxy in _installersConfig) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler<ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions installOptions = new DriverTaskInstance.DriverTaskInstanceOptions(); installOptions.Install = installerConfig.Install; installOptions.Uninstall = false; installOptions.PowerOff = false; if (installerConfig.Install) { if (driverTaskInstance.InstallUninstall(installerConfig, installOptions)) { uninstallConfigs.Insert(0, installerConfigProxy); } group.AddRange(driverTaskInstance.Results); } else { ConsoleOutput.WriteLine("Skipping install of '{0}' on '{1}:{2}'", installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } } foreach (InstallerConfigProxy installerConfigProxy in uninstallConfigs) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler<ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions uninstallOptions = new DriverTaskInstance.DriverTaskInstanceOptions(); uninstallOptions.Install = false; uninstallOptions.Uninstall = installerConfig.UnInstall; uninstallOptions.PowerOff = false; if (installerConfig.UnInstall) { driverTaskInstance.InstallUninstall(installerConfig, uninstallOptions); group.AddRange(driverTaskInstance.Results); } else { ConsoleOutput.WriteLine("Skipping uninstall of '{0}' on '{1}:{2}'", installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } } if (snapshotConfig.PowerOff) { try { driverTaskInstance.PowerOff(); } catch (Exception ex) { ConsoleOutput.WriteLine("Error powering off '{0}:{1}'", _vmConfig.Name, snapshotConfig.Name); ConsoleOutput.WriteLine(ex); } } } return results; }
public override List <ResultsGroup> Run() { List <ResultsGroup> results = new List <ResultsGroup>(); foreach (SnapshotConfig snapshotConfig in _vmConfig.Snapshots) { ResultsGroup group = new ResultsGroup( _vmConfig.Name, snapshotConfig.Name, snapshotConfig.Description); results.Add(group); DriverTaskInstance driverTaskInstance = new DriverTaskInstance( _config, _logpath, _simulationOnly, _vmConfig, _installersConfig, snapshotConfig); List <InstallerConfigProxy> uninstallConfigs = new List <InstallerConfigProxy>(); foreach (InstallerConfigProxy installerConfigProxy in _installersConfig) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler <ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions installOptions = new DriverTaskInstance.DriverTaskInstanceOptions(); installOptions.Install = installerConfig.Install; installOptions.Uninstall = false; installOptions.PowerOff = false; if (installerConfig.Install) { if (driverTaskInstance.InstallUninstall(installerConfig, installOptions)) { uninstallConfigs.Add(installerConfigProxy); } } else { ConsoleOutput.WriteLine("Skipping '{0}' on '{1}:{2}'", installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } group.AddRange(driverTaskInstance.Results); } foreach (InstallerConfigProxy installerConfigProxy in uninstallConfigs) { InstallerConfig installerConfig = installerConfigProxy.Instance; InstallerConfig.OnRewrite = new EventHandler <ReflectionResolverEventArgs>( delegate(object sender, ReflectionResolverEventArgs args) { object[] objs = { snapshotConfig, _vmConfig, _installersConfig }; ReflectionResolver resolver = new ReflectionResolver(objs); string result = null; if (resolver.TryResolve(args.VariableType + "Config", args.VariableName, out result)) { args.Result = result; } }); DriverTaskInstance.DriverTaskInstanceOptions uninstallOptions = new DriverTaskInstance.DriverTaskInstanceOptions(); uninstallOptions.Install = false; uninstallOptions.Uninstall = installerConfig.UnInstall; uninstallOptions.PowerOff = false; if (installerConfig.UnInstall) { driverTaskInstance.InstallUninstall(installerConfig, uninstallOptions); group.AddRange(driverTaskInstance.Results); } else { ConsoleOutput.WriteLine("Skipping '{0}' on '{1}:{2}'", installerConfig.Name, _vmConfig.Name, snapshotConfig.Name); } } if (snapshotConfig.PowerOff) { try { driverTaskInstance.PowerOff(); } catch (Exception ex) { ConsoleOutput.WriteLine("Error powering off '{0}:{1}'", _vmConfig.Name, snapshotConfig.Name); ConsoleOutput.WriteLine(ex); } } } return(results); }