private static XElement[] ToXElement(AssetDefinition assetDef) { if (assetDef.Fields.Keys.Count == 0) { return new XElement[] { } } ; List <XElement> els = new List <XElement>(); foreach (var key in assetDef.Fields.Keys) { els.Add(ToXElement(key, assetDef.Fields[key])); } return(els.ToArray()); } }
public static AssetInstantiationResult CreateInstance(AssetDefinition assetDef, Type baseType) { var dependencies = new List <IAssetDependency>(); Type assetType = baseType; if (assetDef.Type != null) { assetType = ResolveType(assetDef.Type); } if (assetType == null) { return(null); } //Make sure 'T' can be assigned from the assset type if (!baseType.IsAssignableFrom(assetType)) { Engine.Log.Exception(String.Format("Can't assign type {0} from {1} in asset \"{2}\"", baseType.Name, assetType.Name, assetDef.Name)); return(null); } //Instantiate and initialize the new object if (assetType.IsArray) { Type arrayType = assetType.GetElementType(); Array array = Array.CreateInstance(arrayType, assetDef.Fields.Values.Count); int idx = 0; foreach (Object element in assetDef.Fields.Values) { var resolveResult = ResolveValue(element, arrayType); array.SetValue(resolveResult.Instance, idx++); dependencies.AddRange(resolveResult.Dependencies); } return(new AssetInstantiationResult() { Instance = array, Dependencies = dependencies }); } else { Object newObject = Activator.CreateInstance(assetType); //Set field values for the new object foreach (FieldInfo fieldInfo in assetType.GetFields()) { if (assetDef.Fields.Keys.Contains(fieldInfo.Name)) { var resolveResult = ResolveValue(assetDef.Fields[fieldInfo.Name], fieldInfo.FieldType); if (fieldInfo.FieldType.IsAssignableFrom(resolveResult.Instance.GetType())) { fieldInfo.SetValue(newObject, resolveResult.Instance); dependencies.AddRange(resolveResult.Dependencies); } else { TypeConverter conv = TypeDescriptor.GetConverter(fieldInfo.FieldType); if (conv.CanConvertFrom(resolveResult.Instance.GetType())) { object value = conv.ConvertFrom(resolveResult.Instance); fieldInfo.SetValue(newObject, value); dependencies.AddRange(resolveResult.Dependencies); } ; } } } //Set property values for the new object foreach (PropertyInfo propInfo in assetType.GetProperties()) { if (assetDef.Fields.Keys.Contains(propInfo.Name)) { var resolveResult = ResolveValue(assetDef.Fields[propInfo.Name], propInfo.PropertyType); propInfo.SetValue(newObject, resolveResult.Instance, null); if (propInfo.PropertyType.IsAssignableFrom(resolveResult.Instance.GetType())) { propInfo.SetValue(newObject, resolveResult.Instance, null); dependencies.AddRange(resolveResult.Dependencies); } else { TypeConverter conv = TypeDescriptor.GetConverter(propInfo.PropertyType); if (conv.CanConvertFrom(resolveResult.Instance.GetType())) { object value = conv.ConvertFrom(resolveResult.Instance); propInfo.SetValue(value, value, null); dependencies.AddRange(resolveResult.Dependencies); } ; } } } return(new AssetInstantiationResult() { Instance = newObject, Dependencies = dependencies }); } }