MongoDB.Bson.Serialization.Conventions.PublicMemberFinderConvention.FindMembers C# (CSharp) Method

FindMembers() public method

Finds the members of a class that are serialized.
public FindMembers ( Type type ) : IEnumerable
type System.Type The class.
return IEnumerable
        public IEnumerable<MemberInfo> FindMembers(Type type)
        {
            foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
            {
                if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral)
                {
                    // we can't write
                    continue;
                }

                yield return fieldInfo;
            }

            foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
            {
                if (!propertyInfo.CanRead || (!propertyInfo.CanWrite && type.Namespace != null))
                {
                    // we can't write or it is anonymous...
                    continue;
                }

                // skip indexers
                if (propertyInfo.GetIndexParameters().Length != 0)
                {
                    continue;
                }

                // skip overridden properties (they are already included by the base class)
                var getMethodInfo = propertyInfo.GetGetMethod(true);
                if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != type)
                {
                    continue;
                }

                yield return propertyInfo;
            }
        }
    }

Usage Example

        public void TestPublicPropertyFinderConvention() {
            var convention = new PublicMemberFinderConvention();

            var properties = convention.FindMembers(typeof(TestClass)).ToList();

            Assert.AreEqual(3, properties.Count);
            Assert.IsTrue(properties.Any(x => x.Name == "Public"));
            Assert.IsTrue(properties.Any(x => x.Name == "PrivateWrite"));
            Assert.IsTrue(properties.Any(x => x.Name == "PrivateRead"));
        }
All Usage Examples Of MongoDB.Bson.Serialization.Conventions.PublicMemberFinderConvention::FindMembers
PublicMemberFinderConvention