Advertisement
Guest User

Simple generic inheritance

a guest
May 27th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3.  
  4. namespace tacGenericTest
  5. {
  6.     [TestClass]
  7.     public class GenericTest
  8.     {
  9.         [TestMethod]
  10.         public void Test()
  11.         {
  12.             Assert.AreEqual("Person", new Person().Save());
  13.             Assert.AreEqual("Apartment", new Apartment().Save());
  14.         }
  15.     }
  16.  
  17.     class MongoEmu
  18.     {
  19.         public string Save<T>(T data)
  20.         {
  21.             return typeof(T).Name;
  22.         }
  23.  
  24.     }
  25.  
  26.     public class Repository
  27.     {
  28.         public string Save<T>(T data)
  29.         {
  30.             return new MongoEmu().Save(data);
  31.         }
  32.     }
  33.  
  34.     public class EntityBase<T>
  35.         where T : EntityBase<T>
  36.     {
  37.         public string Save()
  38.         {
  39.             return new Repository().Save((T) this);
  40.         }
  41.     }
  42.  
  43.     public class Person : EntityBase<Person>
  44.     {
  45.     }
  46.  
  47.     public class Apartment : EntityBase<Apartment>
  48.     {
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement