简体   繁体   中英

How to create array by FieldInfo c#

I has a FieldInfo, but i don't know what type is this(one of many). I save it in PortDataFieldSpecial class:

public class PortDataFieldSpecial : FieldSpecial
{
    public FieldInfo FieldInfo;
    public Port Port;

    public override VisualElement FieldVisualElement => Port;

    public PortDataFieldSpecial(NodePortAttribute nodePort, FieldInfo fieldInfo, BaseNodeView nodeView) : base(fieldInfo)
    {
        FieldInfo = fieldInfo;
        Port = BuildNode.Instance.GeneratePort(nodePort, fieldInfo, nodeView);
    }
}

This class just generate view port to my program (quest node editor) and Invoke method when value will be changed. This good work with single type data (as int, float, string and other my classes). But when i try set data as array by this method:

foreach(var port in allFieldPorts)
{
    var list = fullGraphData.GetOtherNode(port.Port, this);
    
    if(port.Port.capacity == Port.Capacity.Multi)
    {
        List<object> fieldArray = new List<object>();
        foreach(var otherNode in list)
        {
            fieldArray.Add(otherNode.GetData());
        }

        if(port.FieldInfo.FieldType.IsArray)
        {
            var array = fieldArray.ToArray();
            if(array.Length > 0)
            {
                port.FieldInfo.SetValue(node, array);
            }
        }
    }
    else
    {
        foreach(var otherNode in list)
        {
            portsFieldInfo.SetValue(node, otherNode.GetData());
        }
    }
}

On this graph:

在此处输入图像描述

Getting at exception:

ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'Game.GameEvents.GameEventConfig+RewardSettings[]'.

How i can create List of type FieldInfo.FieldType? Something like that:

List<typeof(port.FieldInfo.FieldType)> fieldList =
    new List<typeof(port.FieldInfo.FieldType)>();

Or can you tell me how to do it differently? Ps I can't rigidly set the type, since there are many such fields and not one type.

The exception tells you the name of the type. It is Game.GameEvents.GameEventConfig.RewardSettings[] , ie, and array of RewardSettings where the type RewardSettings is nested inside GameEventConfig (therefore the + in the type name, but the plus sign is IL notation, not C# notation. Replace it by '.' in C#).

So, create an array of this type and cast the data to it.

You cannot use a Type object as type parameter for a generic type. Generics are resolved at compile time. You could use Reflection to create a generic object by resolving the type parameter at runtime; however, this is rarely useful, as from then on you will have to use Reflection to work with this object.

I think the best you can do with FieldInfo is to use it an if-else statement to branch to an appropriate case if the data you are (de-)serializing is not always of the same type. But I doubt it is.

Note that you can always debug your code. Set a breakpoint, eg on the line fieldArray.Add(otherNode.GetData()); and when the execution stops at this line, add the expression otherNode.GetData() to the Quick Watch Window . It will reveal you the type of the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM