Exemple #1
0
        public void TestInjectField()
        {
            var factory        = new ReflectionFactory();
            var reflectedClass = factory.Create(typeof(MockIClassWithAttributes));

            Assert.AreEqual(1, reflectedClass.fields.Length);
        }
Exemple #2
0
        public void TestReflectedClassCreation()
        {
            var factory        = new ReflectionFactory();
            var reflectedClass = factory.Create(typeof(MockIClassWithoutAttributes));

            Assert.NotNull(reflectedClass);
        }
Exemple #3
0
        /// <summary>
        ///  设置属性的值扩展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="property"></param>
        /// <param name="entity"></param>
        /// <param name="value"></param>
        /// <param name="isCache"></param>
        public static void SetPropertyValue <TEntity, TValue>(this PropertyInfo property, TEntity entity, TValue value, bool isCache = true)
            where TEntity : class
        {
            var propertyInfo = ReflectionFactory.Create <TEntity, TEntity, TValue>(property, property, isCache);

            propertyInfo.SetValue(entity, value);
        }
Exemple #4
0
        public void TestPostConstructor()
        {
            var factory        = new ReflectionFactory();
            var reflectedClass = factory.Create(typeof(MockIClassWithAttributes));

            Assert.AreEqual(1, reflectedClass.methods.Length);
        }
    static void Main(string[] args)
    {
        // Here's the usage of a "traditional" factory, which returns objects that implement a common interface.
        // This is a great pattern for a lot of different scenarios.
        // The only downside is that you have to update your factory class whenever you add a new class.
        TraditionalFactory.Create("A_ID").DoIt();
        TraditionalFactory.Create("B_ID").DoIt();
        Console.ReadKey();

        // But what if we make a class that uses reflection to find attributes of classes it can create? Reflection!
        // This works great and now all we have to do is add an attribute to new classes and this thing will just work.
        // (It could also be more generic in its input / output, but I simplified it for this example)
        ReflectionFactory.Create("A_ID").DoIt();
        ReflectionFactory.Create("B_ID").DoIt();
        // Wait, that's great and all, but everyone always says reflection is so slow, and this thing's going to reflect
        // on every object creation...that's not good right?
        Console.ReadKey();

        // So I created this new factory class which gives the speed of the traditional factory combined with the flexibility
        // of the reflection-based factory.
        // The reflection done here is only performed once. After that, it is as if the Create() method is using a switch statement
        Factory <string, IDoSomething> .Create("A_ID").DoIt();

        Factory <string, IDoSomething> .Create("B_ID").DoIt();

        Console.ReadKey();
    }
Exemple #6
0
        public void TestConstructorWhenNoConstruct()
        {
            var factory        = new ReflectionFactory();
            var reflectedClass = factory.Create(typeof(MockIClassWithoutAttributes));

            Assert.NotNull(reflectedClass.constructor);
            Assert.AreEqual(0, reflectedClass.constructorParameters.Length);
        }
Exemple #7
0
        public void TestConstructorWithConstruct()
        {
            var factory        = new ReflectionFactory();
            var reflectedClass = factory.Create(typeof(MockIClassWithAttributes));

            Assert.IsNull(reflectedClass.constructor);
            Assert.NotNull(reflectedClass.paramsConstructor);
            Assert.AreEqual(1, reflectedClass.constructorParameters.Length);
            Assert.AreEqual(typeof(MockClassToDepend), reflectedClass.constructorParameters[0].type);
        }
Exemple #8
0
        public void ReflectionFactoryCreate_CreateReflectionInfo_postConstructorsCorrect()
        {
            //Arrange
            ReflectionFactory reflectionFactory = new ReflectionFactory();
            //Act
            ReflectionInfo reflectionInfo = reflectionFactory.Create(typeof(someClass_e));

            //Assert
            Assert.AreEqual(2, reflectionInfo.methods.Length);
        }
Exemple #9
0
        public void ReflectionFactoryCreate_CreateReflectionInfo_paramsConstructorCorrect()
        {
            //Arrange
            ReflectionFactory reflectionFactory = new ReflectionFactory();
            //Act
            ReflectionInfo reflectionInfo = reflectionFactory.Create(typeof(someClass_d));

            //Assert
            Assert.AreEqual(false, reflectionInfo.paramsConstructor == null);
        }
Exemple #10
0
        public void ReflectionFactoryCreate_CreateReflectionInfo_typeCorrect()
        {
            //Arrange
            ReflectionFactory reflectionFactory = new ReflectionFactory();
            //Act
            ReflectionInfo reflectionInfo = reflectionFactory.Create(typeof(someClass_d));

            //Assert
            Assert.AreEqual(typeof(someClass_d), reflectionInfo.type);
        }
Exemple #11
0
        public void ReflectionFactoryCreate_CreateReflectionInfo_fieldsCorrect()
        {
            //Arrange
            ReflectionFactory reflectionFactory = new ReflectionFactory();
            //Act
            ReflectionInfo reflectionInfo = reflectionFactory.Create(typeof(someClass_f));

            //Assert
            Assert.AreEqual(4, reflectionInfo.fields.Length);
        }
Exemple #12
0
        public void Create_WithNullInstanceResolvers_ShouldNotThrow()
        {
            ReflectionFactory sut = createSut();

            try
            {
                sut.Create(typeof(NullLogger).GetConstructors().FirstOrDefault(), null);
            }
            catch (Exception)
            {
                Assert.True(false, "Should not throw exception");
            }
        }
Exemple #13
0
        IEnumerable <ConfigurationSection> LoadSectionbyType <T>() where T : ConfigurationSection, new()
        {
            T obj = ReflectionFactory.Create <T>().Build();

            if (obj != null)
            {
                IEnumerable <ConfigurationSection> subSections = null;
                if (!sections.TryGetValue(obj.BlockName, out subSections))
                {
                    List <ConfigurationSection> sublist = new List <ConfigurationSection>();
                    foreach (XmlNode node in GetSubNode(obj.BlockName))
                    {
                        T tobj = ReflectionFactory.Create <T>().Build();
                        tobj.Load(node);
                        sublist.Add(tobj);
                    }
                    subSections = sublist;
                }
                return(subSections);
            }
            return(null);
        }
Exemple #14
0
        public T CreateInstance(ReflectConfigSection configSection)
        {
            object newObject = null;

            try
            {
                Type type = Type.GetType(configSection.ComponentType);

                if (type != null)
                {
                    newObject = ReflectionFactory.Create().Build(type, configSection.Params);
                }
                if (newObject != null && newObject is T)
                {
                    return(newObject as T);
                }
            }
            catch (Exception e)
            {
            }
            return(default(T));
        }
Exemple #15
0
        public void Create_WithNullConstructor_ShouldThrowArgumentNullException()
        {
            ReflectionFactory sut = createSut();

            Assert.Throws <ArgumentNullException>(() => sut.Create(null, null));
        }