方法一:
///舉例:比如某個實體類TestEntity中有自增列(IdentityColumnInfo)這麼一個屬性,該自增列對應的實體類如下:/// 實體屬性處理 /// public class PropertyHandle { #region 反射控制只讀、可見屬性 //SetPropertyVisibility(obj, 名稱 , true); //obj指的就是你的SelectObject, “名稱”是你SelectObject的一個屬性 //當然,調用這兩個方法後,重新SelectObject一下,就可以了。 ////// 通過反射控制屬性是否只讀 /// /// /// /// public static void SetPropertyReadOnly(object obj, string propertyName, bool readOnly) { Type type = typeof(ReadOnlyAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); AttributeCollection attrs = props[propertyName].Attributes; FieldInfo fld = type.GetField(isReadOnly, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance); fld.SetValue(attrs[type], readOnly); } ////// 通過反射控制屬性是否可見 /// /// /// /// public static void SetPropertyVisibility(object obj, string propertyName, bool visible) { Type type = typeof(BrowsableAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); AttributeCollection attrs = props[propertyName].Attributes; FieldInfo fld = type.GetField(browsable, BindingFlags.Instance | BindingFlags.NonPublic); fld.SetValue(attrs[type], visible); } #endregion }
public class IdentityColumnEntity { private bool isIncrementColumn; ///實體類TestEntity代碼如下:/// 是否是自增列 /// [Browsable(true)] [Category(基本)] [DisplayName(是否是自增列)] [ReadOnly(false)] [DefaultValue(false)] public bool IsIncrementColumn { set { isIncrementColumn = value; } get { return isIncrementColumn; } } private Int64 identityIncrement; ////// 標識增量 /// [Browsable(true)] [Category(基本)] [DisplayName(標識增量)] [ReadOnly(false)] [Description(標識增量屬性指定在 Microsoft SQL Server 為插入的行生成標識值時,在現有的最大行標識值基礎上所加的值。標識增量必須是 非零 整數,位數等於或小於 10。)] public Int64 IdentityIncrement { set { identityIncrement = value; } get { return identityIncrement; } } private Int64 ident_Seed; ////// 標識種子 /// [Browsable(true)] [Category(基本)] [DisplayName(標識種子)] [ReadOnly(false)] [Description(指示標識列的初始行值。標識種子必須是 整數,位數等於或小於 10。)] public Int64 Ident_Seed { set { ident_Seed = value; } get { return ident_Seed; } } }
public class TestEntity { private IdentityColumnEntity identityColumnInfo; ///通過 PropertyHandle.SetPropertyReadOnly即可實現,效果如下:/// 是否自增列 /// [Category(擴展信息)] [DisplayName(自增列信息)] [ReadOnlyAttribute(true)] [XmlIgnore] [Browsable(true)] //[XmlAttribute] public IdentityColumnEntity IdentityColumnInfo { get { if (identityColumnInfo != null && identityColumnInfo.IsIncrementColumn) { PropertyHandle.SetPropertyReadOnly(identityColumnInfo, Ident_Seed, true); PropertyHandle.SetPropertyReadOnly(identityColumnInfo, IdentityIncrement, true); PropertyHandle.SetPropertyReadOnly(identityColumnInfo, IsIncrementColumn, true); PropertyHandle.SetPropertyReadOnly(this, IsInsert, true); PropertyHandle.SetPropertyReadOnly(this, IsUpdate, true); } return identityColumnInfo; } set { identityColumnInfo = value; } } ////// 是否插入 /// [Category(維護信息)] [DisplayName(是否插入)] [ReadOnlyAttribute(false)] [XmlAttribute] public bool IsInsert { get ; set ; } ////// 是否更新 /// [Category(維護信息)] [DisplayName(是否更新)] [ReadOnlyAttribute(false)] [XmlAttribute] public bool IsUpdate { get ; set ; } }