如何为一个匿名对象的属性设置值? [英] How to set value for property of an anonymous object?

查看:397
本文介绍了如何为一个匿名对象的属性设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code,例如:

this is my code for example:

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

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

中出现异常:没有找到属性设置方法。我想知道如何创建一个匿名类型与可设置的属性。

It occurs an exception: "Property set method not found". I want to know how to create an anonymous type with properties which can be set.

感谢。

推荐答案

匿名类型属性为只读,他们不能进行设置。

Anonymous type properties are read only and they cannot be set.

匿名类型提供了一种方便的方式来封装一组   只读属性成一个对象,而无需显式   首先定义一个类型。该类型名称是由编译器生成,是   没有可用的源$ C ​​$ C级。各属性的类型是   由编译器推断

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.

匿名类型(C#编程指南)

这篇关于如何为一个匿名对象的属性设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