我想从一个将从xml文件初始化的字符串填充属性的DescriptionAttribute。该属性将用于propertygrid。
主要是从xml文件中获取描述。如何将它变成一个const字符串,然后我可以将其用作属性的DescriptionAttribute。
我尝试了一些没有成功的事情,所以任何帮助都会受到赞赏。
还是有另外一个我们要将xml值分配给描述吗?也许是一个变换器?请指出正确的方向。
public class1
{
string BaboonDescription = "";
string TigerDescription = "";
const string SnakeDescription = "A snake is bla bla bla";
// method that extracts the descriptions from the xml file.
public void PopulateFromXml(string xmlfile)
{
XDocument xDoc = XDocument.Load(xmlfile);
var items = from i in xDoc.Descendants("item")
select i;
foreach (var item in items)
{
switch (item.Attribute("name").Value)
{
case "Baboon":
BaboonDescription = item.Value; // Assigns BaboonDescription the description from xml.
break;
case "Tiger":
TigerDesscription = item.Value; // Assigns TigerDescription the description from xml.
break;
default:
break;
}
}
}
}
public class2 : class1
{
[Description(BaboonDescription)] // problem here. Telling me that I need const string. But i have to get the strings from an xml.
public string Baboon { get; set; }
[Description("tiger is bla bla")] // this one works but I want the description from the xml.
public string Tiger { get; set; }
[Description(SnakeDescription)] // this also works but I want the description from the xml.
public string Snake { get; set; }
}
答案 0 :(得分:0)
DescriptionAttribute无法编译并且同时处于动态状态。
查看我对此SO问题的回答,该问题演示了如何构建动态类型描述符:Optimize class for PropertyGrid
使用DynamicTypeDescriptor类,您可以构建一个包含所需描述属性的包装类。