簡體   English   中英

從嵌套的子屬性中獲取父匿名類型屬性的值。 如何?

[英]Get value of parent anonymous type property from within nested child properties. How?

這個任務在 Javascript 中是微不足道的,但在 C# 中,我找不到辦法。

我有一個包含嵌套匿名類型的匿名類型。 這些子類型包含屬性rootLength ,如果您願意,該屬性最終應該設置為父對象的值,例如 Global。

如何根據父級的值設置子級的值?

var opensslSettings = new 
{
    rootLength = 2048,
    pkcs3 = new 
    {
        rootLength = 2048,
        outDir = AppDomain.CurrentDomain.BaseDirectory,
        outDhp = "www_example_com_2048bit.pkcs3"
    },
    pkcs10 = new 
    {
        rootLength = 2048,
        outDir = AppDomain.CurrentDomain.BaseDirectory,
        outKeyFile = "www_example_com_2048bit.pkcs8",
        outCsrFile = "www_example_com_2048bit.pkcs10"
    },
};

Console.WriteLine(opensslSettings.ToString());

下面是完成的工作示例。

using System.Diagnostics;
using System.Text.Json.Nodes;

namespace asynchronousCode
{
    class Program
    {
        public async static Task Main(string[] args)
        {
            var rootLength = 2048;

            var opensslSettingsJson = new JsonObject
            {
                ["rootLength"] = rootLength,
                ["pkcs3"] = new JsonObject 
                {
                    ["rootLength"] = rootLength,
                    ["outDir"] = AppDomain.CurrentDomain.BaseDirectory,
                    ["outDhp"] = $"www_example_com_{rootLength}bit.pkcs3"
                },
                ["pkcs10"] = new JsonObject
                {
                    ["rootLength"] = rootLength,
                    ["outDir"] = AppDomain.CurrentDomain.BaseDirectory,
                    ["outKeyFile"] = $"www_example_com_{rootLength}bit.pkcs8",
                    ["outCsrFile"] = $"www_example_com_{rootLength}bit.pkcs10"
                },
            };

            Console.WriteLine(opensslSettingsJson.ToJsonString() + Environment.NewLine);

            var opensslSettingsObject = new 
            {
                rootLength = rootLength,
                pkcs3 = new 
                {
                    rootLength = rootLength,
                    outDir = AppDomain.CurrentDomain.BaseDirectory,
                    outDhp = $"www_example_com_{rootLength}bit.pkcs3"
                },
                pkcs10 = new 
                {
                    rootLength = rootLength,
                    outDir = AppDomain.CurrentDomain.BaseDirectory,
                    outKeyFile = $"www_example_com_{rootLength}bit.pkcs8",
                    outCsrFile = $"www_example_com_{rootLength}bit.pkcs10"
                },
            };
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM