public static void AccessAbcTwoTimesEach(IServiceSource source) { Console.WriteLine("Excercise " + source.GetType().Name); Console.WriteLine("1st pass - Get an instance of A, B, and C through the source and access the DoSomething for each."); source.GetService <A>().DoSomething(); source.GetService <B>().DoSomething(); source.GetService <C>().DoSomething(); Console.WriteLine(); Console.WriteLine("2nd pass - Get an instance of A, B, and C through the source and access the DoSomething for each."); source.GetService <A>().DoSomething(); source.GetService <B>().DoSomething(); source.GetService <C>().DoSomething(); Console.WriteLine(); var clock = Stopwatch.StartNew(); for (int i = 0; i < _iterations; i++) { source.GetService <A>(); source.GetService <B>(); source.GetService <C>(); } clock.Stop(); Console.WriteLine("Accessed A, B, and C " + _iterations + " times each in " + clock.ElapsedMilliseconds + "ms through " + source.GetType().Name + "."); Console.WriteLine(); Console.WriteLine(); }
public static IServiceSource[] MakeSources() { IServiceSource[] iss = new IServiceSource[_SourceTypes.Length]; //Assembly ass=Assembly.Load("RestSharpTest"); int i = 0; foreach (var item in _SourceTypes) { IServiceSource v = Activator.CreateInstance(Type.GetType("RestSharpTest.Controllers.ReflectionInjection." + item)) as IServiceSource; iss[i] = v; i++; } return(iss); }
public static void ShowSource(string SourceType) { Type type = Type.GetType("RestSharpTest.Controllers.ReflectionInjection.ClientClass"); ClientClass obj = Activator.CreateInstance(type) as ClientClass; IServiceSource Source = MakeSource(SourceType); obj.Set_ServiceImpl(Source); obj.ShowInfo(); //---------------------ClientClass 可以继承接口 object[] pms = new object[] {}; ConstructorInfo[] infoArray1 = type.GetConstructors(); foreach (ConstructorInfo info in infoArray1) { if (!info.IsStatic) { ParameterInfo[] parameterInfos = info.GetParameters(); pms = new object[parameterInfos.Length]; if (parameterInfos.Length > 0) { for (int i = 0; i < parameterInfos.Length; i++) { pms[i] = null; //可以外部维护字典key:IServiceSource value:Source 实现注入 if (parameterInfos[i].ParameterType == typeof(IServiceSource)) { pms[i] = Source; } } } } } ClientClass obj1 = Activator.CreateInstance(type, pms) as ClientClass; obj.Set_ServiceImpl(Source); obj.ShowInfo(); }
//客户类中的方法,初始化注入点 public void Set_ServiceImpl(IServiceSource serviceImpl) { this._serviceImpl = serviceImpl; }
public ClientClass(IServiceSource serviceImpl) { this._serviceImpl = serviceImpl; Console.WriteLine("注入点" + serviceImpl.ShowInfo()); }
public CollectionBasedServiceCache(IServiceSource source) { _serviceDictionary = new Dictionary <Type, IService>(); _source = source; }
public GenericTypeServiceCache(IServiceSource source) { _source = source; }