/// <summary>
 /// Creates an instance initializing it with the specified parameters.
 /// </summary>
 /// <param name="provider">Application configuration provider</param>
 /// <param name="constructorParams">Constructor parameters</param>
 /// <param name="properties">Configuration instance properties</param>
 /// <param name="instancePrefix">Application instance prefix</param>
 /// <param name="instanceName">Application instance name</param>
 public AppConfigurationSettings(Type provider, 
     ConstructorParameterSettingsCollection constructorParams = null, 
     PropertySettingsCollection properties = null, 
     string instancePrefix = null, 
     string instanceName = null)
 {
     InstancePrefix = instancePrefix;
     InstanceName = instanceName;
     Provider = provider;
     ConstructorParameters = constructorParams ?? new ConstructorParameterSettingsCollection();
     Properties = properties ?? new PropertySettingsCollection();
 }
        /// <summary>
        /// Instantiates an object and sets its properties as provided.
        /// </summary>
        /// <param name="type">Type to instantiate</param>
        /// <param name="constructorParams">Collection of construction parameters</param>
        /// <param name="properties">Collection of properties to set up</param>
        /// <returns>The newly instantiated object</returns>
        public static object CreateInstance(Type type, ConstructorParameterSettingsCollection constructorParams,
            PropertySettingsCollection properties)
        {
            // --- Create the constructor parameter array
            var parameters = new object[constructorParams.Count];
            for (var i = 0; i < constructorParams.Count; i++)
            {
                var converter = TypeDescriptor.GetConverter(constructorParams[i].Type);
                parameters[i] = converter.ConvertFromString(constructorParams[i].Value);
            }

            // --- Instantiate the object
            var tempInstance = Activator.CreateInstance(type, parameters);
            InjectProperties(ref tempInstance, properties);
            return tempInstance;
        }
        public void ContructorWithCollectionInitializerWorks()
        {
            // --- Act
            var collection = new ConstructorParameterSettingsCollection(
                new List<ConstructorParameterSettings>
                    {
                        new ConstructorParameterSettings(typeof(int), "2"),
                        new ConstructorParameterSettings(typeof(string), "hello")
                    });

            // --- Assert
            collection.ShouldHaveCountOf(2);
            collection[0].Type.ShouldEqual(typeof (int));
            collection[0].Value.ShouldEqual("2");
            collection[1].Type.ShouldEqual(typeof (string));
            collection[1].Value.ShouldEqual("hello");
        }
        public void ConstructorWorksWithXElement()
        {
            // --- Act
            var collection1 = new ConstructorParameterSettingsCollection(
                new List<ConstructorParameterSettings>
                    {
                        new ConstructorParameterSettings(typeof (int), "2"),
                        new ConstructorParameterSettings(typeof (string), "hello")
                    });
            var element1 = collection1.WriteToXml("Test");

            var collection2 = new ConstructorParameterSettingsCollection(element1);

            // --- Assert
            element1.Elements("Param").ShouldHaveCountOf(2);
            collection2.ShouldHaveCountOf(2);
        }
示例#5
0
        public void ConstructorWorksWithXElement()
        {
            // --- Act
            var collection1 = new ConstructorParameterSettingsCollection(
                new List <ConstructorParameterSettings>
            {
                new ConstructorParameterSettings(typeof(int), "2"),
                new ConstructorParameterSettings(typeof(string), "hello")
            });
            var element1 = collection1.WriteToXml("Test");

            var collection2 = new ConstructorParameterSettingsCollection(element1);

            // --- Assert
            element1.Elements("Param").ShouldHaveCountOf(2);
            collection2.ShouldHaveCountOf(2);
        }
示例#6
0
        public void ContructorWithCollectionInitializerWorks()
        {
            // --- Act
            var collection = new ConstructorParameterSettingsCollection(
                new List <ConstructorParameterSettings>
            {
                new ConstructorParameterSettings(typeof(int), "2"),
                new ConstructorParameterSettings(typeof(string), "hello")
            });

            // --- Assert
            collection.ShouldHaveCountOf(2);
            collection[0].Type.ShouldEqual(typeof(int));
            collection[0].Value.ShouldEqual("2");
            collection[1].Type.ShouldEqual(typeof(string));
            collection[1].Value.ShouldEqual("hello");
        }
        /// <summary>
        /// Instantiates an object and sets its properties as provided.
        /// </summary>
        /// <param name="type">Type to instantiate</param>
        /// <param name="constructorParams">Collection of construction parameters</param>
        /// <param name="properties">Collection of properties to set up</param>
        /// <returns>The newly instantiated object</returns>
        public static object CreateInstance(Type type, ConstructorParameterSettingsCollection constructorParams,
                                            PropertySettingsCollection properties)
        {
            // --- Create the constructor parameter array
            var parameters = new object[constructorParams.Count];

            for (var i = 0; i < constructorParams.Count; i++)
            {
                var converter = TypeDescriptor.GetConverter(constructorParams[i].Type);
                parameters[i] = converter.ConvertFromString(constructorParams[i].Value);
            }

            // --- Instantiate the object
            var tempInstance = Activator.CreateInstance(type, parameters);

            InjectProperties(ref tempInstance, properties);
            return(tempInstance);
        }
示例#8
0
        public void CreateInstanceWithConstructorParamsWorksAsExpected()
        {
            // --- Arrange
            var settings = new ConstructorParameterSettingsCollection(
                new List <ConstructorParameterSettings>
            {
                new ConstructorParameterSettings(typeof(int), "12"),
                new ConstructorParameterSettings(typeof(int), "23"),
            }
                );

            // --- Act
            var myPoint = ConfigurationHelper.CreateInstance(typeof(MyPoint), settings,
                                                             new PropertySettingsCollection()) as MyPoint;

            // --- Assert
            myPoint.ShouldNotBeNull();
            // ReSharper disable PossibleNullReferenceException
            myPoint.X.ShouldEqual(12);
            // ReSharper restore PossibleNullReferenceException
            myPoint.Y.ShouldEqual(23);
        }
        public void CreateInstanceWithConstructorParamsWorksAsExpected()
        {
            // --- Arrange
            var settings = new ConstructorParameterSettingsCollection(
                new List<ConstructorParameterSettings>
                    {
                        new ConstructorParameterSettings(typeof(int), "12"),
                        new ConstructorParameterSettings(typeof(int), "23"),
                    }
                );

            // --- Act
            var myPoint = ConfigurationHelper.CreateInstance(typeof(MyPoint), settings,
                new PropertySettingsCollection()) as MyPoint;

            // --- Assert
            myPoint.ShouldNotBeNull();
            // ReSharper disable PossibleNullReferenceException
            myPoint.X.ShouldEqual(12);
            // ReSharper restore PossibleNullReferenceException
            myPoint.Y.ShouldEqual(23);
        }
 /// <summary>
 /// Creates a new instance of this class and initializes it from the specified XML element.
 /// </summary>
 /// <param name="element">XML element to initialize this instance from.</param>
 public AppConfigurationSettings(XElement element)
 {
     ConstructorParameters = new ConstructorParameterSettingsCollection();
     Properties = new PropertySettingsCollection();
     ReadFromXml(element);
 }