Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. var obj= Activator.CreateInstance(GetType());
  2.  
  3. public static Method<T>() where T : SomeBase, new()
  4.  
  5. public interface ISomething
  6. {
  7. void DoSomething();
  8. }
  9.  
  10. public class SomeClass : ISomething
  11. {
  12. public virtual void DoSomething() { Console.WriteLine("SomeClass"); }
  13. }
  14.  
  15. public class SomeDerivedClass : SomeClass
  16. {
  17. private int parameter;
  18.  
  19. public SomeDerivedClass(int parameter)
  20. {
  21. this.parameter = parameter;
  22. }
  23.  
  24. public virtual void DoSomething()
  25. {
  26. Console.WriteLine("SomeDerivedClass - {0}", parameter);
  27. base.DoSomething();
  28. }
  29. }
  30.  
  31. public interface IFactory
  32. {
  33. public ISomething Create();
  34. }
  35.  
  36. public class SomeClassFactory : IFactory
  37. {
  38. public ISomething Create() { return new SomeClass(); }
  39. }
  40.  
  41. public class SomeDerivedClassFactory : IFactory
  42. {
  43. public ISomething Create() { return new SomeDerivedClass(SomeParam); }
  44.  
  45. public int SomeParam { get; set; }
  46. }
  47.  
  48. MyClass myClass = new MyClassFactory().Create();
  49.  
  50. public abstract class CreatorOf<T> where T : CreatorOf<T>
  51. {
  52. public static T Create()
  53. {
  54. return (T)Activator.CreateInstance(typeof(T));
  55. }
  56. }
  57.  
  58. public class Inheritor : CreatorOf<Inheritor>
  59. {
  60. public Inheritor()
  61. {
  62.  
  63. }
  64. }
  65.  
  66.  
  67. public class Client
  68. {
  69.  
  70. public Client()
  71. {
  72. var obj = Inheritor.Create();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement