c# - 可扩展属性仅返回计数和容量 C#
问题描述
我想将联名持有人的详细信息包括在主要账户持有人的个人资料中,多个联名持有人将在这里我使用派生自 collectiobass 类的“shareholdercolection”类,将联名持有人的详细信息分配给它。在应用程序属性的执行只返回容量和计数。为什么不叫属性描述符?
public class ShareholderCollection : CollectionBase, ICustomTypeDescriptor
{
public void Add( UiclsJointHolderSummary sh)
{
this.List.Add(sh);
}
public void Remove( UiclsJointHolderSummary sh)
{
this.List.Remove(sh);
}
public UiclsJointHolderSummary this[int index]
{
get{
return (UiclsJointHolderSummary)this.List[index];
}
set
{
base.List[index] = (UiclsJointHolderSummary)value;
}
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, noCustomTypeDesc: true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(System.Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
for (int i = 0; i < this.Count; i = i + 1)
{
ShareholderCollectionPropertyDescriptor pd = new
` `ShareholderCollectionPropertyDescriptor( this,i);
pds.Add(pd);
}
return pds;
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties(attributes);
}
public object GetPropertyOwner(PropertyDescriptor pd )
{
return this;
}
}
描述符类
public class ShareholderCollectionPropertyDescriptor : PropertyDescriptor
{
private ShareholderCollection collection;
private int index = 1;
public ShareholderCollectionPropertyDescriptor(ShareholderCollection _Coll,int _id) : base("#" + _id.ToString(), null)
{
this.collection = _Coll;
this.index = _id;
}
public override AttributeCollection Attributes
{
get
{
// var attributes = TypeDescriptor.GetAttributes(GetValue(null), false);
return new AttributeCollection(null);
//return attributes;
}
}
public override bool CanResetValue(object component)
{
return true;
}
public override Type ComponentType
{
get => this.collection.GetType();
}
public override string DisplayName
{
get
{
UiclsJointHolderSummary sh = this.collection[index];
return "[" + sh.NDX.ToString().Trim() + "] " + sh.INITIALS.ToString() + " " + sh.LASTNAME.ToString();
}
}
public override string Description
{
get
{
UiclsJointHolderSummary sh = this.collection[index];
StringBuilder sb = new StringBuilder();
sb.Append(sh.ADDRESS.ADDRESS1);
sb.Append(",");
sb.Append(sh.ADDRESS.ADDRESS2);
sb.Append(",");
sb.Append(sh.ADDRESS.ADDRESS3);
sb.Append(",");
sb.Append(sh.ADDRESS.CITY);
sb.Append(",");
sb.Append(sh.ADDRESS.PCODE);
return sb.ToString();
}
}
public override object GetValue(object component)
{
return this.collection[index];
}
public override bool IsReadOnly
{ get => false; }
//public override string Name
//{
// get { return "#" + index.ToString(); }
//}
public override Type PropertyType
{
get => this.collection[index].GetType();
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
throw new NotImplementedException();
}
public override bool ShouldSerializeValue(object component)
{
return true;
}
//private static string GetDisplayName(ShareholderCollection ilst, int ind )
//{
// UiclsJointHolderSummary sh = ilst.Item(ind);
// return "[" + sh.NDX.ToString().Trim() + "] " + sh.INITIALS.ToString() + " " + sh.LASTNAME.ToString();
// //return str=DisplayName;
//}
}
转换器类
public class ShareholderCollectionConverter: ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context , CultureInfo culture , Object pvalue , Type destType )
{
if ((destType == typeof(string) )&& (pvalue.GetType()==typeof( ShareholderCollection)))
{
return "Joint Shareholders";
}
return base.ConvertTo(context, culture, pvalue, destType);
}
}
属性类
ShareholderCollection JointHolder= new ShareholderCollection ();
[TypeConverter(typeof(ShareholderCollectionConverter))]
[Category("23. Joint Members")]
[DisplayName("10. Shareholders")]
[Browsable(true)]
[ReadOnly(true)]
public ShareholderCollection JointHolders
{
get
{
return JointHolder;
}
set { JointHolder = value; }
}
在运行时分配的属性值
clsJoinShareholderCollSub objJointShareholderCollSub = new clsJoinShareholderCollSub();
ShareholderCollection sb = objJointShareholderCollSub.getJointHolders(objUIclsFolio);
objUIclsFolio.JointHolders = sb;
pgGeneralFrmFolio.SelectedObject = objUIclsFolio;
pgGeneralFrmFolio.Refresh();
解决方案
推荐阅读
- swift - Swift - 终端/命令行
- javascript - 无法理解反应中的功能
- python - 如何使用 python wave 模块调整音高
- java - Spring boot 的 Postman Post 请求保存了整个 JSON 正文,而不仅仅是字段值。我该如何解决?
- docker - Docker 搜索找不到图像,但 docker pull 可以
- json - 通过 axios 调用/变量赋值返回未定义的 Vue 变量
- java - 如何检查用户是否从 Google/Facebook/GitHub 等注销 - OAuth2
- google-colaboratory - google colab 在本地目录中找不到文件
- laravel - 为收藏添加新价值。拉拉维尔
- c++ - Qt C++ - 如何保护文件