如何动态的生成一个实现 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);
}
}
...全文
204 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
  • 打赏
  • 举报
回复
没有人回答吗?请教各位大哥啊

8,735

社区成员

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

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