C# 从GetValue获取int[]
我试图从方法GetValue(this,null)获取一个int数组,但我只获取System.Int32[]。有什么想法吗? 我希望得到consequences2的值作为输出(当函数被textNum=2调用时),它是[3,4,5],但我只得到“System.Int32[]”。 我需要在运行时获取值,因为我正在开发的应用程序是一个测试游戏,可能有500多个这样的阵列C# 从GetValue获取int[],c#,arrays,unity3d,C#,Arrays,Unity3d,我试图从方法GetValue(this,null)获取一个int数组,但我只获取System.Int32[]。有什么想法吗? 我希望得到consequences2的值作为输出(当函数被textNum=2调用时),它是[3,4,5],但我只得到“System.Int32[]”。 我需要在运行时获取值,因为我正在开发的应用程序是一个测试游戏,可能有500多个这样的阵列 using System.IO; using UnityEngine; using System; using System.Ref
using System.IO;
using UnityEngine;
using System;
using System.Reflection;
using System.Linq;
public class DialogueManager : MonoBehaviour
{
//999 IS RETURN TO MENU
//1000 IS CLOSE GAME
public static DialogueManager instance = null;
Dialogues dial;
public string text1 { get; set; }
public string choices1 { get; set; }
public int[] consequences1 { get; set; }
public string text2 { get; set; }
public string choices2 { get; set; }
public int[] consequences2 { get; set; }
public string text3 { get; set; }
public string choices3 { get; set; }
public int[] consequences3 { get; set; }
public string text4 { get; set; }
public string choices4 { get; set; }
public int[] consequences4 { get; set; }
public string text5 { get; set; }
public string choices5 { get; set; }
public int[] consequences5 { get; set; }
string fixedName;
string[] choices;
string text;
string[] consequences;
private void Awake()
{
if(instance == null)
{
instance = this;
}
dial = new Dialogues();
StreamReader sR = new StreamReader(Application.dataPath + "/GameData/dialogues.json");
string json = sR.ReadToEnd();
dial = JsonUtility.FromJson<Dialogues>(json);
text1 = dial.text1;
choices1 = dial.choices1;
consequences1 = dial.consequences1;
text2 = dial.text2;
choices2 = dial.choices2;
consequences2 = dial.consequences2;
text3 = dial.text3;
choices3 = dial.choices3;
consequences3 = dial.consequences3;
text4 = dial.text4;
choices4 = dial.choices4;
consequences4 = dial.consequences4;
text5 = dial.text5;
choices5 = dial.choices5;
consequences5 = dial.consequences5;
}
public string GetText(int currentText)
{
fixedName = "text" + currentText;
string output = typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null).ToString();
Debug.Log(output);
return output;
}
public string GetTextChoices(int textNum)
{
fixedName = "choices" + textNum;
string output = typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null).ToString();
if (output == "System.String[]")
{
output = null;
}
return output;
}
public int[] GetChoicesConsequences(int textNum)
{
fixedName = "consequences" + textNum;
int[] values = (int[])((dynamic)this).fixedName;
return values;
}
}
public class Dialogues
{
public string text1;
public string choices1;
public int[] consequences1;
public string text2;
public string choices2;
public int[] consequences2;
public string text3;
public string choices3;
public int[] consequences3;
public string text4;
public string choices4;
public int[] consequences4;
public string text5;
public string choices5;
public int[] consequences5;
}
使用System.IO;
使用UnityEngine;
使用制度;
运用系统反思;
使用System.Linq;
公共类DialogueManager:MonoBehavior
{
//999返回菜单
//1000是一场势均力敌的比赛
公共静态DialogueManager实例=null;
对话拨号;
公共字符串text1{get;set;}
公共字符串选项1{get;set;}
public int[]consequences1{get;set;}
公共字符串text2{get;set;}
公共字符串选项2{get;set;}
public int[]consequences2{get;set;}
公共字符串text3{get;set;}
公共字符串选项3{get;set;}
public int[]consequences3{get;set;}
公共字符串text4{get;set;}
公共字符串选项4{get;set;}
public int[]consequences4{get;set;}
公共字符串text5{get;set;}
公共字符串选项5{get;set;}
public int[]consequences5{get;set;}
字符串fixedName;
字符串[]选项;
字符串文本;
字符串[]结果;
私人空间
{
if(实例==null)
{
实例=此;
}
拨号=新对话();
StreamReader sR=新的StreamReader(Application.dataPath+“/GameData/dialogs.json”);
字符串json=sR.ReadToEnd();
dial=JsonUtility.FromJson(json);
text1=dial.text1;
choices1=拨号选择1;
consequences1=拨号。consequences1;
text2=dial.text2;
choices2=拨号选择2;
consequences2=拨号。consequences2;
text3=dial.text3;
choices3=拨号选择3;
consequences3=拨号。consequences3;
text4=拨号。text4;
choices4=拨号选择4;
consequences4=拨号。consequences4;
text5=拨号。text5;
choices5=拨号。choices5;
consequences5=拨号。consequences5;
}
公共字符串GetText(int currentText)
{
fixedName=“text”+当前文本;
字符串输出=typeof(DialogueManager).GetProperty(fixedName).GetValue(this,null).ToString();
Debug.Log(输出);
返回输出;
}
公共字符串GetTextChoices(int-textNum)
{
fixedName=“choices”+textNum;
字符串输出=typeof(DialogueManager).GetProperty(fixedName).GetValue(this,null).ToString();
如果(输出==“System.String[]”)
{
输出=空;
}
返回输出;
}
public int[]getChoicesResultations(int textNum)
{
fixedName=“后果”+textNum;
int[]值=(int[])((动态)this.fixedName;
返回值;
}
}
公开课对话
{
公共字符串text1;
公共弦乐1;
公共int[]后果1;
公共字符串text2;
公共弦乐2;
公共int[]后果2;
公共字符串text3;
公共弦乐3;
公共int[]后果3;
公共字符串text4;
公共弦乐4;
公共int[]后果4;
公共字符串text5;
公共弦乐5;
公共int[]后果5;
}
而不是
int[] values = (int[]) typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null);
你可以尝试使用
int[] values = (int[]) ((dynamic) this).fixedName;
这应该有效,因为您的fixedname
属性是公共实例属性
或者,更好的是,重新思考为什么需要在运行时确定属性名称?一个简单的
if(textNum == 1) return this.consequences1;
else if(textNum == 2) return this.consequences2;
// ...
不是吗
编辑 我只是重读了你的问题。您的问题是
Console.WriteLine
在打印实际数组值时打印System.Int32[]
?如果是这样,你可以使用
foreach(int v in values) // where values is your int[]
{
Console.WriteLine(v);
}
Console.WriteLine("[" + String.Join(",", values) + "]");
产生
或者你可以用
foreach(int v in values) // where values is your int[]
{
Console.WriteLine(v);
}
Console.WriteLine("[" + String.Join(",", values) + "]");
产生
[1,2,3]
编辑2 下面我写了一些代码,我认为这些代码可以完成您的任务。主要逻辑发生在
GetPropertyValueSafe
方法中,该方法负责检查请求的属性是否存在,如果存在,则获取指定类型的属性值
public class DialogueManager
{
private static IReadOnlyCollection<PropertyInfo> AllProperties = typeof(DialogueManager).GetProperties();
private T GetPropertyValueSafe<T>(string propertyName)
{
PropertyInfo thePropertyInfo = DialogueManager.AllProperties.SingleOrDefault(x => x.Name == propertyName);
if (thePropertyInfo is null)
{
throw new InvalidOperationException($"Type {this.GetType()} has no property {propertyName}.");
}
return (T) thePropertyInfo.GetValue(this);
}
private string ArrayToString<T>(T[] array)
{
return String.Join(", ", array);
}
public string GetText(int currentText)
{
return this.GetPropertyValueSafe<string>("text" + currentText);
}
public string GetTextChoices(int textNum)
{
return this.GetPropertyValueSafe<string>("choices" + textNum);
}
public int[] GetChoicesConsequences(int textNum)
{
return this.GetPropertyValueSafe<int[]>("consequences" + textNum);
}
public string text1 { get; set; } = "Text 1";
public string choices1 { get; set; } = "Choice 1";
public int[] consequences1 { get; set; } = new int[] { 1, 2 };
public string text2 { get; set; } = "Text 2";
public string choices2 { get; set; } = "Choice 2";
public int[] consequences2 { get; set; } = new int[] { 1, 2, 3 };
public static void Main(string[] args)
{
DialogueManager d = new DialogueManager();
Console.WriteLine(d.ArrayToString(d.GetChoicesConsequences(1)));
Console.WriteLine(d.GetText(1));
Console.WriteLine(d.GetTextChoices(1));
Console.WriteLine(d.ArrayToString(d.GetChoicesConsequences(2)));
Console.WriteLine(d.GetText(2));
Console.WriteLine(d.GetTextChoices(2));
Console.ReadLine();
}
}
我不明白这个问题:
int
只是System.Int32
的语法糖,所以您已经得到了预期的int数组。是否需要使用反射?还有其他方法可以让你一败涂地)debug.log不会完全打印出对象的内容,数组就是这样,如果你想让它打印出值,最好更具体一些。我需要找到名为fixedname的int[]的值,所以问题是如何获取“值”@MaxGameDev抱歉,我还是不知道你到底想要实现什么?您能否编辑您的问题,并添加完整的代码示例以及所需的输出?@MaxGameDev请参阅编辑#2。我把我认为是你的问题的完整代码。