Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using ConsoleApplication1;
  8.  
  9. namespace ConsoleApplication1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Persistable c = new PersistableObjects.Customer(1, "C");
  16.             Persistable e = new PersistableObjects.Employee();
  17.             Persistence.PrintPersistedObjects();
  18.             Console.ReadLine();
  19.         }
  20.     }
  21.  
  22.     interface Persistable
  23.     {
  24.         object GetID();
  25.     }
  26.  
  27.     static class Persistence
  28.     {
  29.         public static Type[] persistableEntities = AppDomain.CurrentDomain.GetAssemblies()
  30.                        .SelectMany(t => t.GetTypes())
  31.                        .Where(t => t.IsClass && t.Namespace == "PersistableObjects").ToArray();
  32.  
  33.         static Dictionary<Type, Dictionary<object, Persistable>> topkek = new Dictionary<Type,Dictionary<object, Persistable>>();
  34.  
  35.         static Persistence()
  36.         {
  37.             foreach (Type t in persistableEntities)
  38.             {
  39.                 topkek.Add(t, new Dictionary<object, Persistable>());
  40.             }
  41.         }
  42.  
  43.         public static void Persist(this Persistable persistable)
  44.         {
  45.             topkek[persistable.GetType()].Add(persistable.GetID(), persistable);
  46.         }
  47.  
  48.         public static void PrintPersistedObjects()
  49.         {
  50.             foreach (var v in topkek.Values)
  51.             {
  52.                 foreach (Persistable p in v.Values)
  53.                 {
  54.                     //Console.WriteLine(p.GetType());
  55.                     if (persistableEntities.Contains(p.GetType()))
  56.                     {
  57.                         foreach (FieldInfo f in p.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
  58.                         {
  59.                             object fieldValue;
  60.  
  61.                             if (persistableEntities.Contains(f.FieldType))
  62.                             {
  63.                                 fieldValue = ((Persistable)f.GetValue(p)).GetID();
  64.                                 Console.WriteLine(fieldValue);
  65.                             }
  66.                             else
  67.                             {
  68.                                 fieldValue = f.GetValue(p);
  69.                                 Console.WriteLine(fieldValue);
  70.                             }
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. namespace PersistableObjects
  80. {
  81.     class Customer : Persistable
  82.     {
  83.         private int id;
  84.         private string name;
  85.  
  86.         public Customer(int id, string name)
  87.         {
  88.             this.id = id;
  89.             this.name = name;
  90.             this.Persist();
  91.         }
  92.  
  93.         public object GetID()
  94.         {
  95.             return id;
  96.         }
  97.     }
  98.  
  99.     class Employee : Persistable
  100.     {
  101.         private int id = 2;
  102.         private string name = "E";
  103.         private Customer c;
  104.  
  105.         public Employee()
  106.         {
  107.             c = new Customer(3, "FU");
  108.             this.Persist();
  109.         }
  110.  
  111.         public object GetID()
  112.         {
  113.             return id;
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement