propertyinfo,propertyinfo isclass

http://www.itjxue.com  2023-01-15 21:58  来源:未知  点击次数: 

c#中如何取消控件的事件绑定?

你这个委托是动态创建的自定义委托,估计不通过函数、凭几句代码是不可能清除事件的,还是自己编个函数吧。网上有很多,你可以参考这个:

这个估计你也看过了,不过确实够精炼了:(需要using

system.reflection)

void

clearevent(control

control,

string

eventname)

{

if

(control

==

null)

return;

if

(string.isnullorempty(eventname))

return;

bindingflags

mpropertyflags

=

bindingflags.instance

|

bindingflags.public

|

bindingflags.static

|

bindingflags.nonpublic;

bindingflags

mfieldflags

=

bindingflags.static

|

bindingflags.nonpublic;

type

controltype

=

typeof(system.windows.forms.control);

propertyinfo

propertyinfo

=

controltype.getproperty("events",

mpropertyflags);

eventhandlerlist

eventhandlerlist

=

(eventhandlerlist)propertyinfo.getvalue(control,

null);

fieldinfo

fieldinfo

=

(typeof(control)).getfield("event"

+

eventname,

mfieldflags);

delegate

d

=

eventhandlerlist[fieldinfo.getvalue(control)];

if

(d

==

null)

return;

eventinfo

eventinfo=controltype.getevent(eventname);

foreach

(delegate

dx

in

d.getinvocationlist())

eventinfo.removeeventhandler(control,

dx);

}

调用案例:

clearevent(button1,"click");//就会清除button1对象的click事件的所有挂接事件。

c#怎么将datable换成实体类

DataTable与实体类互相转换

/// summary

/// DataTable与实体类互相转换

/// /summary

/// typeparam name="T"实体类/typeparam

public class ModelHandlerT where T : new()

{

#region DataTable转换成实体类

/// summary

/// 填充对象列表:用DataSet的第一个表填充实体类

/// /summary

/// param name="ds"DataSet/param

/// returns/returns

public ListT FillModel(DataSet ds)

{

if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)

{

return null;

}

else

{

return FillModel(ds.Tables[0]);

}

}

/// summary

/// 填充对象列表:用DataSet的第index个表填充实体类

/// /summary

public ListT FillModel(DataSet ds, int index)

{

if (ds == null || ds.Tables.Count = index || ds.Tables[index].Rows.Count == 0)

{

return null;

}

else

{

return FillModel(ds.Tables[index]);

}

}

/// summary

/// 填充对象列表:用DataTable填充实体类

/// /summary

public ListT FillModel(DataTable dt)

{

if (dt == null || dt.Rows.Count == 0)

{

return null;

}

ListT modelList = new ListT();

foreach (DataRow dr in dt.Rows)

{

//T model = (T)Activator.CreateInstance(typeof(T));

T model = new T();

for (int i = 0; i dr.Table.Columns.Count; i++)

{

PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);

if (propertyInfo != null dr[i] != DBNull.Value)

propertyInfo.SetValue(model, dr[i], null);

}

modelList.Add(model);

}

return modelList;

}

/// summary

/// 填充对象:用DataRow填充实体类

/// /summary

public T FillModel(DataRow dr)

{

if (dr == null)

{

return default(T);

}

//T model = (T)Activator.CreateInstance(typeof(T));

T model = new T();

for (int i = 0; i dr.Table.Columns.Count; i++)

{

PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);

if (propertyInfo != null dr[i] != DBNull.Value)

propertyInfo.SetValue(model,dr[i],null);

}

return model;

}

#endregion

#region 实体类转换成DataTable

/// summary

/// 实体类转换成DataSet

/// /summary

/// param name="modelList"实体类列表/param

/// returns/returns

public DataSet FillDataSet(ListT modelList)

{

if (modelList == null || modelList.Count == 0)

{

return null;

}

else

{

DataSet ds = new DataSet();

ds.Tables.Add(FillDataTable(modelList));

return ds;

}

}

/// summary

/// 实体类转换成DataTable

/// /summary

/// param name="modelList"实体类列表/param

/// returns/returns

public DataTable FillDataTable(ListT modelList)

{

if (modelList == null || modelList.Count == 0)

{

return null;

}

DataTable dt = CreateData(modelList[0]);

foreach(T model in modelList)

{

DataRow dataRow = dt.NewRow();

foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())

{

dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);

}

dt.Rows.Add(dataRow);

}

return dt;

}

/// summary

/// 根据实体类得到表结构

/// /summary

/// param name="model"实体类/param

/// returns/returns

private DataTable CreateData(T model)

{

DataTable dataTable = new DataTable(typeof (T).Name);

foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())

{

dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));

}

return dataTable;

}

#endregion

}

c#反射判判断某个对象的某个属性是否string类型

