/// <summary>
        /// Gets the fields of a type that can be serialized
        /// </summary>
        /// <param name="type">Type to analyze</param>
        /// <param name="baseClassToStopIterating">Stop iterating fields when encountering this base type</param>
        /// <returns>The fields of a type</returns>
        public static FieldInfo[] GetFields(Type type, Type baseClassToStopIterating)
        {
            var key = new TwoTypesClass
            {
                Type      = type,
                OtherType = baseClassToStopIterating
            };

            return(FieldsForType.GetOrAdd(key,
                                          t =>
            {
                var fields = new List <FieldInfo>();
                var targetType = t.Type;

                do
                {
                    fields.AddRange(targetType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                                                         | BindingFlags.DeclaredOnly)
                                    .Where(fi => (fi.Attributes & FieldAttributes.NotSerialized) == 0));
                    targetType = targetType.BaseType;
                }while (targetType != null && targetType != key.OtherType);

                return fields.OrderBy(f => f.Name,
                                      StringComparer.Ordinal)
                .ToArray();
            }));
        }
예제 #2
0
 public bool Equals(TwoTypesClass obj)
 {
     return(obj != null &&
            Type == obj.Type &&
            OtherType == obj.OtherType);
 }