c# - 如何从类/对象 C# 中检索所有属性
问题描述
假设我有这个课程:
public class Test
{
public string Attribute1 { get; set; }
public int Attribute2 { get; set; }
}
我有这个类的对象
Test test = new Test();
现在我想填充这个对象,但是我不知道里面有什么属性。如何获取属性的引用来填充它们?我在想这样的事情:
List<object> attrs = GetAttributes<Test>(test);
foreach (var attr in attrs)
{
if(attr.GetType() == typeof(string))
{
attr = "filled";
}
else
{
attr = 1;
}
}
Console.WriteLine(test.Attribute1); //print "filled"
Console.WriteLine(test.Attribute2); //print 1
解决方案
推荐阅读
- java - 如何在 Eclipse 插件中以编程方式编辑 xml 模式文件
- ios - 是否可以延迟 iOS Safari 上的视频播放?
- shell - 如何为文件夹中的所有pdf添加页眉和页脚
- c++ - 代码为相似的输入给出不同的结果
- git - 如何切换到不同的分支但维护文件夹和提交历史
- python - C#:从另一个进程正在使用的文件中快速读取
- c++ - 给定硬币的所有可能总和
- javascript - 如何禁用 ` 中显示的建议密码列表
` 用于所有浏览器的剃刀组件中的字段 - wordpress - 自版本 4.3.0 文件位置以来,不推荐使用 WP_Widget 的调用构造函数方法
- android - 延迟在 Dispatchers.Main 上启动协程