Esempio n. 1
0
        public override void Activate()
        {
            IDictionary <string, Lazy <object> > valuesLazy = ImportValues.ToDictionary(
                x => x.Key.MemberName,
                v => v.Value);

            if (CachedInstance == null && this.Definition.ParameterlessConstructor)
            {
                _cachedInstance = CustomActivator.CreateInstance(Definition.GetPartType());

                var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value);
                CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues);
            }
            else if (this.Definition.ParameterlessConstructor)
            {
                var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value);
                CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues);
            }
            else
            {
                var constructor        = Definition.GetConstructor();
                var requiredParameters = constructor.GetParameters();

                //Sorting imports to match constructor order
                object[] unpackValues = new SortedDictionary <string, Lazy <object> >(valuesLazy)
                                        .Zip(requiredParameters, (lazyValues, required) => new { lazyValues, required })
                                        .OrderBy(x => x.required.Name)
                                        .Select(x => x.lazyValues.Value.Value)
                                        .ToArray <object>();

                _cachedInstance = Definition.GetConstructor().Invoke(unpackValues);
            }
        }
        public void Activate()
        {
            object     obj    = CustomActivator.CreateInstance(_member.DeclaringType);
            MethodInfo method = obj.GetType().GetMethod(_member.Name);

            _cachedInstance = Delegate.CreateDelegate(_delType, obj, method);
        }
        public void ReflectionHelperDeveSobrescreverPropriedadesDoObjetoJaInstanciado()
        {
            var myObject = new InstantiableObject();

            myObject.Integer         = 1;
            myObject.Boolean         = false;
            myObject.AnotherInteger  = 2;
            myObject.AnotherBoolean  = false;
            myObject.IntegerNullable = 3;

            var properties = new Dictionary <string, object>();

            properties.Add("Integer", "10");
            properties.Add("Boolean", "true");
            properties.Add("AnotherInteger", "20");
            properties.Add("AnotherBoolean", "true");
            properties.Add("IntegerNullable", null);

            CustomActivator.PopulateObjectProperties(myObject, properties);

            myObject.Integer.Should().Be(10);
            myObject.Boolean.Should().Be(true);
            myObject.AnotherInteger.Should().Be(20);
            myObject.AnotherBoolean.Should().Be(true);
            myObject.IntegerNullable.Should().Be(null);
        }
        public void CustomActivatorMustInstantiateIntarfaceWithItsDefaultValues()
        {
            var resultObject = CustomActivator.CreateInstance <IParametersTest>();

            resultObject.Integer.Should().Be(0);
            resultObject.Boolean.Should().Be(false);
            resultObject.AnotherInteger.Should().Be(0);
            resultObject.AnotherBoolean.Should().Be(false);
            resultObject.IntegerNullable.Should().Be(null);
        }
Esempio n. 5
0
        public void ConfigurableServiceProviderIsSet_WhenExecuting_RegistrationIsCalled()
        {
            var serviceProvider = new CustomActivator();

            var runtime = new CoreRuntime(new RuntimeConfiguration {
                ServiceProvider = serviceProvider
            });

            runtime.Execute(new ExecutionMetadata {
                JobType = typeof(TestJob).AssemblyQualifiedName
            });

            #pragma warning disable CS0618 // Type or member is obsolete
            Assert.AreEqual(1, serviceProvider.RegisteredInstances.OfType <RuntimeContext>().Count(), "There should be a single registration of the RuntimeContext");
            #pragma warning restore CS0618 // Type or member is obsolete
        }
        public IEnumerable <TObject> CreateCollection <TObject>(IEnumerable <dynamic> dataCollection)
            where TObject : class
        {
            List <TObject> collection = new List <TObject>();

            IDictionary <string, object> unwrappedData;

            foreach (var data in dataCollection)
            {
                unwrappedData = data;

                TObject instance = CustomActivator.CreateInstanceAndPopulate <TObject>(unwrappedData);
                collection.Add(instance);
            }

            return(collection);
        }
