private void SetApisAndContainer <T>(object instance, T container, IocMap map) { // needs to use GetPalType(); to get pal type instead. This will help eliminate the needs for direct // connection with the .NET / Core / Mono runtime. var type = instance.GetType(); var properties = type.GetProperties(); foreach (var property in properties) { SetContainer(property, instance, container, map); } }
private void SetContainer <T>(PropertyInfo property, object currentInstance, T container, IocMap map) { object apiInstance = null; if (PropertyExtendsApi(property)) { apiInstance = property.GetValue(currentInstance); if (apiInstance == null) { //try //{ apiInstance = XCommonAppPal.Api.Activation.CreateInstance(property.PropertyType); if (property.CanWrite) { property.SetValue(currentInstance, apiInstance); } //} //catch //{ // ignore //} } } else if (IsNamedAnApi(property.PropertyType.Name)) { apiInstance = property.GetValue(currentInstance); if (apiInstance == null) { var type = property.PropertyType; if (type.IsGenericType) { var typeDefintion = type.GetGenericTypeDefinition(); if (type.GenericTypeArguments.Length == 1) { var fullName = GetName(typeDefintion);//.FullName ?? typeDefintion.Namespace + "." + typeDefintion.Name); if (map.InterfaceNodes.TryGetValue(fullName, out InterfaceNode interfaceNode)) { var classes = interfaceNode.Classes; if (classes.Count == 1) { var classType = classes.Values.SingleOrDefault(); if (classType == null) { throw new Exception(); } //try //{ var result = XCommonAppPal.Api.Runtimic.Execution.Metadata.Assemblies.MakeGenericType(classType, new Type[] { container.GetType() }); apiInstance = XCommonAppPal.Api.Activation.CreateInstance(result); if (property.CanWrite) { property.SetValue(currentInstance, apiInstance); } //} //catch (System.Exception) //{ //} } else { throw new Exception("Multiple classes found."); } } } else { } } else { if (type.IsClass) { apiInstance = XCommonAppPal.Api.Activation.CreateInstance(type); if (property.CanWrite) { property.SetValue(currentInstance, apiInstance); } } else if (type.IsInterface) { var fullName = GetName(type);//.FullName ?? type.Namespace + "." + type.Name); if (map.InterfaceNodes.TryGetValue(fullName, out InterfaceNode interfaceNode)) { var classes = interfaceNode.Classes; if (classes.Count == 1) { var classType = classes.Values.SingleOrDefault(); if (classType == null) { throw new Exception(); } //try //{ var result = XCommonAppPal.Api.Runtimic.Execution.Metadata.Assemblies.MakeGenericType(classType, new Type[] { container.GetType() }); apiInstance = XCommonAppPal.Api.Activation.CreateInstance(result); if (property.CanWrite) { property.SetValue(currentInstance, apiInstance); } //} //catch (System.Exception) //{ //} } else { throw new Exception("Multiple classes found."); } } } } } } if (TypeExtends(property.PropertyType, typeof(Api <,>))) { var undelerlyingProperty = property.PropertyType.GetProperty("Underlying"); SetContainer(undelerlyingProperty, apiInstance, container, map); } if (apiInstance != null) { var apiInstanceType = apiInstance.GetType(); var containerProperty = apiInstanceType.GetProperty("Container"); if (containerProperty != null && containerProperty.CanWrite) { containerProperty.SetMethod.Invoke(apiInstance, new object[] { container }); } SetApisAndContainer(apiInstance, container, map); } }