private void InjectDepedencyProperties(object instance) { #if NET451 PropertyInfo[] properties = objectType.GetProperties(BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) #elif DOTNET5_4 PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) #endif .Where(p => p.GetCustomAttributes(false).Any(s => s.GetType() == typeof(DepedencyAtrribute))) .ToArray(); foreach (PropertyInfo p in properties) { #if NET451 DepedencyAtrribute attr = (DepedencyAtrribute)p.GetCustomAttributes(typeof(DepedencyAtrribute), false)[0]; #elif DOTNET5_4 DepedencyAtrribute attr = p.GetCustomAttributes <DepedencyAtrribute>().First(); #endif var v = this.GetService(attr.ServiceType, attr.ServiceName, attr.ServiceVersion); if (v == null) { throw new KeyNotFoundException( String.Format( "The sepecified depedency service with a key '{0}' could not be found. Type: {1}", ServiceKey.Create(attr.ServiceType, attr.ServiceName, attr.ServiceVersion), ObjectType.AssemblyQualifiedName)); } p.SetValue(instance, v, null); } }
private object GetParameterValue(ParameterInfo pi) { Object value = null; DepedencyAtrribute[] depAttrs = (DepedencyAtrribute[])pi.GetCustomAttributes(typeof(DepedencyAtrribute), false); if (depAttrs.Length == 0) { Type type = pi.ParameterType; String name = pi.Name; String s; if (parameters.TryGetValue(name, out s)) { value = ParseValue(type, s); } else if ((pi.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault) { value = pi.DefaultValue; } else { throw new ArgumentException(pi.Name, String.Format("Parameter \"{0}\" must be provided. Type: {1}", pi.Name, ObjectType.AssemblyQualifiedName)); } } else { DepedencyAtrribute depAttr = depAttrs[0]; value = this.GetService(depAttr.ServiceType, depAttr.ServiceName, depAttr.ServiceVersion); } return(value); }