Esempio n. 1
0
        public override bool TryResolve(TagType type, out TagInitializationDelegate initializer)
        {
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out initializer) != null);

            initializer = null;
            return(false);
        }
Esempio n. 2
0
        public override bool TryResolve(TagType type, out TagInitializationDelegate initializer)
        {
            if (TryGetDefaultType(type, out initializer) || initializers.TryGetValue(type, out initializer))
            {
                return(true);
            }
            else
            {
                if (entryAssembly != null)
                {
                    // If there is no default resolver, try to get a resolver type from the entry assembly
                    IEnumerable <Type> tagHandlerTypes = entryAssembly.GetTypesByAttribute <TagHandlerForAttribute>(true, attr => attr.Type == type)
                                                         .Where(t => !t.IsInterface && !t.IsAbstract);
                    if (TryFindMatchingType(tagHandlerTypes, out initializer))
                    {
                        initializers.TryAdd(type, initializer);
                        return(true);
                    }

                    if (referencedAssemblies != null)
                    {
                        // If we still don't have a resolver type, do the search in all referenced assemblies for a resolver type
                        IEnumerable <Type> refdTagHandlerTypes = referencedAssemblies.SelectMany(asm => asm.GetTypesByAttribute <TagHandlerForAttribute>(true, attr => attr.Type == type))
                                                                 .Where(t => !t.IsInterface && !t.IsAbstract);
                        if (TryFindMatchingType(tagHandlerTypes, out initializer))
                        {
                            initializers.TryAdd(type, initializer);
                            return(true);
                        }
                    }
                }

                return(false);
            }
        }
Esempio n. 3
0
        private static bool TryFindMatchingType(IEnumerable <Type> tagHandlerTypes, out TagInitializationDelegate initializer)
        {
            Contract.Requires <ArgumentNullException>(tagHandlerTypes != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out initializer) != null);

            foreach (Type handlerType in tagHandlerTypes)
            {
                ConstructorInfo ci = handlerType.GetConstructor(constructorFinderArray);
                if (ci != null)
                {
                    initializer = Expression.Lambda <TagInitializationDelegate>(
                        Expression.New(ci, readerParamExpression),
                        readerParamExpression
                        ).Compile();

                    return(true);
                }
            }

            initializer = null;
            return(false);
        }
Esempio n. 4
0
        private static bool TryGetDefaultType(TagType type, out TagInitializationDelegate initializer)
        {
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out initializer) != null);

            switch (type)
            {
            case TagType.End:
                initializer = rdr => EndTag.Default;
                break;

            case TagType.Object:
                initializer = rdr => new ObjectTag(rdr);
                break;

            case TagType.Array:
                initializer = rdr => new ArrayTag(rdr);
                break;

            case TagType.List:
                initializer = rdr => new ListTag(rdr);
                break;

            case TagType.SByte:
                initializer = rdr => new SByteTag(rdr);
                break;

            case TagType.Byte:
                initializer = rdr => new ByteTag(rdr);
                break;

            case TagType.Int16:
                initializer = rdr => new Int16Tag(rdr);
                break;

            case TagType.UInt16:
                initializer = rdr => new UInt16Tag(rdr);
                break;

            case TagType.Int32:
                initializer = rdr => new Int32Tag(rdr);
                break;

            case TagType.UInt32:
                initializer = rdr => new UInt32Tag(rdr);
                break;

            case TagType.Int64:
                initializer = rdr => new Int64Tag(rdr);
                break;

            case TagType.UInt64:
                initializer = rdr => new UInt64Tag(rdr);
                break;

            case TagType.Single:
                initializer = rdr => new SingleTag(rdr);
                break;

            case TagType.Double:
                initializer = rdr => new DoubleTag(rdr);
                break;

            case TagType.String:
                initializer = rdr => new StringTag(rdr);
                break;

            case TagType.Date:
                initializer = rdr => new DateTag(rdr);
                break;

            case TagType.TimeSpan:
                initializer = rdr => new TimeSpanTag(rdr);
                break;

            case TagType.ByteVector2:
                initializer = rdr => new ByteVector2Tag(rdr);
                break;

            case TagType.ByteVector3:
                initializer = rdr => new ByteVector2Tag(rdr);
                break;

            case TagType.ByteVector4:
                initializer = rdr => new ByteVector2Tag(rdr);
                break;

            case TagType.SingleVector2:
                initializer = rdr => new SingleVector2Tag(rdr);
                break;

            case TagType.SingleVector3:
                initializer = rdr => new SingleVector3Tag(rdr);
                break;

            case TagType.SingleVector4:
                initializer = rdr => new SingleVector4Tag(rdr);
                break;

            case TagType.DoubleVector2:
                initializer = rdr => new DoubleVector2Tag(rdr);
                break;

            case TagType.DoubleVector3:
                initializer = rdr => new DoubleVector3Tag(rdr);
                break;

            case TagType.DoubleVector4:
                initializer = rdr => new DoubleVector4Tag(rdr);
                break;

            default:
                initializer = null;
                break;
            }

            return(initializer != null);
        }
Esempio n. 5
0
 public abstract bool TryResolve(TagType type, out TagInitializationDelegate initializer);