c# 利用反射获取属性名和值

winesmoke 2007-11-21 10:32:36
class A
{
public string Property1{get;set;}
public int Property2{get;set;}
}

class B
{
public string Property1{get;set;}
public A Property2{get;set;}
}

请问如何通过反射获取class B 的Property2属性(也就是class A)的 属性名和值?
...全文
11257 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dong520888 2011-09-11
  • 打赏
  • 举报
回复
还是没有看到我想要的!
李世垚 2011-05-03
  • 打赏
  • 举报
回复
哎,虽然楼上还有为达人写的代码不错,可惜连需求都么有弄明白。这样的人在我的手下永远不会委以重任。
linghuliz 2010-03-06
  • 打赏
  • 举报
回复
C#语言是微软的首推,难道应用不广吗?
回13楼的
swafboxh 2007-11-23
  • 打赏
  • 举报
回复
路过
suyiming 2007-11-22
  • 打赏
  • 举报
回复
up
SatSun 2007-11-22
  • 打赏
  • 举报
回复
C#语言现在应用还很广吗?
winesmoke 2007-11-21
  • 打赏
  • 举报
回复
谢谢各位哈!
jinjazz 2007-11-21
  • 打赏
  • 举报
回复
sorry,写错了

public void PrintProperties<T>(T t)
{
if (t == null)
{
return;
}

PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return;
}

foreach (PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
Console.WriteLine("{0}:{1}", name,value);
}
else
{

PrintProperties(value);
}
}
}
jinjazz 2007-11-21
  • 打赏
  • 举报
回复
递归一下

public void PrintProperties<T>(T t)
{
if (t == null)
{
return;
}

PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return;
}

foreach (PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
Console.WriteLine("{0}:{1}", name,value);
}
else
{
foreach (PropertyInfo itemsub in value.GetType().GetProperties())
{

PrintProperties(value);
}
}
}
}
winesmoke 2007-11-21
  • 打赏
  • 举报
回复
各位达人再帮see see 啊!
winesmoke 2007-11-21
  • 打赏
  • 举报
回复

//print public properties
public void PrintProperties<T>(T t)
{
if (t == null)
{
return;
}

PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return;
}

foreach (PropertyInfo item in properties)
{
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
Console.WriteLine("{0}:{1}", item.Name, item.GetValue(obj, null));
}
else
{
//我不知道怎样写了
}
}


大家在帮看看吧
jinjazz 2007-11-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication26
{
public partial class Form1 : Form
{

class A
{
public string Property1 { get { return "Property1"; } }
public int Property2 { get { return 1; } }
}

class B
{
private A property2=new A();
public string Property1 { get { return "Property1"; } }
public A Property2 { get { return property2; } }
}


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

B b = new B();


foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties())
{
MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
}


}
}
}
winesmoke 2007-11-21
  • 打赏
  • 举报
回复
@mooddecode
我没说清楚么?

比如
A a = new A();
a.Property1 = "property1OfA";
a.Property2 = 1;
B b = new B();
b.Property1 = "property1OfB";
b.Proerty2 = a;


现在我想通过反射把对象b和a 的属性名和值打印出来
不知道我说明白没

lovefootball 2007-11-21
  • 打赏
  • 举报
回复
Type type = typeof(B);
B b = new B();
PropertyInfo pi = type.GetProperty("Property2");
A a = (A)pi.GetValue(b, null);
..............
jinjazz 2007-11-21
  • 打赏
  • 举报
回复
B b = new B();

Type t = b.GetType().GetProperty("Property2").GetValue(b, null).GetType();

foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties())
{
MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
}
syllcy 2007-11-21
  • 打赏
  • 举报
回复
Assembly assembly = Assembly.LoadFrom(filename);
Type t = assembly.GetType("B");
Object obj = t.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);

string strReturn = (string)t.InvokeMember("Property2",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null,
obj,
null);
strReturn 就是你所要取得属性值。
mooddecode 2007-11-21
  • 打赏
  • 举报
回复
:)







--------------

110,566

社区成员

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

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

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