示例#1
0
        /// <summary>
        /// Constructs a new instance
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cache"></param>
        /// <param name="type"></param>
        /// <param name="fieldNameConvention"></param>
        public TypeData(FudgeContext context, TypeDataCache cache, Type type, FudgeFieldNameConvention fieldNameConvention)
        {
            Type = type;
            DefaultConstructor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
            Constructors       = type.GetConstructors();
            CustomAttributes   = type.GetCustomAttributes(true);

            cache.RegisterTypeData(this);

            fieldNameConvention = OverrideFieldNameConvention(type, fieldNameConvention);

            var kind         = CalcKind(context, cache, type, fieldNameConvention, out subType, out subType2, out fieldType);
            var inlineAttrib = GetCustomAttribute <FudgeInlineAttribute>();

            if (inlineAttrib != null)
            {
                kind = inlineAttrib.Inline ? TypeKind.Inline : TypeKind.Reference;
            }
            Kind = kind;

            if (kind != TypeKind.FudgePrimitive)        // If it's primitive we won't need to look inside it to serialize it
            {
                ScanProperties(context, cache, fieldNameConvention);
                ScanFields(context, cache, fieldNameConvention);

                PublicMethods       = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                AllInstanceMethods  = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                StaticPublicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
            }
        }
        public void RangeChecking()
        {
            var context = new FudgeContext();
            var cache = new TypeDataCache(context);

            Assert.Throws<ArgumentNullException>(() => new TypeDataCache(null));
            Assert.Throws<ArgumentNullException>(() => cache.GetTypeData(null, FudgeFieldNameConvention.Identity));
        }
        public void HandlesCycles()
        {
            var context = new FudgeContext();
            var cache = new TypeDataCache(context);

            var data = cache.GetTypeData(typeof(Cycle), FudgeFieldNameConvention.Identity);
            Assert.NotNull(data);
            Assert.Equal(data, data.Properties[0].TypeData);
        }
        /// <summary>
        /// Constructs a new <see cref="FudgeSurrogateSelector"/>.
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> for this selector.</param>
        public FudgeSurrogateSelector(FudgeContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            this.context = context;
            this.typeDataCache = new TypeDataCache(context);
            this.selectors = BuildSelectorList();
        }
示例#5
0
        private void ScanFields(FudgeContext context, TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention)
        {
            var fields = Type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            var list   = from field in fields
                         where field.GetCustomAttributes(typeof(FudgeTransientAttribute), true).Length == 0
                         select new PropertyData(cache, fieldNameConvention, field);

            Fields = list.ToArray();
        }
        public void GettingTypeData()
        {
            var context = new FudgeContext();
            var cache = new TypeDataCache(context);

            TypeData data = cache.GetTypeData(this.GetType(), FudgeFieldNameConvention.Identity);
            Assert.NotNull(data);
            TypeData data2 = cache.GetTypeData(this.GetType(), FudgeFieldNameConvention.Identity);
            Assert.Same(data, data2);
        }
示例#7
0
        private void ScanProperties(FudgeContext context, TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention)
        {
            var props = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var list  = from prop in props
                        where prop.GetIndexParameters().Length == 0 &&          // We don't want to deal with anything with indices - e.g. this[string]
                        prop.GetCustomAttributes(typeof(FudgeTransientAttribute), true).Length == 0
                        select new PropertyData(cache, fieldNameConvention, prop);

            Properties = list.ToArray();
        }
        /// <summary>
        /// Constructs a new <see cref="FudgeSurrogateSelector"/>.
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> for this selector.</param>
        public FudgeSurrogateSelector(FudgeContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            this.context       = context;
            this.typeDataCache = new TypeDataCache(context);
            this.selectors     = BuildSelectorList();
        }
示例#9
0
            private PropertyData(TypeDataCache typeCache, FudgeFieldNameConvention fieldNameConvention, MemberInfo info, Type memberType)
            {
                this.info           = info;
                this.name           = info.Name;
                this.typeData       = typeCache.GetTypeData(memberType, fieldNameConvention);
                this.serializedName = GetSerializedName(info, fieldNameConvention);

                var inlineAttrib = GetCustomAttribute <FudgeInlineAttribute>();

                if (inlineAttrib == null)
                {
                    this.kind = typeData.Kind;
                }
                else
                {
                    this.kind = inlineAttrib.Inline ? TypeKind.Inline : TypeKind.Reference;
                }
            }
示例#10
0
        private static TypeKind CalcKind(FudgeContext context, TypeDataCache typeCache, Type type, FudgeFieldNameConvention fieldNameConvention, out TypeData subType, out TypeData subType2, out FudgeFieldType fieldType)
        {
            // REVIEW 2010-02-14 t0rx -- There seems to be some duplication here with the FudgeSurrogateSelector, should look at joining up
            subType   = null;
            subType2  = null;
            fieldType = context.TypeDictionary.GetByCSharpType(type);
            if (fieldType != null)
            {
                // Just a simple field
                return(TypeKind.FudgePrimitive);
            }

            // Check for arrays
            if (type.IsArray)
            {
                subType = typeCache.GetTypeData(type.GetElementType(), fieldNameConvention);
                return(TypeKind.Inline);
            }

            // Check for dictionaries
            Type keyType, valueType;

            if (DictionarySurrogate.IsDictionary(type, out keyType, out valueType))
            {
                subType  = typeCache.GetTypeData(keyType, fieldNameConvention);
                subType2 = typeCache.GetTypeData(valueType, fieldNameConvention);
                return(TypeKind.Inline);
            }

            // Check for lists
            Type elementType;

            if (ListSurrogate.IsList(type, out elementType))
            {
                subType = typeCache.GetTypeData(elementType, fieldNameConvention);
                return(TypeKind.Inline);
            }

            return(TypeKind.Reference);
        }
示例#11
0
 /// <summary>
 /// Determines whether this kind of surrogate can handle a given type
 /// </summary>
 /// <param name="cache"><see cref="TypeDataCache"/> for type data.</param>
 /// <param name="fieldNameConvention">Convention to use for renaming fields.</param>
 /// <param name="type">Type to test.</param>
 /// <returns>True if this kind of surrogate can handle the type.</returns>
 public static bool CanHandle(TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention, Type type)
 {
     return(CanHandle(cache.GetTypeData(type, fieldNameConvention)));
 }
 /// <summary>
 /// Determines whether this kind of surrogate can handle a given type
 /// </summary>
 /// <param name="cache"><see cref="TypeDataCache"/> for type data.</param>
 /// <param name="fieldNameConvention">Convention to use for renaming fields.</param>
 /// <param name="type">Type to test.</param>
 /// <returns>True if this kind of surrogate can handle the type.</returns>
 public static bool CanHandle(TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention, Type type)
 {
     return CanHandle(cache.GetTypeData(type, fieldNameConvention));
 }
 public ToFromFudgeMsgSurrogateTest()
 {
     typeDataCache = new TypeDataCache(context);
 }
示例#14
0
 /// <summary>
 /// Contructs a new instance based on a field
 /// </summary>
 /// <param name="typeCache"></param>
 /// <param name="fieldNameConvention"></param>
 /// <param name="info"></param>
 public PropertyData(TypeDataCache typeCache, FudgeFieldNameConvention fieldNameConvention, FieldInfo info)
     : this(typeCache, fieldNameConvention, info, info.FieldType)
 {
     this.getter = ReflectionUtil.CreateGetterDelegate(info);
     this.setter = ReflectionUtil.CreateSetterDelegate(info);
 }
 public PropertyBasedSerializationSurrogateTest()
 {
     typeDataCache = new TypeDataCache(context);
 }