好吧,显然我在.NET中迷失了,因为它不是我通常编写的代码。我正在尝试在解析配置文件期间创建一个对象数组,因此它们可以快速有效地处理在帧处理过程中可能我一直在网上搜索我应该做的事情,但要么我不知道正确的命名法要么被搜查,要么我走错了路。
这是试图遍历JSON文件并将预制类与配置值一起分配到对象数组中的代码,希望以每秒60帧的速度循环遍历这些对象。
public class UserConfig {
public object[] myActions = {
};
public void processConfig() {
this.myActions.Clear();
string json = File.ReadAllText("config.json");
JToken jsonNodes = JToken.Parse( json );
WalkNode( jsonNodes, item => {
object newAction = new object{};
switch( item["bodyAction"].ToString() ) {
case "handLeftForward":
newAction.detector = new DetectArmForward( JointType.ShoulderLeft, JointType.HandLeft );
break;
}
switch( item["detectType"].ToString() ) {
case "distanceMin":
switch( newAction.detector.direction ) {
case "negative":
switch( newAction.detector.path ) {
case "Z":
newAction.detector.checker = new CheckNegativeDistanceMinZ( newAction.detector.jointBase, newAction.detector.jointEnd, item.distanceMin );
break;
}
break;
case "positive":
switch( newAction.detector.path ) {
case "Z":
newAction.detector.checker = new CheckPositiveDistanceMinZ( newAction.detector.jointBase, newAction.detector.jointEnd, item.distanceMin );
break;
}
break;
}
break;
}
switch( item["executeAction"].ToString() ) {
case "keyTap":
newAction.executer = new executeKeyTap( 123 );
break;
}
this.myActions[ this.myActions.Count() ] = newAction;
});
}
}
然后希望我能做点像......
for( action in this.myActions ) {
if( action.detector.checker.check( joints ) ) {
action.executer.execute( joints );
}
}
检查并有条件地执行从配置处理构建的方法。
它不会让我使用这个设置(以及我尝试过的所有设置),因为对象必须预先声明,但我不知道我需要什么特定的对象...所以我迷路了:) 感谢大家的关注!
答案 0 :(得分:0)
我可能会忽视某些内容,但似乎您正在寻找命令foreach
而不是for。在你的情况下,这应该可以正常工作。
foreach(object action in this.myActions)
{
if( action.detector.checker.check( joints ) ) {
action.executer.execute( joints );
}
}
答案 1 :(得分:0)
如果您只想将对象转储到容器中,然后在另一端获取数组,则可以定义一个列表:
var myList = new List<dynamic>();
myList.Add(someObject);
var myArray = myList.ToArray();
这将为您提供一组动态对象,但您可以在定义列表时将其更改为所需的任何对象类型。更多信息:.Net Collections
对象需要有一个名为detector的公共属性,所以它需要来自这样的类:
class myObject {
public dynamic detector;
public dynamic someOtherProperty;
}
new DetectArmForward
创建的对象将分配给该属性。