using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication33
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Values item = SetValues();
PrintValues(item);
}
/// <summary>
/// 通过遍历属性输出属性和值
/// </summary>
/// <param name="item"></param>
private void PrintValues(Values item)
{
System.Reflection.PropertyInfo[] properties = item.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
string name=property.Name;
string value = property.GetValue(item).ToString();
}
}
/// <summary>
/// 通过遍历属性赋值
/// </summary>
/// <returns></returns>
private Values SetValues()
{
Values item = new Values();
System.Reflection.PropertyInfo[] properties = item.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
properties[i].SetValue(item, (i + 1));
}
return item;
}
}
class Values
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Value3 { get; set; }
public int Value4 { get; set; }
public int Value5 { get; set; }
public int Value6 { get; set; }
public int Value7 { get; set; }
public int Value8 { get; set; }
public int Value9 { get; set; }
public int Value10 { get; set; }
}
}
————————————————
public class PP
2 {
3 public string a { get; set; }
4 public string b { get; set; }
5 public string c { get; set; }
6 }
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 Hashtable ht = new Hashtable();
14 ht.Add("a", "utf8");
15 ht.Add("b", "xxxx");
16 ht.Add("c", "xxxx");
17 PP config = new PP();
18 PropertyInfo[] propertys = config.GetType().GetProperties();
19 foreach (PropertyInfo property in propertys)
20 {
21 for (int i = 0; i < ht.Count; i++)
22 {
23 property.SetValue(config, ht[property.Name].ToString(), null);
24 }
25 }
26 Console.WriteLine(config.a+"\t"+config.b);
27 Console.ReadLine();
28 }
29 }
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication33{ public partial class Defau...
PropertyInfo[] propertys = new myClass().GetType().GetProperties(); 即可获得
对象
myClass的所有
属性
组成的集合 propertys
转载于:https://www.cnblogs.com/wangchunming/archive/2012/05/10/2494476.html...
数据实体类中如何给
属性
赋值
.因为从数据库中取出来的是一个DataSet如果一个一个给数据实体类中的
属性
赋值
显的非常的笨拙,而且我们的数据表多达100个.在网上搜索了下写出了个简单的方法,代码如下:
首先我们做个实体类Class1,那我们怎么找到他并
赋值
呢?
QQ:292258449
在使用三层架构开发一个网站时,希望把DataTable
对象
转换为List
对象
,于是在网上找资料,总结一个比较方便的方法来实现——使用反射。
初始化一个List
对象
获取到T所有的
属性
,初始化一个T
对象
遍历
所有
属性
,如果DataTable中含有相应
属性
的值则为T
对象
赋值
,如果没有对应的列则检查数据模型是否定义有误(
属性
名与列名比较时不区分大小写) 将T
对象
添加到List
对象
中
总体代码:
反射获取到的
属性
信息
DataTable中的列信息,对比上图,我们会发现,
属性
中首字母都是大写,而列名则是采用的Camel命名,首字母是小写,但是通过单步调试可以发现 dt.
本文介绍
C#
上如何使用DataSet
对象
,并对DataSet
对象
中的表进行
遍历
,同时
遍历
表中的每一行,
遍历
每一行的每一列的值。
首先什么是DataSet,在
C#
中,Dataset就像一个数据库,其中可以有多个表(Table),也可以只有一个表,每个表中有行(DataRow)和列(DataColumn)。使用DataRow[DataColumn]的形式可以得到某行某列数据值。
代码如下:
//下面例子中使用foreach来
遍历
DataSet中的所有表,对于每个表
遍历
所有的记录,并输出每一行的每个值
foreach (DataTable dt in MyDataset.Tables) //MyD
今天有个网友问如何历遍
对象
的所有公共
属性
,并且生成XML。采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到。
对象
模型:
public class Master
public string Description { get; set; }
public List...
一、初始化数组:
代码如下:string[] s1 = {“aaa”,”bbb”,”ccc”} //直接
赋值
string[] s2 = new string[5] {“aaa”,”bbb”,”ccc”}; //
赋值
加指定长度string[] s3 = new string[]{“aaa”,”bbb”,”ccc”}; string[] s4 = new string[5]; //用循环直接
赋值
二、
遍历
数组:
代码如下:foreach (string s in str){ Console.Write(s);}
三、二维数组:string[,] arr2; //声明一个二维数组st
本文实例讲述了
C#
实现利用反射简化给类字段
赋值
的方法。分享给大家供大家参考。具体分析如下:
说明:这个例子主要的思路是建立一个类和数据库查询语句的字段结构是一致的
然后利用反射,直接用数据字段名称进行拼凑,给类
对象
的字段进行
赋值
1.类的定义
namespace CCB_Donet.ClassFolder
public class FieldRuleInfo
public string gStrFNo;
public string gStrFName;
public string gStrFLock;
public string gStrFCaption;
有时我们需要判断某个类是否实现了某个接口(Interface),比如在使用反射机制(Reflection)来查找特定类型的时候。
简单来说,可以使用Type.IsAssignableFrom方法:
typeof(IFoo).IsAssignableFrom(bar.GetType());
typeof(IFoo).IsAssignableFrom(typeof(BarClass));
从字面意思可以看出,IsAssignableFrom表示BarClass类型能否
赋值
给IFoo接口,所以它返回true的条件就是BarClass直接或间接实现了IFoo接口。Type类型中还有一个方法是IsSu