1 using System;
2
3 [assembly: Attributes.Instance(TypeName = "Classes.Something")]
4 [assembly: Attributes.Factory(TypeName = "Classes.SomethingFactory")]
5
6 namespace Attributes
7 {
8 [AttributeUsage(AttributeTargets.Assembly)]
9 class InstanceAttribute : Attribute
10 {
11 private string typeName;
12
13 public string TypeName
14 {
15 get { return typeName; }
16 set { this.typeName = value; }
17 }
18 }
19
20 [AttributeUsage(AttributeTargets.Assembly)]
21 class FactoryAttribute : Attribute
22 {
23 private string typeName;
24
25 public string TypeName
26 {
27 get { return typeName; }
28 set { this.typeName = value; }
29 }
30 }
31 }
32
33 namespace Interfaces
34 {
35 interface ISomething
36 {
37 void Do();
38 }
39
40 interface ISomethingFactory
41 {
42 ISomething GetSomething(Type t);
43 ISomething GetSomething(string typeName);
44 }
45 }
46
47 namespace Classes
48 {
49 class SomethingFactory : MarshalByRefObject, Interfaces.ISomethingFactory
50 {
51 public SomethingFactory() {}
52
53 public Interfaces.ISomething GetSomething(Type t)
54 {
55 if (t != null)
56 {
57 return GetSomething(t.FullName);
58 }
59
60 return null;
61 }
62
63 public Interfaces.ISomething GetSomething(string typeName)
64 {
65 try
66 {
67 return (Interfaces.ISomething) Activator.CreateInstance(Type.GetType(typeName));
68 }
69 catch {}
70
71 return null;
72 }
73 }
74
75 class Something : MarshalByRefObject, Interfaces.ISomething
76 {
77 public void Do()
78 {
79 Console.WriteLine("Aren't factories fun? ... A bit of an overkill, I know!");
80 }
81 }
82 }
83
84 namespace AppDomains
85 {
86 class Domain
87 {
88 public static Interfaces.ISomethingFactory Factory()
89 {
90 System.Reflection.Assembly loadingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
91 Attributes.FactoryAttribute factoryAttribute = (Attributes.FactoryAttribute) Attribute.GetCustomAttribute(loadingAssembly, typeof(Attributes.FactoryAttribute));
92
93 AppDomain domain = AppDomain.CreateDomain("TempDomain");
94 Interfaces.ISomethingFactory factory = (Interfaces.ISomethingFactory) domain.CreateInstanceAndUnwrap(loadingAssembly.FullName, factoryAttribute.TypeName);
95
96 return factory;
97 }
98
99 public static Interfaces.ISomething Instance(Interfaces.ISomethingFactory factory)
100 {
101 System.Reflection.Assembly loadingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
102 Attributes.InstanceAttribute instanceAttribute = (Attributes.InstanceAttribute) Attribute.GetCustomAttribute(loadingAssembly, typeof(Attributes.InstanceAttribute));
103
104 Type instanceType = Type.GetType(instanceAttribute.TypeName);
105 Interfaces.ISomething instance = factory.GetSomething(instanceType);
106
107 return instance;
108 }
109 }
110 }
111
112 namespace Client
113 {
114 class FactoryChant
115 {
116 [STAThread]
117 static void Main(string[] args)
118 {
119 Interfaces.ISomethingFactory factory = AppDomains.Domain.Factory();
120 Interfaces.ISomething something = AppDomains.Domain.Instance(factory);
121
122 something.Do();
123
124 Console.ReadLine();
125 }
126 }
127 }