c# 遍历类中的属性并赋值的问题

qq_40127297 2018-11-01 06:45:11

如图我想遍历这样的类中的所有属性
并赋值应该怎么办啊。
没有数组的话我可以遍历。
...全文
1049 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxy2847225301 2018-11-02
  • 打赏
  • 举报
回复
public class Rootoject {
public string A { get; set; }
public string B { get; set; }
public Entry[] entry { get; set; }
}
public class Entry {
public string A1 { get; set; }
public string A2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
string A = "A的值";
string B = "B的值";
Entry[] entry ={new Entry{A1="第一个",A2="第一个"},
new Entry{A1="第二个",A2="第二个"},
new Entry{A1="第三个",A2="第三个"},
new Entry{A1="...",A2="..."}
};

Rootoject config = new Rootoject();
PropertyInfo []propertys = config.GetType().GetProperties();
foreach (PropertyInfo property in propertys)
{
if (property.Name.Equals("A"))
{
property.SetValue(config, A, null);
}
else if (property.Name.Equals("B"))
{
property.SetValue(config,B, null);
}
else if (property.Name.Equals("entry"))
{
property.SetValue(config, entry, null);
}
}
Console.WriteLine(config.A+"\t"+config.B);
foreach (var item in config.entry)
{
Console.WriteLine(item.A1+"\t"+item.A2);
}
}
}
zxy2847225301 2018-11-02
  • 打赏
  • 举报
回复

public class Rootoject {
public string A { get; set; }
public string B { get; set; }
public Entry[] entry { get; set; }
}
public class Entry {
public string A1 { get; set; }
public string A2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
string A = "A的值";
string B = "B的值";
Entry[] entry ={new Entry{A1="第一个",A2="第一个"},
new Entry{A1="第二个",A2="第二个"},
new Entry{A1="第三个",A2="第三个"},
new Entry{A1="...",A2="..."}
};

Rootoject config = new Rootoject();
PropertyInfo []propertys = config.GetType().GetProperties();
foreach (PropertyInfo property in propertys)
{
if (property.Name.Equals("A"))
{
property.SetValue(config, A, null);
}
else if (property.Name.Equals("B"))
{
property.SetValue(config,B, null);
}
else if (property.Name.Equals("entry"))
{
property.SetValue(config, entry, null);
}
}
Console.WriteLine(config.A+"\t"+config.B);
foreach (var item in config.entry)
{
Console.WriteLine(item.A1+"\t"+item.A2);
}
}
}
qq_40127297 2018-11-02
  • 打赏
  • 举报
回复
引用 10 楼 hanjun0612 的回复:
我觉得,如果牵涉到model内部嵌套model的。
并且不确定类型的话。
需要递归反射。不过你肯定要限制递归最大值

好的,我琢磨一下,多谢。
正怒月神 2018-11-02
  • 打赏
  • 举报
回复
我觉得,如果牵涉到model内部嵌套model的。 并且不确定类型的话。 需要递归反射。不过你肯定要限制递归最大值
qq_40127297 2018-11-02
  • 打赏
  • 举报
回复
引用 8 楼 qq_32661557 的回复:

Rootobject config = new Rootobject();
PropertyInfo[] propertys = config.GetType().GetProperties();
foreach (var property in propertys)
{
if (property.PropertyType == typeof(string))
{
property.SetValue(config, "1111", null);
}
else if (property.PropertyType == typeof(Entry[]))
{
Entry[] entry = {
new Entry
{
A1 = "2222",
B1 = "2222"
},
new Entry
{
A1 = "3333",
B1 = "3333"
}
};
property.SetValue(config, entry, null);
}
}

多谢多谢。那我现在要是不确定Entry[] 这个数组有几个的时候怎么办呢?
本人QQ-554433626 2018-11-02
  • 打赏
  • 举报
回复

           Rootobject config = new Rootobject();
            PropertyInfo[] propertys = config.GetType().GetProperties();
            foreach (var property in propertys)
            {
                if (property.PropertyType == typeof(string))
                {
                    property.SetValue(config, "1111", null);
                }
                else if (property.PropertyType == typeof(Entry[]))
                {
                    Entry[] entry = {
                        new Entry
                        {
                            A1 = "2222",
                            B1 = "2222"
                        },
                        new Entry
                        {
                            A1 = "3333",
                            B1 = "3333"
                        }
                    };
                    property.SetValue(config, entry, null);
                }
            }
qq_40127297 2018-11-02
  • 打赏
  • 举报
回复
引用 3 楼 zxy13826134783 的回复:
在foreach里面根据当前属性名来赋值,用条件语句实现

能写个例子吗,不胜感激
qq_40127297 2018-11-02
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
属性具有不同的类型和功能,自然其初值也是不一样的
像你这样眉毛胡子一把抓的统一赋予相同的值,显然是不合理的

所以我想知道怎么赋值的,不知道咋写了。
qq_40127297 2018-11-02
  • 打赏
  • 举报
回复
引用 4 楼 caozhy 的回复:
你是要给数组赋值,还是给数组的元素赋值?
应该是config.GetType().GetProperty("Entry").SetValue(new Entry[] { ... }, null);

我想先给属性赋值,然后给下面的数组赋值,给数组里面都的元素赋值
threenewbee 2018-11-01
  • 打赏
  • 举报
回复
你是要给数组赋值,还是给数组的元素赋值? 应该是config.GetType().GetProperty("Entry").SetValue(new Entry[] { ... }, null);
zxy2847225301 2018-11-01
  • 打赏
  • 举报
回复
在foreach里面根据当前属性名来赋值,用条件语句实现
xuzuning 2018-11-01
  • 打赏
  • 举报
回复
属性具有不同的类型和功能,自然其初值也是不一样的
像你这样眉毛胡子一把抓的统一赋予相同的值,显然是不合理的
qq_40127297 2018-11-01
  • 打赏
  • 举报
回复
我这样写的,能遍历,但是不能给数组赋值。还要怎么才能接着遍历类里的数组,然后赋值呢

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