繁体   English   中英

如何通过反射获取数组中项目的值

[英]How Can I Get The Value of an Item in an Array via Reflection

跳到第三段询问问题。

上下文:我正在创建2D太空飞船游戏,其中一种游戏机制是将功率路由到飞船的7个不同部分,以适应您当前所处的情况。您可以随时切换到一些预设,称为电源配置。 我将电源配置存储在3个不同的阵列中,如下所示:

int[] powerConfiguration1 = new int[7] {10,10,10,10,20,20,20};
int[] powerConfiguration2 = new int[7] {20,20,20,10,10,10,10};
int[] powerConfiguration3 = new int[7] {10,20,10,20,10,20,10};

切换配置时,它会调用一个方法。 该方法进行一些计算以确定切换配置将花费多长时间。 但是,我不想在播放器切换到三种配置中的任何一种的情况下多次制作switch语句并复制/粘贴代码,而是要在System.Reflections中使用PropertyInfo来选择需要从中提取值的属性。

问题:问题是我不知道如何从数组中获取项目。 到目前为止,这就是我要确定的位置,总共需要重新布线多少功率,然后将其全部添加到变量中。 0是我决定存储屏蔽功率的配置中的索引。 powerToShields是当前路由到屏蔽的电源。

void switchConfiguration(int number) {
PropertyInfo powerConfiguration = GetType().GetProperty("powerConfiguration" + number);
int powerToReroute = 0;
powerToReroute += Mathf.Abs(powerToShields - powerConfiguration[0]);

有人可以解释我做错了什么,和/或告诉我如何解决? 还是有更好的方法来做到这一点?

编辑1:这是用C#(统一)编码的。

侧面想

我想我的第一个问题是为什么不将数组存储在列表中。 因此,为什么不只存储int []的列表,而不是powerConfiguration1,powerConfiguration2,powerConfiguration3,所以

List<int[]> powerConfigurationList = new List<int[]>;
powerConfigurationList.Add(new int[7] {10,10,10,10,20,20,20});
powerConfigurationList.Add(new int[7] {20,20,20,10,10,10,10});
powerConfigurationList.Add(new int[7] {10,20,10,20,10,20,10});

这样,您可以通过以下方式获取商品:

powerToReroute = (powerConfigurationList[number])[0]

回答你的问题

但是,假设您有某些充分的理由不能这样做,并且为了回答您的确切问题,请执行以下操作:

...
PropertyInfo powerConfiguration = GetType().GetProperty("powerConfiguration" + number); //this line is taken from your example above

//then you need to do something like the below
var value = (int[])powerConfiguration.GetValue(instanceThatHasTheProperty);
int powerToReroute = 0;
powerToReroute += Mathf.Abs(powerToShields - value[0]);

从您的代码片段中,我看到您具有GetType().GetProperty("powerConfiguration" + number); 我不确定从中获取该类型的实际实例是什么。 所以,你需要更换instanceThatHasTheProperty在我上面的代码片段,以任何情况下,你正在试图获得来自属性的值。

对我而言,立即显而易见的是,代码被不必要地混淆了,看起来很像XY问题。 使用锯齿状数组和反射对于结束面向对象的编程似乎是很多工作。 我的建议是创建一个类来存储您的电源配置,在列表中存储多个电源配置,然后从列表中选择配置。

电源配置的样本类别

public class PowerConfiguration
{
    public int ID { get; set; }

    public string Name { get; set; }

    public int Shields { get; set; }

    public int Weapons { get; set; }

    public int LifeSupport { get; set; }

    public int Propulsion { get; set; }
}

插入和访问您的电源配置

public class DoStuff
{
    public void LoadPowerConfiguration()
    {
        // Create a List to store configurations
        List<PowerConfiguration> allPowerConfigurations = new List<PowerConfiguration>();

        // Add some mock data to the list
        allPowerConfigurations.Add(new PowerConfiguration()
        {
            ID = 0,
            Name = "Balanced Combat",
            Shields = 30,
            Weapons =  30,
            LifeSupport = 20,
            Propulsion = 20
        });

        allPowerConfigurations.Add(new PowerConfiguration()
        {
            ID = 1,
            Name = "Offensive",
            Shields = 20,
            Weapons = 50,
            LifeSupport = 10,
            Propulsion = 20
        });

        // Figure out which ID you what (eg. from the user pressing '0')
        int selectedConfigurationID = 0;

        // Get the configuration from the list
        PowerConfiguration selectedConfiguration =
            allPowerConfigurations.FirstOrDefault(p => p.ID == selectedConfigurationID);

        // Now perform your operations against the PowerConfiguration object's properties
        int powerToShields = 100;
        int powerToReroute = 0;

        powerToReroute += Math.Abs(powerToShields - selectedConfiguration.Shields);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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