C# 如何动态申明变量 和 PROPERTYGRID控件如何动态给属性赋值 (谢谢)

Lixin19821010 2007-12-04 03:19:24
1. private static ReporterGrid reporterGridA1 = new ReporterGrid();
private static ReporterGrid reporterGridA2 = new ReporterGrid();
private static ReporterGrid reporterGridA3 = new ReporterGrid();
private static ReporterGrid reporterGridA4 = new ReporterGrid();
private static ReporterGrid reporterGridA5 = new ReporterGrid();
private static ReporterGrid reporterGridA6 = new ReporterGrid();
private static ReporterGrid reporterGridA7 = new ReporterGrid();
private static ReporterGrid reporterGridA8 = new ReporterGrid();
private static ReporterGrid reporterGridA9 = new ReporterGrid();
private static ReporterGrid reporterGridA10 = new ReporterGrid();
private static ReporterGrid reporterGridA11 = new ReporterGrid();
private static ReporterGrid reporterGridA12 = new ReporterGrid();
private static ReporterGrid reporterGridA13 = new ReporterGrid();
private static ReporterGrid reporterGridA14 = new ReporterGrid();
private static ReporterGrid reporterGridA15 = new ReporterGrid();
private static ReporterGrid reporterGridA16 = new ReporterGrid();
private ReporterGrid[] _Members1 = new ReporterGrid[] { reporterGridA1, reporterGridA2, reporterGridA3, reporterGridA4, reporterGridA5, reporterGridA6, reporterGridA7, reporterGridA8, reporterGridA9, reporterGridA10, reporterGridA11, reporterGridA12, reporterGridA13, reporterGridA14, reporterGridA15, reporterGridA16 };
正如大家所看到的,这个代码实在是太垃圾了.比如有一个ArrayList arrayList = new ArrayList();arrayList有16个元素,如何根据这个arrayList的容量大小来动态申明上面的16个ReporterGrid,并且放入_Members1数组内.

2. [Category("属性配置(主要原料成本表.其他原料成本表.经营业务利润明细表)"), DisplayName("主要原料成本预算表")]
正入大家所看到的,这里的Category, 和 DisplayName属性都是写死的,琢磨了一段时间也不知道怎么把这些弄成变量的形式.请各位专家给予指导

3. [Category("属性配置(主要产品产销表.其他产品产销表.主营业务成本明细表)"), DisplayName("PCP,一、精炼石油产品")]
public ReporterGrid[] Members1
{
get
{
return _Members1;
}
set
{
_Members1 = value;
}
}

[Category("属性配置(主要产品产销表.其他产品产销表.主营业务成本明细表)"), DisplayName("CET,二、化工原料类小计")]
public ReporterGrid[] Members2
{
get
{
return _Members2;
}
set
{
_Members2 = value;
}
}
如上所表示,这是两个PROPERTYGRID的数据集,_Members1的申明为第一个问题上面的申明所表示,_Members2为另一大堆同样的申明,但其实这两个我是可以合并成一个的,如何可以做到呢.谢谢
...全文
698 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganju 2008-07-30
  • 打赏
  • 举报
回复
MARK
Lixin19821010 2007-12-07
  • 打赏
  • 举报
回复
十分感谢NOAHFANG的回复,明天来看看,试运行一下,仔细研究一下.十分感谢.
noahfang 2007-12-06
  • 打赏
  • 举报
回复
动态改变Category和DisplayName
在你的类中添加两个public的泛型Dictionary<string,string>:categories存放每个属性的类别,displayNames存放每个属性的DisplayName。

继承PropertyDescriptor创建一个MyPropertyDescriptor类:

public class MyPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor basePropertyDescriptor;
private string category;
private string displayName;

public MyPropertyDescriptor(
PropertyDescriptor basePropertyDescriptor, string cat,string disName
) : base(basePropertyDescriptor)
{
this.basePropertyDescriptor = basePropertyDescriptor;
this.category=cat;
this.displayName=disName;
}

public override bool CanResetValue(object component)
{
return basePropertyDescriptor.CanResetValue(component);
}

public override Type ComponentType
{
get { return basePropertyDescriptor.ComponentType; }
}

//修改DisplayName
public override string DisplayName
{
get
{
return displayName;
}
}

public override string Description
{
get
{
return basePropertyDescriptor.Description;
}
}

//修改类别
public override string Category
{
get
{
return category;
}
}

public override object GetValue(object component)
{
return this.basePropertyDescriptor.GetValue(component);
}

public override bool IsReadOnly
{
get {
return basePropertyDescriptor.IsReadOnly;
}
}

public override bool IsBrowsable
{
get {
return basePropertyDescriptor.IsBrowsable;
}
}

public override string Name
{
get { return this.basePropertyDescriptor.Name; }
}

public override Type PropertyType
{
get { return this.basePropertyDescriptor.PropertyType; }
}

public override void ResetValue(object component)
{
this.basePropertyDescriptor.ResetValue(component);
}

public override bool ShouldSerializeValue(object component)
{
return this.basePropertyDescriptor.ShouldSerializeValue(component);
}

public override void SetValue(object component, object value)
{
this.basePropertyDescriptor.SetValue(component, value);
}
}
把要显示的Category和DisplayName作为构造函数的参数传入,改写Category和DisplayName属性让它们返回相应的值,其他的属性和方法保持缺省状态。

继承TypeConverter创建一个MyTypeConverter类:
class MyConverter : TypeConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value);
PropertyDescriptorCollection newpdc = new PropertyDescriptorCollection(null);
YourClass myObject=(YourClass )value;

foreach (PropertyDescriptor pd in pdc)
{
if (myObject.categories.ContainsKey(pd.Name))
{
MyPropertyDescriptor newpd = new MyPropertyDescriptor(pd, myObject.categories[pd.Name], myObject.displayNames[pd.Name]);
newpdc.Add(newpd);
}
else
newpdc.Add(pd);
}
return newpdc;
}
把MyTypeConverter应用到你的类上

[TypeConverter(typeof(MyConverter))]
class YourClass
.....

然后在你的代码中给categories和displayName添加值,如果对象已经赋给PropertyGrid,还要刷新PropertyGrid
vwxyzh 2007-12-05
  • 打赏
  • 举报
回复
“并且不使用CODEDOM.谢谢”

那就用Emit :)
Lixin19821010 2007-12-05
  • 打赏
  • 举报
回复
upup
Lixin19821010 2007-12-04
  • 打赏
  • 举报
回复
第一个问题,琢磨了一会已经解决了,因为有时候上班,头都大了,就在那乱写了,然后也就糊涂了,整个人.谢谢大家关注.
如果大家有谁对其他问题感兴趣,请不吝赐教 :)
Lixin19821010 2007-12-04
  • 打赏
  • 举报
回复
对于问题1,就是如何用一两个循环,做到上面的申明.现在是最好不要写死了,因为帖子贴出来的只是一小部分,有的需要实例化一百多个,我就要实例化一百多次,申明一百多个变量,哪里有这样写程序的呀.尽管我已经这样写了...但是我还是想找到一个好的方法.谢谢
lovvver 2007-12-04
  • 打赏
  • 举报
回复
声明可以使用动态,但是后面的属性描述,以及属性定义,是不能做成动态的。
如果静态可以实现,就直接写死得了,用静态应该会更快。
Lixin19821010 2007-12-04
  • 打赏
  • 举报
回复
并且不使用CODEDOM.谢谢
Lixin19821010 2007-12-04
  • 打赏
  • 举报
回复
开发环境为VS2005, C#

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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