C# 确定属性值
我有一个现有的窗口窗体应用程序。应用程序有一个属性网格。属性的值由用户在运行时设置。我想做的是从代码中确定任何给定属性的当前值。我取得了部分成功。我能够获得类别以及属性名称信息。我很难获得用户设置的属性的当前值,以及其他两个相关问题 我使用的代码如下:C# 确定属性值,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我有一个现有的窗口窗体应用程序。应用程序有一个属性网格。属性的值由用户在运行时设置。我想做的是从代码中确定任何给定属性的当前值。我取得了部分成功。我能够获得类别以及属性名称信息。我很难获得用户设置的属性的当前值,以及其他两个相关问题 我使用的代码如下: // ICustomTypeDescriptor Interface Implementation public AttributeCollection GetAttributes() { return TypeDescript
// ICustomTypeDescriptor Interface Implementation
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(GetType());
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(GetType());
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(GetType());
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(GetType());
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(GetType());
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(GetType());
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(GetType(), editorBaseType);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(GetType(), attributes);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(GetType());
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
// ... This returns a list of properties.
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(GetType(), attributes);
PropertyDescriptor[] arr = new PropertyDescriptor[pdc.Count];
pdc.CopyTo(arr, 0);
PropertyDescriptorCollection propertyCollection = new PropertyDescriptorCollection(arr);
ModifyProperties(propertyCollection); // modifies which properties are visible
// temporary code to get the program to print out the properties
foreach (PropertyDescriptor pd in propertyCollection)
{
Print("input category = "+pd.Category);
Print("input display name = "+pd.DisplayName);
Print("input name = "+pd.Name);
// Print("input value = "+pd.GetValue(Input).ToString()); <--- Does NOT work
}
return propertyCollection;
}
public PropertyDescriptorCollection GetProperties()
{
return TypeDescriptor.GetProperties(GetType());
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
//ICustomTypeDescriptor接口实现
公共属性集合GetAttributes()
{
返回TypeDescriptor.GetAttributes(GetType());
}
公共字符串GetClassName()
{
返回TypeDescriptor.GetClassName(GetType());
}
公共字符串GetComponentName()
{
返回TypeDescriptor.GetComponentName(GetType());
}
公共类型转换器GetConverter()
{
返回TypeDescriptor.GetConverter(GetType());
}
公共事件描述符GetDefaultEvent()
{
返回TypeDescriptor.GetDefaultEvent(GetType());
}
公共属性描述程序GetDefaultProperty()
{
返回TypeDescriptor.GetDefaultProperty(GetType());
}
公共对象GetEditor(类型editorBaseType)
{
返回TypeDescriptor.GetEditor(GetType(),editorBaseType);
}
公共事件描述符集合GetEvents(属性[]属性)
{
返回TypeDescriptor.GetEvents(GetType(),attributes);
}
公共事件描述符集合GetEvents()
{
返回TypeDescriptor.GetEvents(GetType());
}
公共属性DescriptorCollection GetProperties(属性[]属性)
{
//…这将返回属性列表。
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(GetType(),attributes);
PropertyDescriptor[]arr=新的PropertyDescriptor[pdc.Count];
pdc.CopyTo(arr,0);
PropertyDescriptorCollection propertyCollection=新的PropertyDescriptorCollection(arr);
ModifyProperties(propertyCollection);//修改可见的属性
//使程序打印出属性的临时代码
foreach(propertyCollection中的PropertyDescriptor pd)
{
打印(“输入类别=”+pd.类别);
打印(“输入显示名称=“+pd.DisplayName”);
打印(“输入名称=”+pd.name);
//打印(“input value=“+pd.GetValue(input.ToString());可能是这样
foreach (PropertyDescriptor pd in propertyCollection)
{
Print("input category = "+pd.Category);
Print("input display name = "+pd.DisplayName);
Print("input name = "+pd.Name);
Print("input value = " + pd.GetValue(GetPropertyOwner(pd)));
}
如果您有自定义对象,希望从中获得一个漂亮的字符串,可以添加一个助手方法,如下所示:
private string GetPropertyValue(PropertyDescriptor pd)
{
var property = GetPropertyOwner(pd);
if (property is CustomObject)
{
var dataSeries = property as CustomObject;
// This will return a string of the list contents ("One, Two, Three")
return string.Join(",", dataSeries.ListProperty.ToArray());
}
else if (property is ....)
{
return somthing else
}
return property.ToString();
}
演示类:
public class CustomObject
{
private List<string> _listProperty = new List<string>(new string[]{"One","Two","Three"});
public List<string> ListProperty
{
get { return _listProperty; }
set { _listProperty = value; }
}
}
公共类CustomObject
{
私有列表_listProperty=新列表(新字符串[]{“一”、“二”、“三”});
公共列表列表属性
{
获取{return\u listProperty;}
设置{u listProperty=value;}
}
}
编辑以使用您的“GetPropertyOwner”函数。非常感谢。这在几乎所有属性上都非常有效。但是,我的一个属性是一个数据系列,在这种情况下它不起作用。它没有给我用户输入的系列的名称。它只是返回字符串DataSeriesHelper。有什么建议吗?还有,有没有建议对属性重新进行自定义排序es?再次非常感谢。GetValue返回对象的值,不是所有对象都有一个pretty.ToString(),如果这些是自定义对象,则可以重写.ToString()返回您想要的内容。或者您可以添加一个helper方法,为列表、数组等属性创建一个漂亮的字符串。您非常有帮助。非常感谢。请告诉我如何执行此操作。input series属性当前显示TF 12-12(5分钟)但是返回的值是DataSeriesHelper。我不知道如何从返回的值中重新设置它。我可能缺少基本反射的这种新型方法中的内容,但既然代码中没有实例,它怎么会有值?你肯定想要对象的PropertyDescriptionCollection,而不是它的类型?