为什么我不能在同一个类中拥有名为“id”和“set_id”的属性?

Luc*_*ann 10 c# asp.net properties asp.net-core

我正在尝试使用 Web API。我创建了一个类,以便可以反序列化返回的数据。问题是返回的对象具有以下两个属性:

public string id { get; set; }
public string set_id { get; set; }
Run Code Online (Sandbox Code Playgroud)

set_id与“集合”相关。set;编译器在from属性上抛出错误id,表示它已经包含 的定义set_id

CS0102 类型“MyClass”已包含“set_id”的定义

有什么办法可以在不重命名属性的情况下解决这个问题吗?

Moh*_*ari 13

我建议使用 JsonPropertyName

例如:

using System.Text.Json.Serialization;

    namespace ConsoleApp1
    {
        public class Class1
        {
            public string id { get; set; }
            [JsonPropertyName("set_id")]
            public string setId { get; set; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这个属性基于你的 json 库,如果你使用 newtonsoft 它将是JsonProperty


让我们找出为什么我们不能同时使用名称为 id 和 set_id 的 2 个变量

为了理解这一点,我们将看到由 C# 编译器生成的 IL 代码

对于具有 1 个属性 id 的类,生成的 IL 如下所示:

.class public auto ansi beforefieldinit ConsoleApp1.Test
    extends [System.Runtime]System.Object
{
    .custom instance void System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
        01 00 01 00 00
    )
    .custom instance void System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8) = (
        01 00 00 00 00
    )
    // Fields
    .field private string '<id>k__BackingField'
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    .custom instance void [System.Runtime]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggerBrowsableState) = (
        01 00 00 00 00 00 00 00
    )

    // Methods
    .method public hidebysig specialname 
        instance string **get_id** () cil managed 
    {
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x20b8
        // Header size: 1
        // Code size: 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldfld string ConsoleApp1.Test::'<id>k__BackingField'
        IL_0006: ret
    } // end of method Test::get_id

    .method public hidebysig specialname 
        instance void set_id (
            string 'value'
        ) cil managed 
    {
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x20c0
        // Header size: 1
        // Code size: 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: stfld string ConsoleApp1.Test::'<id>k__BackingField'
        IL_0007: ret
    } // end of method Test::set_id

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x20c9
        // Header size: 1
        // Code size: 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [System.Runtime]System.Object::.ctor()
        IL_0006: nop
        IL_0007: ret
    } // end of method Test::.ctor

    // Properties
    .property instance string id()
    {
        .get instance string ConsoleApp1.Test::get_id()
        .set instance void ConsoleApp1.Test::set_id(string)
    }

} // end of class ConsoleApp1.Test
Run Code Online (Sandbox Code Playgroud)

在此代码中,您可以看到 getter 和 setter 方法转换为名为 set_id 和 get_id 的 2 个方法(set_id = set , get_id = get)

现在我们保留了 set_id 和 get_id ,因此我们不能有其他名称为 set_id 或 get_id 的字段