gpt4 book ai didi

c# - 如何为匿名对象的属性设置值?

转载 作者:IT王子 更新时间:2023-10-29 04:23:06 25 4
gpt4 key购买 nike

例如,这是我的代码:

var output = new
{
NetSessionId = string.Empty
};

foreach (var property in output.GetType().GetProperties())
{
property.SetValue(output, "Test", null);
}

发生异常:“找不到属性集方法”。我想知道如何创建具有可设置属性的匿名类型。

谢谢。

最佳答案

匿名类型属性是只读的,不能设置。

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

Anonymous Types (C# Programming Guide)

关于c# - 如何为匿名对象的属性设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com