Пример #1
0
        public void GetWithNonPublic()
        {
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            ImmediateType immediateType = TypeAccessor.Get <PublicValueTypeTestClass>(true);

            CheckPublicAndNonPublicInstanceMembers(immediateType);

            immediateType = TypeAccessor.Get(typeof(PublicValueTypeTestClass), true);
            CheckPublicAndNonPublicInstanceMembers(immediateType);

            #region Local functions

            void CheckPublicAndNonPublicInstanceMembers(ImmediateType type)
            {
                Assert.IsNotNull(type);
                Assert.AreEqual(typeof(PublicValueTypeTestClass), type.Type);
                // Public & Non Public instance members
                CollectionAssert.AreEquivalent(
                    classifiedMembers.AllFields,
                    immediateType.Fields.Select(field => field.FieldInfo));
                CollectionAssert.AreEquivalent(
                    classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.NonPublicInstanceProperties).Concat(classifiedMembers.StaticProperties),
                    immediateType.Properties.Select(property => property.PropertyInfo));
            }

            #endregion
        }
Пример #2
0
        public void ImmediateTypeGetMembers()
        {
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var immediateType = new ImmediateType(typeof(PublicValueTypeTestClass), TypeAccessor.DefaultFlags | BindingFlags.NonPublic);

            CollectionAssert.AreEquivalent(
                classifiedMembers.AllMembers,
                SelectAllMemberInfos(immediateType.Members));
            CollectionAssert.AreEquivalent(
                classifiedMembers.AllMembers,
                SelectAllMemberInfos(immediateType.GetMembers()));

            #region Local function

            IEnumerable <MemberInfo> SelectAllMemberInfos(IEnumerable <ImmediateMember> members)
            {
                return(members.Select <ImmediateMember, MemberInfo>(member =>
                {
                    if (member is ImmediateField field)
                    {
                        return field.FieldInfo;
                    }
                    if (member is ImmediateProperty property)
                    {
                        return property.PropertyInfo;
                    }

                    throw new InvalidOperationException("Members contain an unexpected value");
                }));
            }

            #endregion
        }
Пример #3
0
        public void GetWithFlags()
        {
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            const BindingFlags flags         = BindingFlags.Public | BindingFlags.Static;
            ImmediateType      immediateType = TypeAccessor.Get <PublicValueTypeTestClass>(flags);

            CheckStaticInstanceMembers(immediateType);

            immediateType = TypeAccessor.Get(typeof(PublicValueTypeTestClass), flags);
            CheckStaticInstanceMembers(immediateType);

            #region Local functions

            void CheckStaticInstanceMembers(ImmediateType type)
            {
                Assert.IsNotNull(type);
                Assert.AreEqual(typeof(PublicValueTypeTestClass), type.Type);
                // Static members
                CollectionAssert.AreEqual(
                    classifiedMembers.StaticFields.Concat(classifiedMembers.ConstFields),
                    immediateType.Fields.Select(field => field.FieldInfo));
                CollectionAssert.AreEquivalent(
                    classifiedMembers.StaticProperties,
                    immediateType.Properties.Select(property => property.PropertyInfo));
            }

            #endregion
        }
        public void ObjectWrapperGetMembers()
        {
            var testObject = new PublicValueTypeTestClass();
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var objectWrapper = new ObjectWrapper(testObject);

            CollectionAssert.AreEquivalent(
                classifiedMembers.AllPublicMembers,
                SelectAllMemberInfos(objectWrapper.Members));
            CollectionAssert.AreEquivalent(
                classifiedMembers.AllPublicMembers,
                SelectAllMemberInfos(objectWrapper.GetMembers()));

            #region Local function

            IEnumerable <MemberInfo> SelectAllMemberInfos(IEnumerable <ImmediateMember> members)
            {
                return(members.Select <ImmediateMember, MemberInfo>(member =>
                {
                    if (member is ImmediateField field)
                    {
                        return field.FieldInfo;
                    }
                    if (member is ImmediateProperty property)
                    {
                        return property.PropertyInfo;
                    }

                    throw new InvalidOperationException("Members contain an unexpected value");
                }));
            }

            #endregion
        }
Пример #5
0
        public void ImmediateTypeWithFlags()
        {
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var testType = new ImmediateType(typeof(PublicValueTypeTestClass)); // BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static

            CollectionAssert.AreEqual(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields),
                testType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties),
                testType.Properties.Select(property => property.PropertyInfo));

            testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.NonPublic | BindingFlags.Instance);
            CollectionAssert.AreEqual(
                classifiedMembers.NonPublicInstanceFields,
                testType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.NonPublicInstanceProperties,
                testType.Properties.Select(property => property.PropertyInfo));

            testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            CollectionAssert.AreEqual(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.NonPublicInstanceFields),
                testType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.NonPublicInstanceProperties),
                testType.Properties.Select(property => property.PropertyInfo));

            testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            CollectionAssert.AreEqual(
                classifiedMembers.StaticFields.Concat(classifiedMembers.ConstFields),
                testType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.StaticProperties,
                testType.Properties.Select(property => property.PropertyInfo));

            testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.IgnoreCase);
            CollectionAssert.AreEqual(
                Enumerable.Empty <FieldInfo>(),
                testType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEqual(
                Enumerable.Empty <PropertyInfo>(),
                testType.Properties.Select(property => property.PropertyInfo));
        }
Пример #6
0
        public void ImmediateTypeSerialization()
        {
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var           type             = new ImmediateType(typeof(PublicValueTypeTestClass));
            ImmediateType deserializedType = SerializeAndDeserialize(type);

            Assert.AreEqual(typeof(PublicValueTypeTestClass), deserializedType.Type);
            Assert.AreEqual(typeof(object), deserializedType.BaseType);
            Assert.IsNull(deserializedType.DeclaringType);
            Assert.AreEqual(nameof(PublicValueTypeTestClass), deserializedType.Name);
            Assert.AreEqual(
                $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(PublicValueTypeTestClass)}",
                deserializedType.FullName);
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields),
                deserializedType.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties),
                deserializedType.Properties.Select(property => property.PropertyInfo));
        }
Пример #7
0
        public void ImmediateTypeValueType()
        {
            // Public class
            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var immediateTypePublic = new ImmediateType(typeof(PublicValueTypeTestClass));

            Assert.AreEqual(typeof(PublicValueTypeTestClass), immediateTypePublic.Type);
            Assert.AreEqual(typeof(object), immediateTypePublic.BaseType);
            Assert.IsNull(immediateTypePublic.DeclaringType);
            Assert.AreEqual(nameof(PublicValueTypeTestClass), immediateTypePublic.Name);
            Assert.AreEqual(
                $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(PublicValueTypeTestClass)}",
                immediateTypePublic.FullName);
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields),
                immediateTypePublic.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties),
                immediateTypePublic.Properties.Select(property => property.PropertyInfo));

            // Internal class
            classifiedMembers = TypeClassifiedMembers.GetForInternalValueTypeTestObject();

            var immediateTypeInternal = new ImmediateType(typeof(InternalValueTypeTestClass));

            Assert.AreEqual(typeof(InternalValueTypeTestClass), immediateTypeInternal.Type);
            Assert.AreEqual(typeof(object), immediateTypeInternal.BaseType);
            Assert.IsNull(immediateTypeInternal.DeclaringType);
            Assert.AreEqual(nameof(InternalValueTypeTestClass), immediateTypeInternal.Name);
            Assert.AreEqual(
                $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(InternalValueTypeTestClass)}",
                immediateTypeInternal.FullName);
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields),
                immediateTypeInternal.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties),
                immediateTypeInternal.Properties.Select(property => property.PropertyInfo));
        }
Пример #8
0
        public void GetImmediateFields()
        {
            TypeClassifiedMembers classifiedMember = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            Type testType = typeof(PublicValueTypeTestClass);
            IEnumerable <ImmediateField> fields = testType.GetImmediateFields();

            Assert.IsNotNull(fields);
            CollectionAssert.AreEquivalent(
                classifiedMember.PublicInstanceFields.Concat(classifiedMember.StaticFields).Concat(classifiedMember.ConstFields),
                fields.Select(field => field.FieldInfo));

            fields = testType.GetImmediateFields(BindingFlags.NonPublic | BindingFlags.Instance);
            Assert.IsNotNull(fields);
            CollectionAssert.AreEquivalent(
                classifiedMember.NonPublicInstanceFields,
                fields.Select(field => field.FieldInfo));

            testType = typeof(EmptyType);
            fields   = testType.GetImmediateFields();
            Assert.IsNotNull(fields);
            CollectionAssert.IsEmpty(fields);
        }
Пример #9
0
        public void GetImmediateProperties()
        {
            TypeClassifiedMembers classifiedMember = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            Type testType = typeof(PublicValueTypeTestClass);
            IEnumerable <ImmediateProperty> properties = testType.GetImmediateProperties();

            Assert.IsNotNull(properties);
            CollectionAssert.AreEquivalent(
                classifiedMember.PublicInstanceProperties.Concat(classifiedMember.StaticProperties),
                properties.Select(property => property.PropertyInfo));

            properties = testType.GetImmediateProperties(BindingFlags.NonPublic | BindingFlags.Instance);
            Assert.IsNotNull(properties);
            CollectionAssert.AreEquivalent(
                classifiedMember.NonPublicInstanceProperties,
                properties.Select(property => property.PropertyInfo));

            testType   = typeof(EmptyType);
            properties = testType.GetImmediateProperties();
            Assert.IsNotNull(properties);
            CollectionAssert.IsEmpty(properties);
        }
        public void ObjectWrapperInfo()
        {
            var testObject = new PublicValueTypeTestClass();

            TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject();

            var objectWrapper = new ObjectWrapper(testObject);

            Assert.AreSame(testObject, objectWrapper.Object);
            Assert.AreEqual(typeof(PublicValueTypeTestClass), objectWrapper.Type);
            Assert.AreEqual(
                new ImmediateType(typeof(PublicValueTypeTestClass)),
                objectWrapper.ImmediateType);
            CollectionAssert.AreEquivalent(
                classifiedMembers.AllPublicMembers.Select(member => member.Name),
                objectWrapper.Members.Select(field => field.Name));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields),
                objectWrapper.Fields.Select(field => field.FieldInfo));
            CollectionAssert.AreEquivalent(
                classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties),
                objectWrapper.Properties.Select(property => property.PropertyInfo));
        }