如何动态的生成一个实现 System.ComponentModel.INotifyPropertyChanged 的类的定义?

websco 2010-03-07 06:52:00
如何动态的生成一个实现 System.ComponentModel.INotifyPropertyChanged 的类的定义?
这个动态类中有形如 A、B、C、D、E、F等多个动态的属性。

public class ClassA: System.ComponentModel.INotifyPropertyChanged
{
string AField;
public string A
{
get
{
return this.AField;
}
set
{
if ((object.ReferenceEquals(this.AField, value) != true))
{
this.AField = value;
this.RaisePropertyChanged("A");
}
}
}

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}




附: 已知生成一个简单的 getter setter 的属性的代码如下:


/// <summary>
/// 添加一个public属性,并且会创建对应的名为 propertyNm + "Field" 的私有字段
/// </summary>
/// <param name="propertyNm"></param>
/// <param name="type"></param>
/// <param name="canGet">是否实现getter</param>
/// <param name="canSet">是否实现setter</param>
public void AppendPublicProperty(string propertyNm, Type type, bool canGet, bool canSet)
{
FieldBuilder field = this.tb.DefineField(propertyNm + "Field", type, FieldAttributes.Private);

PropertyBuilder property = tb.DefineProperty(propertyNm, PropertyAttributes.HasDefault, type, null);

MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

if (canGet)
{
MethodBuilder getAccessor = tb.DefineMethod("get_" + propertyNm, getSetAttr, type, Type.EmptyTypes);
ILGenerator getIL = getAccessor.GetILGenerator();
// For an instance property, argument 默认值 is the instance. Load the
// instance, then load the private field and return, leaving the
// field value on the stack.
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, field);
getIL.Emit(OpCodes.Ret);
property.SetGetMethod(getAccessor);
}

if (canSet)
{
MethodBuilder setAccessor = tb.DefineMethod("set_" + propertyNm, getSetAttr, null, new Type[] { type });
ILGenerator setIL = setAccessor.GetILGenerator();
// Load the instance and then the numeric argument, then store the
// argument in the field.
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Stfld, field);
setIL.Emit(OpCodes.Ret);
property.SetSetMethod(setAccessor);
}
}
...全文
283 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xie_xingan 2010-03-10
  • 打赏
  • 举报
回复
说说你的需求?为什么要这么做?
jv9 2010-03-10
  • 打赏
  • 举报
回复
引用 4 楼 websco 的回复:
谢谢楼上的两位,我就是想要的是 public class Person : INotifyPropertyChanged{...} 的IL代码,因为我要生成的 Person 是没有固定名称、固定数量的属性的。 有点类似于从 DataTable.Rows 中生成 ObservableCollection <object> 的意思,但是要求“object” 实现 INotifyPropertyChan?-



现在问题应该解决了吧。
websco 2010-03-10
  • 打赏
  • 举报
回复
动态程序集可能用的人比较少,不过我却认为使用是很有必要的,特别是Silverlight不支持System.Data.DataTable,而且早期的 ADO.NET Data Service 又不支持翻页,从减轻数据库负担以及减轻网络数据量传输而言,将 WCF 中的 System.Data.DataTable 或 System.Data.DataSet 序列化后传递至 Silverlight 端仍是一个有效的选择。

不过我这个例子和 DataTable 并没有关系,只是我是以Xml作为数据源头,生成具有动态属性的动态类,以此作为绑定的数据源,然后在Silverlight页面上显示,显示的效果类似于 Excel ,有单元格合并等功能。
(不知道该如何发图,不然发个图上来)。

现在问题已解决,有意看源码者请至http://blog.csdn.net/websco/archive/2010/03/10/5363572.aspx,共同学习。
jv9 2010-03-09
  • 打赏
  • 举报
回复
能问一下你想实现什么么?动态生成是什么意思呢?

正常使用:


//Add using statements
using System.ComponentModel;
using System.Windows.Data;


...


// Create a class that implements INotifyPropertyChanged
public class Person : INotifyPropertyChanged
{
private string firstNameValue;
public string FirstName{
get { return firstNameValue; }
set
{
firstNameValue=value;
// Call NotifyPropertyChanged when the property is updated
NotifyPropertyChanged("FirstName");
}
}

// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;

// NotifyPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

websco 2010-03-09
  • 打赏
  • 举报
回复
谢谢楼上的两位,我就是想要的是 public class Person : INotifyPropertyChanged{...} 的IL代码,因为我要生成的 Person 是没有固定名称、固定数量的属性的。 有点类似于从 DataTable.Rows 中生成 ObservableCollection<object> 的意思,但是要求“object” 实现 INotifyPropertyChanged。
  • 打赏
  • 举报
回复
估计指的是绑定时候的动态更新吧
websco 2010-03-08
  • 打赏
  • 举报
回复
没有人回答吗?请教各位大哥啊
源码链接: https://pan.quark.cn/s/a4b39357ea24 在本研究中,我们将详细研究如何借助Python执行数据可视化,旨在剖析2018年期间中国四个主要城市——北京、上海、广州以及深圳的空气质量状况。通过绘制反映空气质量指数(AQI)与细颗粒物(PM2.5)变化趋势的图表,我们能够深入理解这些大都市全年的空气环境质量,并明确评估其优良天气所占的比重。 我们必须首先进行数据准备工作。在当前提供的压缩文件内,名为"2018天气"的文件极有可能是数据来源,其中可能收录了涉及四个城市每日空气质量监测的详细信息。这些数据通常涵盖日期、城市名称、AQI数值、PM2.5含量等核心参数。在Python编程环境中,我们惯常运用pandas库来对这结构化数据进行高效的处理和分析。 1. **数据导入与初步处理**: - 利用`pandas.read_csv()`方法来导入存储为CSV格式的数据资料。 - 数据整理:对数据中的空白项、非正常数值进行修正,保证数据的精确性。 - 调整日期字段的格式,确保其能够适用于时间序列分析的需求。 2. **数据深度分析**: - 针对每个城市的AQI和PM2.5数据执行统计性描述,例如计算平均值、中位数、标准偏差等指标。 - 确定空气质量良好天气的天数,即那些AQI值低于75(依据中国的空气质量评估标准)的日数。 3. **数据呈现**: - 运用matplotlib或seaborn工具绘制折线图,直观展示四个城市在2018年全年的AQI和PM2.5变化动态。 - 可通过采用不同的颜色方案和线条型来区分不同城市的数据系列。 - 添加必要的图示元素,如日期坐标轴、城市名称标注、图表标题及图例说明,以提升图表的可读...

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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