public bool IsString(object obj,string propertyName)

{

if(obj==null)

{

throw new ArgumentNullException("obj");

}

Type type = obj.GetType();

PropertyInfo property = type.GetProperty(propertyName);

if(property==null)

{

throw new ArgumentException("不存在属性"+propertyName+"!","propertyName");

}

bool result=typeof(string).Equals(property.PropertyType);

if (resultproperty.GetValue(obj)==null)

{

property.SetValue(obj,string.Empty);

}

return result;

}

如何获取一个对象的所有属性?(c#)

我给你来一段吧 自己写的 给分啊

先写一个从字符串生成任何类型的对象的函数

public object stringToObject(string str,Type objectType)//传递两个参数,一个是字符串含有每个属性的属性名和值对,属性和值用冒号分隔,属性与属性用逗号分隔,另一个参数是要创建的对象的类型{string[] shuxing = str.Split(new char[] { ',' });//将字符串分解成 “属性:值”数组

for (int i = 0; i shuxing.Length; i++){shuxing[i]= shuxing[i].Replace(\, );

}//去掉字符串的双引号

object obj = System.Activator.CreateInstance(objectType); //使用反射动态创建对象

PropertyInfo[] pis =obj .GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);//获得对象的所有public属性

if (pis != null)//如果获得了属性

foreach (PropertyInfo pi in pis)//针对每一个属性进行循环{for (int i = 0; i shuxing.Length; i++)//检查字符串中的所有“属性:值”列表{if (shuxing[i].Split(new char[] { ':' })[0] == pi.Name)//如果对象的属性名称恰好和字符串中的属性名相同{Type proertyType= pi.PropertyType; //获得对象属性的类型

pi.SetValue(obj, Convert.ChangeType(shuxing[i].Split(new char[] { ':' })[1],proertyType), null);

//将字符串中的字符串类型的“值”转换为对象属性的类型,并赋值给对象属性}}}return obj;}然后调用这个函数

定义一个学生类class stu{string _name;int _age;public string name

{get{return _name;}

set {_name=value;}}public int age{get { return _age; }

set { _age = value; }}}pageload里面可以使用下面方法创建stu对象了

由于使用了反射技术需要导入名字空间 using System.Reflection;

using using System.Reflection;

。。。。。。。

.NET 如何判断一个对象中是否包含NULL值?

public static string AnyPropertyIsNullT(T t) where T : class

{

PropertyInfo[] rs =? t.GetType().GetProperties();

foreach( PropertyInfo prop in rs )

{

PropertyInfo Info = typeof(T).GetProperty(prop.Name);

object value =Info.GetValue(t);

if( value == null )

{

return string.Format("Property: {0}, null value!", prop.Name);

}

}

return null;

}

C#特性可以动态修改吗

不能

public class AppSetings

{

class A {

[Display(Name = "name")]

public int M { set; get; }

}

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Hello World!");

Program p = new Program();

p.mian();

Console.ReadLine();

}

public void mian() {

//fun();

A a = new A();

texingSet(a);

texingGet(a);

}

public void texingSet(A a) {

Type type = a.GetType();

PropertyInfo property = type.GetProperty("M");

object[] attributes = property.GetCustomAttributes(true);

DisplayAttribute display = (DisplayAttribute)attributes[0];

DisplayAttribute display2 = (DisplayAttribute)attributes[0];

Type despType = typeof(DisplayAttribute);

PropertyInfo namPropertyInfo = despType.GetProperty("Name");

namPropertyInfo.SetValue(display, "dddd");

Console.WriteLine("-----------------------");

Console.WriteLine("name1:" + display.Name);

Console.WriteLine("name2:" + display2.Name);

Console.WriteLine("-----------------------");

namPropertyInfo.SetValue(display2, "dddd2");

Console.WriteLine("name1:" + display.Name);

Console.WriteLine("name2:" + display2.Name);

Console.WriteLine("-----------------------");

PropertyInfo property2 = type.GetProperty("M");

object[] attributes2 = property2.GetCustomAttributes(true);

DisplayAttribute display3 = (DisplayAttribute)attributes2[0];

Type despType3 = typeof(DisplayAttribute);

PropertyInfo namPropertyInfo2 = despType3.GetProperty("Name");

namPropertyInfo2.SetValue(display2, "dddd3");

Console.WriteLine("name1:" + display.Name);

Console.WriteLine("name2:" + display2.Name);

Console.WriteLine("name3:" + display3.Name);

}

public void texingGet(A a) {

Type type = a.GetType();

PropertyInfo property = type.GetProperty("M");

object[] attributes = property.GetCustomAttributes(true);

DisplayAttribute display = (DisplayAttribute)attributes[0];

Console.WriteLine("name:" + display.Name);

}

}

(责任编辑:IT教学网)

更多

推荐Oracle文章