javascript C#中的Javascript对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26498808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:02:54  来源:igfitidea点击:

Javascript Object in C#

javascriptc#unity3d

提问by user3789335

Is there an easy way to create something like the following JS code:

是否有一种简单的方法可以创建类似以下 JS 代码的内容:

var players = [
    {name:"Joe",score:25,color:"red",attribs:[0,1,2,3,4]},
    {name:"Jenny",score:1,color:"black",attribs:[4,3,2,1,0]}
];

in C#(for Unity 3d)?

C# 中(对于 Unity 3d)?

I've already looked at List, Dictionary and ArrayList, but everything seam so ... inflexible and overcomplicated...

我已经看过 List、Dictionary 和 ArrayList,但一切都如此......不灵活且过于复杂......

The main objective here is to have something flexible, that can be access from many other places w/o the need to remember array indexes, variable types etc. Probably can't be done in C#... But something that is fairly close should be enough. ArrayList maybe...?

这里的主要目标是有一些灵活的东西,可以从许多其他地方访问,而无需记住数组索引、变量类型等。可能无法在 C# 中完成......但应该是相当接近的足够。ArrayList 可能...?

Thank you.

谢谢你。

回答by OJay

This JavaScript being an Array of objects is directly transferable to C#, and you have multiple ways to do it and multiple collection classes to use (unlike JavaScript which only has one collection class)

这个 JavaScript 是一个对象数组,可以直接转移到 C#,你有多种方法可以做到这一点,并且可以使用多个集合类(不像 JavaScript 只有一个集合类)

It can almost be written verbatim in C# and is perfectly valid (Using Anonymous Types):

它几乎可以用 C# 逐字编写并且完全有效(使用匿名类型):

var players = new [] {
    new  {name = "Joe",score = 25, color = "red", attribs = new int[]{ 0,1,2,3,4}},
    new  {name = "Jenny",score = 1, color = "black", attribs = new int[]{4,3,2,1,0}}
};

But I'm not sure if this is going to achieve what you want (Functionality wise)

但我不确定这是否能达到你想要的(功能明智)

IMO creating a typed object would be a better approach (I would consider this to be a better approach in both C# and JavaScript), so I would approach it more like this (JavaScript):

IMO 创建类型化对象将是一种更好的方法(我认为这在 C# 和 JavaScript 中都是更好的方法),所以我会更像这样(JavaScript):

function Player(name,score,color,attribs)
{
    this.name = name;
    this.score = score;
    this.color = color;
    this.attribs = attribs;
}

var players = [
    new Player("Joe", 25, "red", [0, 1, 2, 3, 4]),
    new Player("Jenny",1,"black", [4, 3, 2, 1, 0])
];

and the same thing in C#:

在 C# 中也是同样的事情:

public class Player
{
    public string name;
    public int score;
    public string color;
    public int[] attribs;
}


Player[] players = new Player[]{
         new Player(){ name = "Joe", score = 25, color = "red", attribs = new int[]{0,1,2,3,4} },
         new Player(){ name = "Jenny", score = 1, color = "black", attribs = new int[]{4,3,2,1,0} },
};

or to get a lot more flexibility you can use them in a List like:

或者为了获得更大的灵活性,您可以在列表中使用它们,例如:

 List<Player> players = new List<Player>(){
             new Player(){ name = "Joe", score = 25, color = "red", attribs = new int[]{0,1,2,3,4} },
             new Player(){ name = "Jenny", score = 1, color = "black", attribs = new int[]{4,3,2,1,0} },
    };

回答by logikal

Could you make a Playermodel (class) with the Name, Score, Color, Attribsproperties and store each Playerin memory in a List<Player>:

你能不能PlayerName, Score, Color,Attribs属性创建一个模型(类)并将每个属性存储Player在内存中List<Player>

public class Player
{
    public string Name { get; set; }
    public int Score { get; set; }
    public string Color { get; set; }
    public int[] Attribs { get; set; }
}

...

List<Player> myPlayers = new List<Player>();
Player joe = new Player();
joe.Name = "Joe";
joe.Score = 25;
joe.Color = "red;
joe.Attribs = new int[] { 0, 1, 2, 3, 4 };;
myPlayers.Add(joe);

Using System.Linqyou could query the List<Player>for specific players Eg:

使用System.Linq您可以查询List<Player>特定玩家的信息,例如:

Player player = myPlayers.SingleOrDefault(p => p.Name == "Joe");

Player player = myPlayers.SingleOrDefault(p => p.Name == "Joe");

Extra credit - always test that player is not default before trying to use it:

额外的功劳 - 在尝试使用它之前总是测试玩家不是默认的:

if (player != default(Player))
{
    // do something with joe
}

回答by Alexei Levenkov

More or less match for syntax would be anonymous types:

或多或少的语法匹配是匿名类型:

var players = new[]{
    new {name = "Joe", score = 25, color = "red", attribs = new[]{0,1,2,3,4}},
    new {name = "Jenny", score = 1, color = "black", attribs = new []{4,3,2,1,0}}
};

but it have very different meaning than JavaScript (array of immutable strongly typed objects in C# vs. dictionary of duck typed object in JavaScript).

但它与 JavaScript 的含义非常不同(C# 中的不可变强类型对象数组与 JavaScript 中的鸭子类型对象字典)。

You may want to look into using dynamicand ExpandoObjectwhich exists to support languages like JavaScript in .Net if they are present in Unity3d.

如果 Unity3d 中存在 .Net 中的 JavaScript 等语言,您可能需要考虑使用dynamicExpandoObject来支持这些语言。

    dynamic person = new System.Dynamic.ExpandoObject();
    person.Name = "John Smith";
    person.Age = 33;

Note that C# is strongly typed language and it may be better to change way of thinking to match the language. Staying with JavaScript is reasonable option if you can't get away from duck typed languages.

请注意,C# 是强类型语言,最好改变思维方式以匹配该语言。如果您无法摆脱鸭子类型的语言,那么继续使用 JavaScript 是合理的选择。