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);
        }
        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);
        }
示例#4
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);
        }
        /// <summary>
        /// Creates a surrogate for a given type.
        /// </summary>
        /// <param name="type">Type for which to get surrogate.</param>
        /// <param name="fieldNameConvention">Convention for mapping .net property names to serialized field names.</param>
        /// <returns>Surrogate for the type.</returns>
        /// <exception cref="FudgeRuntimeException">Thrown if no surrogate can be automatically created.</exception>
        public virtual IFudgeSerializationSurrogate GetSurrogate(Type type, FudgeFieldNameConvention fieldNameConvention)
        {
            var typeData = typeDataCache.GetTypeData(type, fieldNameConvention);

            foreach (var selector in selectors)
            {
                IFudgeSerializationSurrogate surrogate = selector(context, typeData);
                if (surrogate != null)
                {
                    return(surrogate);
                }
            }

            throw new FudgeRuntimeException("Cannot automatically determine surrogate for type " + type.FullName);
        }
示例#6
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;
                }
            }
示例#7
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));
 }