Я новичок в отражении, что было бы лучшим способом рекурсивно перебирать объекты, которые имеют общие списки и обычные массивы как поля, содержащие другие объекты?Рекурсивно перебирать список/массив
Дополнительная информация
На самом деле я создал следующий атрибут для обозначения полей, которые будут экспортированы для перевода:
using System;
[AttributeUsage(AttributeTargets.Field)]
public class ExportForTranslationAttribute : System.Attribute
{
public ExportForTranslationAttribute() { }
}
Тогда я начинаю на определенном объекте и углубиться оттуда. (теперь я скопировал весь метод.)
частный Int64 timesMaxDeepnessReached = 0;
private void searchForExportableStringsInObject(object obj, int deepness)
{
if (deepness > maxDeepness)
{
timesMaxDeepnessReached++;
return;
}
if (obj != null)
{
//only follow serializable objects!
var objAttributes = obj.GetType().GetCustomAttributes(true);
foreach (var oa in objAttributes)
{
if (oa.GetType() == typeof(SerializableAttribute))
{
var fields = obj.GetType().GetFields();
if (fields == null || !fields.Any())
{ return; }
foreach (var fieldInfo in fields)
{
//1. handle string fields that are directly on the component
#region directly on object
var attributes = fieldInfo.GetCustomAttributes(true);
bool hadExportAttribute = false;
foreach (var a in attributes)
{
if (a.GetType() == typeof(ExportForTranslationAttribute))
{
Debug.Log("found something!");
hadExportAttribute = true;
if (fieldInfo.FieldType == typeof(string))
{
try
{
Debug.Log("info.GetValue= " + fieldInfo.GetValue(obj));
addKeyWhenNotExisting((string)fieldInfo.GetValue(obj));
}
catch (Exception ex) { Debug.Log("error while getting value to export: " + ex); }
}
else if (fieldInfo.FieldType == typeof(string[]))
{
Debug.Log("found string[]!");
try
{
Debug.Log("info.GetValue= " + fieldInfo.GetValue(obj));
foreach (var item in (string[])fieldInfo.GetValue(obj))
{
addKeyWhenNotExisting(item);
}
}
catch (Exception ex) { Debug.Log("error while getting value to export: " + ex); }
}
else if (fieldInfo.FieldType == typeof(List<string>))
{
Debug.Log("found List<string>!");
try
{
Debug.Log("info.GetValue= " + fieldInfo.GetValue(obj));
foreach (var item in (List<string>)fieldInfo.GetValue(obj))
{
addKeyWhenNotExisting(item);
}
}
catch (Exception ex) { Debug.Log("error while getting value to export: " + ex); }
}
else
{
Debug.LogWarning("Can only add ExportForTranslation-Attribute to string values and collection of string values. Not on the type: " + fieldInfo.FieldType);
}
}
}
#endregion //directly on object
}
}
else if (oa.GetType() == typeof(List<>))
{
try
{
foreach (var item in (IList)oa)
{
searchForExportableStringsInObject(item, ++deepness);
}
}
catch (Exception ex) { }//{ Debug.Log("error while getting value to export: " + ex); }
}
else if (oa.GetType() == typeof(Array))
{
try
{
foreach (var item in (Array)oa)
{
searchForExportableStringsInObject(item, ++deepness);
}
}
catch (Exception ex) { }//{ Debug.Log("error while getting value to export: " + ex); }
}
}
}
else
{
return;
}
}
Прежде всего, почему вы глотаете все исключения? – CodesInChaos
Я думаю, что более конкретная справочная информация, которую вы действительно пытаетесь сделать, может быть полезна. –
+1 Я согласен, что вопрос не особенно хорошо написан, и, возможно, это не совсем понятно, требуется, но если я правильно его интерпретирую, я думаю, что у него есть какая-то заслуга. –