Пример #1
0
        public IPartBuilder GetPartBuilder(Type type, PartDefinition partDefinition)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            IPartBuilder result;

            if (partDefinition.ReadMethod != null || partDefinition.WriteMethod != null)
            {
                return(CustomPartBuilder.Create(type, partDefinition));
            }

            if (type.IsGenericParameter)
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (_readyParts.TryGetValue(type, out result))
            {
                return(result);
            }

            if (type.IsArray)
            {
                return(new ArrayPartBuilder(type, partDefinition, this));
            }

            if (type.IsEnum)
            {
                return(new EnumPartBuilder(type, partDefinition, this));
            }

            if (BarfFormatter.IsSerializable(type))
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (type.IsGenericType)
            {
                var genericTypeDef = type.GetGenericTypeDefinition();
                PartBuilderFactory factory;
                if (_genericParts.TryGetValue(genericTypeDef, out factory))
                {
                    return(factory(type, partDefinition));
                }
            }

            if (partDefinition.IsDynamic)
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (typeof(IVersionSerializable).IsAssignableFrom(type))
            {
                return(new VersionSerializablePartBuilder(type));
            }

            if (typeof(ICustomSerializable).IsAssignableFrom(type))
            {
                return(new CustomSerializablePartBuilder(type));
            }

            var builder = SerializableStructPartBuilder.TryCreate(type, partDefinition, this);

            if (builder != null)
            {
                return(builder);
            }

            if (type.IsSerializable && !partDefinition.IsBaseType)
            {
                return(BinaryFormatterPartBuilder.Instance);
            }

            throw new NotSupportedException("The defined member cannot be serialized because it and/or one if its nested types are not serializable - " + partDefinition);
        }
Пример #2
0
        /// <summary>
        /// Instruments the specified assembly definition.
        /// </summary>
        /// <param name="assemblyDefinition">The assembly definition.</param>
        public void Instrument(AssemblyDefinition assemblyDefinition)
        {
            int eligibleTypeCount  = 0;
            int validTypeCount     = 0;
            var reflectionAssembly = GetAssembly(assemblyDefinition.Name.FullName);

            foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules)
            {
                Module module = null;

                foreach (var mod in reflectionAssembly.GetModules(false))
                {
                    if (moduleDefinition.Name == moduleDefinition.Name)
                    {
                        module = mod;
                    }
                }

                if (module == null)
                {
                    throw new InvalidOperationException("Couldn't resolve module: " + moduleDefinition.Name);
                }

                try
                {
                    _context = new InstrumentationContext(moduleDefinition);

                    foreach (Type type in module.GetTypes())
                    {
                        if (!type.IsClass)
                        {
                            continue;
                        }

                        if (!BarfFormatter.IsSerializable(type))
                        {
                            continue;
                        }

                        var attribute = Attribute
                                        .GetCustomAttributes(type, typeof(SerializableClassAttribute), false)
                                        .OfType <SerializableClassAttribute>()
                                        .FirstOrDefault <SerializableClassAttribute>();

                        if (attribute.RuntimeOnly)
                        {
                            continue;
                        }

                        var barfTypeDef = BarfTypeDefinition.Get(type);

                        if (barfTypeDef == null)
                        {
                            // todo - remove once everything is supported
                            continue;
                        }

                        ++eligibleTypeCount;

                        if (type.IsGenericType)
                        {
                            continue;
                        }

                        Trace.WriteLine("Instrumenting - " + type.Name);

                        using (_context.OpenType(barfTypeDef))
                        {
                            _context.BarfSerializer.CustomAttributes.Add(new CustomAttribute(_context.Import(typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes))));
                            _context.TypeDefinition.NestedTypes.Add(_context.BarfSerializer);
                            _context.TypeDefinition.Module.Types.Add(_context.BarfSerializer);

                            GenerateDefaultCtor(_context.BarfSerializer, _context.BaseBarfSerializer);
                            GenerateCreateEmptyMethod();
                            GenerateSerializeMethod();
                            GenerateInnerDeserializeMethod();

                            _context.BarfTester.CustomAttributes.Add(new CustomAttribute(_context.Import(typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes))));
                            _context.BarfTester.Interfaces.Add(_context.BarfTesterInterface);
                            _context.TypeDefinition.NestedTypes.Add(_context.BarfTester);
                            _context.TypeDefinition.Module.Types.Add(_context.BarfTester);

                            GenerateDefaultCtor(_context.BarfTester, typeof(object));

                            GenerateFillMethod();
                            GenerateAssertAreEqualMethod();


                            ++validTypeCount;
                        }
                    }
                }
                finally
                {
                    _context = null;
                }
            }
            Trace.TraceInformation("{0} - {1} of {2} eligible types were generated in {3}", GetType().Name, validTypeCount, eligibleTypeCount, assemblyDefinition.Name.Name);
        }