0

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.

4
  • github.com/xceedsoftware/wpftoolkit/wiki/PropertyGrid Commented Aug 23, 2018 at 20:45
  • Hey ASh, thanks for that link it looks very promising, i'll have to take a proper look, Thanks! Commented Aug 23, 2018 at 20:54
  • Well, propertygrid won't work in my situation unfortunatly. But thanks for the suggestion Commented Aug 23, 2018 at 21:21
  • I've spun up my own reflection object viewer in the past, but here is a similar approach: stackoverflow.com/questions/3668802/… Commented Aug 24, 2018 at 6:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey, thanks for the reply. I've been playing around with this and i'm run into an issue. islist always returns false and i cannot cast to List<NMSTemplate> even tho the objects in the list all inherit from NMSTemplate
@Ben how is the declaration of your collection? List<NMSTemplate> was just an example. You've to replace it with the correct type..
Ah, I assumed because they all inherit from NMSTemplate i could get away with using that. I'll need to work out a way of getting the correct type.
Ok, so someone advised me to use dynamic to solve this issue, and that worked :D thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.