Esempio n. 7
0
        public void ConfigurableServiceProviderIsSet_WhenExecuting_ContextMatchesMetaInfo()
        {
            var serviceProvider = new CustomActivator();

            var userName    = "******";
            var userDisplay = "Schnyder, Michael";

            var runtime = new CoreRuntime(new RuntimeConfiguration {
                ServiceProvider = serviceProvider
            });

            runtime.Execute(new ExecutionMetadata {
                JobType = typeof(TestJob).AssemblyQualifiedName, UserId = userName, UserDisplayName = userDisplay
            });

            #pragma warning disable CS0618 // Type or member is obsolete
            var ctx = serviceProvider.RegisteredInstances.OfType <RuntimeContext>().Single();

            Assert.AreEqual(userName, ctx.UserId);
            Assert.AreEqual(userDisplay, ctx.UserDisplayName);
            #pragma warning restore CS0618 // Type or member is obsolete
        }
        public ConfigurationReader Read(IConfigurationSection configSection)
        {
            var assemblies      = new UsingSectionReader().GetAssemblies(configSection.GetSection("Using"));
            var typeFinder      = new TypeFinder(assemblies);
            var customActivator = new CustomActivator(_serviceProvider, typeFinder);

            var endpointSectionReader        = new EndpointSectionReader(customActivator);
            var errorPoliciesSectionReader   = new ErrorPoliciesSectionReader(customActivator);
            var inboundSettingsSectionReader = new InboundSettingsSectionReader();
            var inboundSectionReader         = new InboundSectionReader(typeFinder, endpointSectionReader, errorPoliciesSectionReader, inboundSettingsSectionReader);
            var outboundSectionReader        = new OutboundSectionReader(typeFinder, endpointSectionReader);

            Inbound = inboundSectionReader
                      .GetConfiguredInbound(configSection.GetSection("Inbound"))
                      .ToList();

            Outbound = outboundSectionReader
                       .GetConfiguredOutbound(configSection.GetSection("Outbound"))
                       .ToList();

            return(this);
        }
        public void ReflectionHelperDeveInstanciarObjetoComPropriedadesPopuladas()
        {
            var properties = new Dictionary <string, object>();

            //Passing the correct types (int and bool)
            properties.Add("Integer", 30);
            properties.Add("Boolean", true);

            //Passing the types in string to test if CustomActivator will cast correctly
            properties.Add("AnotherInteger", "99");
            properties.Add("AnotherBoolean", "true");

            //We will not pass this property to test if CustomActivator will bring its default value(NULL)
            //properties.Add("IntegerNullable", 50);

            var resultObject = CustomActivator.CreateInstanceAndPopulateProperties <InstantiableObject>(properties);

            resultObject.Integer.Should().Be(30);
            resultObject.Boolean.Should().Be(true);
            resultObject.AnotherInteger.Should().Be(99);
            resultObject.AnotherBoolean.Should().Be(true);
            resultObject.IntegerNullable.Should().Be(null);
        }
 public EndpointSectionReader(CustomActivator customActivator)
 {
     _customActivator = customActivator;
 }
Esempio n. 11
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            bool flag = false;

            if (binder.Type == typeof(XmlDocument) &&
                !binder.Type.IsAbstract)
            {
                result = ConversionsEngine.CreateXmlDocument(_innerData);
                return(true);
            }

            if (!binder.Type.IsAbstract &&
                !typeof(ICollection).IsAssignableFrom(binder.Type) &&
                !typeof(IEnumerable).IsAssignableFrom(binder.Type) &&
                _innerData.Count != 0)
            {
                try
                {
                    dynamic dynamicData = this.InnerObjects.FirstOrDefault();

                    if (dynamicData != null)
                    {
                        IDictionary <string, object> objData = dynamicData;
                        result = CustomActivator.CreateInstanceAndPopulate(binder.Type, objData);
                    }
                    else
                    {
                        result = CustomActivator.CreateInstanceAndPopulate(binder.Type, _innerData);
                    }

                    flag = true;
                }
                catch (InvalidCastException e)
                {
                    result = null;
                }
                catch (Exception e)
                {
                    result = null;
                }

                return(flag);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(binder.Type))
            {
                if (binder.Type == typeof(string))
                {
                    result = this.ToString();
                }
                else if (binder.Type == typeof(IDictionary <string, object>))
                {
                    result = _innerData;
                }
                else if (binder.Type.IsGenericType)
                {
                    Type genericType = binder.Type.GenericTypeArguments[0];

                    if (genericType == typeof(XmlDocument))
                    {
                        List <XmlDocument> collection = new List <XmlDocument>();

                        foreach (var entry in _innerData)
                        {
                            collection.Add(ConversionsEngine.CreateXmlDocument(entry));
                        }

                        result = collection;
                    }
                    else
                    {
                        result = ConversionsEngine.CreateCollection(genericType, InnerObjects);
                    }
                }
                else if (binder.Type.BaseType == typeof(Array))
                {
                    result = ConversionsEngine.CreateArray(binder.Type.GetElementType(), InnerObjects);
                }
                else
                {
                    result = null;
                    return(false);
                }

                return(true);
            }

            throw new InvalidCastException("Could not cast to type which is not implementing IAppSettings");
        }
Esempio n. 12
0
 public ErrorPoliciesSectionReader(CustomActivator customActivator)
 {
     _customActivator = customActivator;
 }
 public ErrorPoliciesSectionReader(TypeFinder typeFinder, CustomActivator customActivator, EndpointSectionReader endpointSectionReader)
 {
     _typeFinder            = typeFinder;
     _customActivator       = customActivator;
     _endpointSectionReader = endpointSectionReader;
 }