xml ファイルから初期化される文字列から、プロパティの DescriptionAttribute を設定したいと思います。プロパティはプロパティ グリッドで使用されます。
主なことは、xml ファイルから説明を取得することです。プロパティの DescriptionAttribute として使用できる const 文字列に取得する方法。
私はいくつかのことを試しましたが成功しませんでしたので、助けていただければ幸いです。
または、説明に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; }
}