Reputation: 1321
Ok i've asked a previous question on this but i think I need to take a step back and ask a different question.
What i'm trying to do here is create an editor for a field decoded from a game (a modding tool), these files are decoded by a 3rd party library and added to a class which is passed back.
Now there are over 1000 varients of this class which all share the baseclass NMSTemplate
, each has it's own unique properties which can be anything from a basic object (string, int) to a collection of other varients of NMSTemplate
.
I've tried numerous ways to do this, and my latest is something like follows
IOrderedEnumerable<FieldInfo> fields = template.GetType().GetFields().OrderBy(field => field.MetadataToken);
foreach(FieldInfo f in fields)
{
MBINField field = new MBINField()
{
Name = f.Name,
Value = f.GetValue(null),
NMSType = f.FieldType.Name
};
_fields.Add(field);
}
I then bind a listview to this field collection, and use a datatemplate to change how it's displayed
<DataTemplate x:Key="MbinListTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<ListView ItemsSource="{Binding Value}" ItemTemplateSelector="{StaticResource MbinTemplateSelector}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="MbinStringTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBox Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
Now appart from issues getting the value from FieldInfo, i realised that i've hit a pitfall when I run into a Property that is a collection of NMSTemplate
i'm not sure how i can then also handle the dynamic showing of the properties of classes in that collection.
Upvotes: 0
Views: 47
Reputation: 1261
I'm not sure if I got your question correctly but you could try to check for the field type and handle each type separately. Here's and example to handle the field of type List<NMSTemplate>
var fields = template.GetType().GetFields().OrderBy(field => field.MetadataToken);
foreach(var f in fields)
{
var isGeneric = f.FieldType.IsGenericType;
var isList = f.FieldType == typeof(List<NMSTemplate>);
if(isGeneric && isList)
{
var value = f.GetValue(template);
var list = (List<NMSTemplate>)value;
foreach(var listEntry in list)
{
// ...
}
}
}
In the foreach loop you can than create all the MBINField
objects and add them to the _fields
list.
Upvotes: 1