//http://patriksvensson.se/2013/08/how-to-dynamically-add-attributes-to-a-class-with-castle-core/
        public static Type CreateTypeDecoratedWithAttribute(Type classType)
        {
            if (classType == null || !classType.IsClass || classType.IsSealed)
            {
                throw new InvalidOperationException("Invalid type");
            }
            lock (MetaTypeCache)
            {
                Type typeProxified = null;
                if (!MetaTypeCache.TryGetValue(classType, out typeProxified))
                {
                    // Create the proxy generation options.
                    // This is how we tell Castle.DynamicProxy how to create the attribute.
                    var proxyOptions = new ProxyGenerationOptions();
                    var attrTypes    = new [] { typeof(ProtoBuf.ProtoContractAttribute), typeof(DataContractAttribute) };

                    foreach (var attrType in attrTypes)
                    {
                        if (!classType.CustomAttributes.Any(e => e.AttributeType == attrType))
                        {
                            // Get the attribute constructor.
                            Type[] ctorTypes = Type.EmptyTypes;
                            var    ctor      = attrType.GetConstructor(ctorTypes);
                            Debug.Assert(ctor != null, "Could not get constructor for attribute.");

                            // Create an attribute builder.
                            object[] attributeArguments = new object[] { };
                            //var builder = new CustomAttributeBuilder(ctor, attributeArguments);
                            var builder = new CustomAttributeInfo(ctor, attributeArguments);
                            proxyOptions.AdditionalAttributes.Add(builder);
                        }
                    }

                    // Create the proxy generator.
                    var proxyGenerator = new ProxyGenerator();
                    //proxyGenerator.ProxyBuilder.CreateClassProxyType()
                    var newGenerator = new ProxyGeneratorAttributePropertyTest(proxyGenerator.ProxyBuilder.ModuleScope, classType)
                    {
                        Logger = proxyGenerator.ProxyBuilder.Logger
                    };
                    MetaTypeCache.Add(classType, typeProxified = newGenerator.GenerateCode(Type.EmptyTypes, proxyOptions));
                }
                return(typeProxified);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var testInstance = ProxyGeneratorAttributePropertyTest.CreateClassDecoratedWithAttribute <CustomerFlat>();
            var serialiced   = FakeCustomerDataRepository.New(0).SerializeXML().Replace("Customer", testInstance.GetType().Name);

            testInstance = (CustomerFlat)serialiced.DeserializeXML(testInstance.GetType());//to use proxy type
            var asd1 = testInstance.SerializePB();
            var asd2 = testInstance.SerializeXML();
            var asd3 = testInstance.SerializeJSON();
            //var asd4 = testInstance.SerializeWCF();
            var testMetatype = PersistenceListProtoBuffer <CustomerFlat> .MetaDecorator();

            var testMetatype2 = PersistenceListProtoBuffer <Activator> .MetaDecorator();

            var testMetatype3 = PersistenceListProtoBuffer <Tuple <int, string> > .MetaDecorator();

            using (var test = new PersistenceDictionary <Guid, Customer>(new PersistenceListProtoBuffer <Customer>(compression: CompressionMode.LZ4Fast)))
            {
                const int testIndex = 333;
                var       time1     = Tools.TimeProfiler(() => test.AddRange(FakeCustomerDataRepository.GetCustomers(TotalDataToGenerate, true), e => e.Id, e => e, false));
                var       replacer  = Tools.TimeProfiler(() => test[test.GetKeyByIndex(testIndex)] = FakeCustomerDataRepository.New(-1)); //this update destroy correlation to 1
                var       time2     = Tools.TimeProfiler(() => test.All(e => true));
                //var taker = Tools.TimeProfiler(() => test.OrderBy(e => e.Value.Position).Take(100).ToArray());
                var verify1     = Tools.TimeProfiler(() => test.IndexedItems().Where(e => e.Item1 != e.Item3.Position).Count());
                var verify2     = Tools.TimeProfiler(() => test.Where(e => e.Key != e.Value.Id).Count());
                var retrieve    = Tools.TimeProfiler(() => test.GetValueByIndex(testIndex));
                var retrieveNum = Tools.TimeProfiler(() => test.GetIndexByKey(retrieve.Item2.Id));
                var time3       = Tools.TimeProfiler(() => test.Remove(retrieve.Item2.Id));
                var retrieve2   = Tools.TimeProfiler(() => test.GetItemByIndex(testIndex));
                var time4       = Tools.TimeProfiler(() => test.RemoveAt(testIndex));
                var nEntry      = FakeCustomerDataRepository.New(TotalDataToGenerate);
                var insert      = Tools.TimeProfiler(() => test.Add(nEntry.Id, nEntry));
                var verify3     = Tools.TimeProfiler(() => test.Where(e => e.Key != e.Value.Id).Count());
                var verify4     = Tools.TimeProfiler(() => test.Values.Average(e => e.Random));
                var verify5     = Tools.TimeProfiler(() => test.Average(e => e.Value.Random));
            }
            GC.Collect();

            var t0         = Stopwatch.StartNew();
            var dataBuffer = FakeCustomerDataRepository.GetCustomers(TotalDataToGenerate).ToList();

            t0.Stop();
            Console.WriteLine("data: " + t0.ElapsedMilliseconds);

            Stopwatch tX = null;
            var       t1 = Stopwatch.StartNew();

            using (var test = new PersistenceListProtoBuffer <Customer>())
            {
                using (var buffer = test.MakeBufferedAdd())
                {
                    foreach (var e in dataBuffer)
                    {
                        buffer.Add(e);
                    }
                }
                var insert = Tools.TimeProfiler(() => test.Insert(15, FakeCustomerDataRepository.New(TotalDataToGenerate)));
                tX = Stopwatch.StartNew();
                var renderList = test.ToList();
                tX.Stop();
            }
            t1.Stop();
            Console.WriteLine("buffered: " + (t1.ElapsedMilliseconds - tX.ElapsedMilliseconds));
            Console.WriteLine("renderList: " + tX.ElapsedMilliseconds);

            var t2 = Stopwatch.StartNew();

            using (var test = new PersistenceListProtoBuffer <Customer>())
            {
                test.AddRange(dataBuffer, false);
                test.Add(null);
                //var result = test.ToList();
                //Console.WriteLine(result.Count);
                int num1 = test.IndexOf(test[3]);
                int num2 = test.IndexOf(null);

                bool test1 = test.Contains(test[3]);
                bool test2 = test.Contains(null);

                var test3 = test.Where(e => e.FirstName.StartsWith("A")).Skip(1000).Take(1000).ToList();
            }
            t2.Stop();
            Console.WriteLine("chunk: " + t2.ElapsedMilliseconds);

            var t3 = Stopwatch.StartNew();

            using (var test = new PersistenceListProtoBuffer <Customer>())
            {
                test.AddRange(dataBuffer, true);
                test.Add(null);
                //var result = test.ToList();
                //Console.WriteLine(result.Count);
                int num1 = test.IndexOf(test[3]);
                int num2 = test.IndexOf(null);

                bool test1 = test.Contains(test[3]);
                bool test2 = test.Contains(null);

                var test3 = test.Where(e => e.FirstName.StartsWith("A")).Skip(1000).Take(1000).ToList();
            }
            t3.Stop();
            Console.WriteLine("size: " + t3.ElapsedMilliseconds);

            Console.WriteLine("terminado pulse tecla");
            Console.ReadKey();
        }