0

I would like to populate the DescriptionAttribute of a property, from a string that will be initialized from an xml file. The property will be used in a propertygrid.

The main thing is getting the descriptions from an xml file. How to get it into a const string that i can then use as the DescriptionAttribute of a property.

I have tried a few things with no success so any help would be appreciated.

or is there another wey to assign the xml values to the description? A typeconverter perhaps? Just point me to the right direction please.

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; }
}
4

1 回答 1

0

DescriptionAttribute不能同时被编译和动态。

查看我对这个 SO 问题的回答,该问题演示了如何构建动态类型描述符:Optimize class for PropertyGrid

使用 DynamicTypeDescriptor 类,您可以使用所需的描述属性构建包装类。

于 2013-09-27T09:02:51.987 回答