private static void LoadSimpleValue(ICsvConfigurable target, FieldInfo fieldInfo, Values values, string propertyName) { var value = values.Get(propertyName, string.Empty); var fieldValue = default(object); var type = fieldInfo.FieldType; if (value == string.Empty && type.IsValueType) { fieldValue = Activator.CreateInstance(type); } else if (type.IsEnum) { fieldValue = Enum.Parse(type, value, true); } else if (fieldInfo.FieldType.IsEnum) { fieldValue = Enum.Parse(fieldInfo.FieldType, value); } else { fieldValue = Convert.ChangeType(value, type, CultureInfo.InvariantCulture); } fieldInfo.SetValue(target, fieldValue); }
private static void LoadUnityTypeValue(ICsvConfigurable target, FieldInfo fieldInfo, Values values, string propertyName) { var unityObjectInstance = values.FindUnityAsset(propertyName, fieldInfo.FieldType); fieldInfo.SetValue(target, unityObjectInstance); }
private static bool ParseListObjectField(ICsvConfigurable target, string name, FieldInfo fieldInfo, Values values) { var fieldType = fieldInfo.FieldType; if (!fieldType.IsGenericType || (fieldType.GetGenericTypeDefinition() != typeof(List <>))) { return(false); } var genericType = fieldType.GetGenericArguments().FirstOrDefault(); var isAssetGenericType = unityTypes.Any(x => x.IsAssignableFrom(genericType)); if (!isAssetGenericType) { return(false); } var list = Activator.CreateInstance(fieldType) as IList; var assets = values.GetScriptableObjects(genericType, name); if (assets == null || list == null) { return(false); } foreach (var asset in assets) { if (asset) { list.Add(asset); } } fieldInfo.SetValue(target, list); return(true); }
private static void ParseObjectFieldsAndProperties(ICsvConfigurable target, Values values) { var type = target.GetType(); var fields = type.GetFields(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance); foreach (var each in fields) { ParseObjectField(target, each, values); } }
private static void ParseObjectField(ICsvConfigurable target, FieldInfo fieldInfo, Values values) { var attribute = fieldInfo.GetCustomAttributes(typeof(RemotePropertyAttribute), inherit: false).OfType <RemotePropertyAttribute>().FirstOrDefault(); if (attribute != null) { var value = values.Get <string>(attribute.PropertyName, string.Empty); var fieldValue = Convert.ChangeType(value, fieldInfo.FieldType); fieldInfo.SetValue(target, fieldValue); } }
private static void ParseObjectField(ICsvConfigurable target, FieldInfo fieldInfo, Values values) { var propertyName = GetPropertyName(fieldInfo); if (propertyName != null) { var fieldType = fieldInfo.FieldType; if (ParseListObjectField(target, propertyName, fieldInfo, values)) { return; } if (unityTypes.Any(_ => fieldType.IsSubclassOf(_))) { LoadUnityTypeValue(target, fieldInfo, values, propertyName); } else { LoadSimpleValue(target, fieldInfo, values, propertyName); } } }
private static void ProcessObject(ICsvConfigurable target, Values values) { ParseObjectFieldsAndProperties(target, values); target.Configure(values); }