简体   繁体   English

刚开始学习 C# & ASP.NET - 奇怪的语法(对我来说),我不知道它是什么

[英]Just started learning C# & ASP.NET - strange syntax (to me) and I don't know what it is

So I'm watching the tutorials provided by Microsoft on ASP.Net所以我在看微软在 ASP.Net 上提供的教程

They created a class named product which looks like this:他们创建了一个名为 class 的产品,如下所示:

public class Product
    {
        public string Id { get; set; }
        public string Maker { get; set; }

        [JsonPropertyName("img")]
        public string Image { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int[] Ratings { get; set; }

        public override string ToString() => JsonSerializer.Serialize<Product>(this);
    }

I don't know what this is我不知道这是什么

[JsonPropertyName("img")]

and I have no idea how to search for it on Google.我不知道如何在谷歌上搜索它。

I don't understand what those square brackets are (they should define an array, but what's their point in that case?)我不明白那些方括号是什么(它们应该定义一个数组,但在这种情况下它们的意义何在?)

Also, what's the point of the whole line?另外,整条线的意义何在? What does it do?它有什么作用?

Thanks.谢谢。

In c#, you define arrays like this:在 c# 中,您可以像这样定义 arrays:

string[] companies = new string[3] {"Google", "Microsoft", "Facebook"}; //3 is the size and it's necessary when defining array without items. (It isn't necessary now)

So unlike python or javascript, array items are not located inside square brackets.因此,与 python 或 javascript 不同,数组项不在方括号内。

But there is another thing called an attribute .但是还有一种叫做属性的东西。
Attributes are special classes that inherit from the Attribute class and you use the constructor of that class in square brackets before the property or method or variable that you want the attribute to apply to it.属性是从Attribute class 继承的特殊类,您可以在您希望属性应用到它的属性或方法或变量之前的方括号中使用该 class 的构造函数。 In this example, You apply the JsonPropertyName attribute to the Image as the following:在此示例中,您将JsonPropertyName属性应用于Image ,如下所示:

[JsonPropertyName("img")]
public string Image { get; set; }

The JsonSerializer.Serialize<Product>(this) will check each property's attribute (if it has one) and applies that attribute while serializing. JsonSerializer.Serialize<Product>(this)将检查每个属性的属性(如果有的话)并在序列化时应用该属性。 When you Serialize an instance of this class using the overridden ToString() method, this attribute forces the serializer to use img for JSON property name instead of Image so you will get img instead of Image in JSON.当您使用重写的ToString()方法序列化此 class 的实例时,此属性会强制序列化程序将 img 用于 JSON 属性名称而不是 Image,因此您将在 Z0ECD11C1D7A287A278D1 中获得 img 而不是 Image。
Example without using attribute:使用属性的示例:

{
    "Id": <Id>,
    "Maker": <Maker>,
    "Image": <Image>, //Image
    "Url": <Url>,
    "Title": <Title>,
    "Description": <Description>,
    "Ratings": [<Ratings>]
}

But when you use [JsonPropertyName("img")] attribute, it will look like this:但是当你使用[JsonPropertyName("img")]属性时,它看起来像这样:

{
    "Id": <Id>,
    "Maker": <Maker>,
    "img": <Image>, //img
    "Url": <Url>,
    "Title": <Title>,
    "Description": <Description>,
    "Ratings": [<Ratings>]
}

For more information about this attribute you can refer to following links:有关此属性的更多信息,您可以参考以下链接:
usage and structure 用法结构
And for more information about attributes, visit this .有关属性的更多信息,请访问

It describes and gives some features for one bottom property.它描述并给出了一个底部属性的一些特性。 You can search it as "Attributes in C#".您可以将其搜索为“C# 中的属性”。 You will get a lot of documentation for them.你会得到很多他们的文档。

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

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