public static Type GetCreateInstanceType(this Type propertyType, AutowireAttribute autowireAttr) { if (autowireAttr.Type != null) { return(autowireAttr.Type); } else { if (propertyType.IsInterface) { if (propertyType.Name.StartsWith("i", StringComparison.OrdinalIgnoreCase)) { string presumedFullName = $"{propertyType.FullName.Substring(0, propertyType.FullName.LastIndexOf(propertyType.Name))}{propertyType.Name.Substring(1)}, {propertyType.Assembly.FullName}"; Type presumedType = Type.GetType(presumedFullName); return(presumedType); } else { return(null); } } else { return(propertyType); } } }
public void GeCreateInstanceType_By_Interface_Naming_Rule() { var prop = typeof(TestClass).GetProperty(nameof(TestClass.NamedInterface)); AutowireAttribute autowireAttr = new AutowireAttribute(); var type = prop.PropertyType.GetCreateInstanceType(autowireAttr); Assert.AreEqual(typeof(NamedInterface), type); }
private object Resolve(object attr, FieldInfo field, IBuilderContext context) { Trace.WriteLine(string.Format("Resolving Autowired property - {0} of type {1}", field.Name, context.Existing.GetType())); object returnval = null; AutowireAttribute autowireAttr = attr as AutowireAttribute; var ignoreCache = context.Existing.GetType().GetCustomAttribute <IgnoreAutowireCacheAttribute>(); if (ignoreCache == null) { var interfaces = context.Existing.GetType().GetInterfaces(); foreach (var iface in interfaces) { var currentObjectCacheKey = iface.FullName + "|" + (context.BuildKey.Name ?? ""); if (!_instanceCache.ContainsKey(currentObjectCacheKey)) { _instanceCache[currentObjectCacheKey] = context.Existing; } } } string cacheKey = field.FieldType.FullName + "|" + (autowireAttr.ScopeName ?? ""); Trace.WriteLine("Cache key = {0}", cacheKey); if (_instanceCache.ContainsKey(cacheKey)) { return(_instanceCache[cacheKey]); } Trace.WriteLine("Autowired instance Not found in cache"); if (string.IsNullOrWhiteSpace(autowireAttr.ScopeName)) { try { Trace.WriteLine("Resolving in default scope"); returnval = context.NewBuildUp(new NamedTypeBuildKey(field.FieldType)); } catch (Exception) { Trace.WriteLine("Could not get concrete object"); throw; } } else { Trace.WriteLine(string.Format("Resolving in {0} scope", autowireAttr.ScopeName)); returnval = context.NewBuildUp(new NamedTypeBuildKey(field.FieldType, autowireAttr.ScopeName)); } if (returnval != null) { ignoreCache = returnval.GetType().GetCustomAttribute <IgnoreAutowireCacheAttribute>(); if (ignoreCache == null) { _instanceCache[cacheKey] = returnval; } } return(returnval); }
public ObjectInfo(AutowireAttribute autowireAttribute, Type objectType) { Parent = null; FromProperty = false; PropertyDefinedAutowireAttribute = autowireAttribute; PropertyDefinedChangePropertyAttributes = objectType.GetChangePropertyAttributes(); ObjectType = PropertyDefinedAutowireAttribute.Type; ConfirmedPropertyAttributes = ConfirmedPropertyAttributesGetter.GetConfirmedPropertyAttributes(this, null); Type = ObjectType.GetShortAssemblyName(); Singleton = PropertyDefinedAutowireAttribute.Singleton; Id = this.GetObjectId(); //PropertyDefinedAutowireAttribute = autowireAttribute; //ObjectAdoTemplateNameAttribute = objectType.GetAdoTemplateNameAttribute(); //ObjectType = ObjectTypeUtil.GetObjectType(PropertyDefinedAutowireAttribute, objectType); //ChangeAdoTemplateAttributes = new List<ChangeAdoTemplateAttribute>(); //ConfirmedPropertyAttributes = new List<PropertyAttribute>(); //Type = ObjectTypeUtil.GetShortAssemblyName(ObjectType); //Singleton = PropertyDefinedAutowireAttribute.Singleton; //Id = ObjectIdUtil.GetObjectId(this); }