- リフレクションを使用して型のプロパティを取得する
Type.GetProperties()
PropertyInfo
次に、カスタム属性XmlElementAttribute
をそれぞれ検索できますPropertyInfo.GetCustomAttribute
- 属性が見つかった (つまり、null でない) 場合は、単純にその内容をクエリして、一致するかどうかを確認できます。
- 残りのプロパティについて、手順 2 と 3 を繰り返します。
プログラム例:
(LINQ と拡張メソッドで最適化)
using System;
using System.Linq;
using System.Reflection;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string propName = FindPropertyNameByXmlElementAttributeElementName(typeof (MyClass), "Foo");
Console.WriteLine(propName);
Console.ReadKey();
}
static string FindPropertyNameByXmlElementAttributeElementName(Type type, string elementName)
{
PropertyInfo propertyInfo =
type.GetProperties().SingleOrDefault(
prop => prop.HasAttributeWithValue<XmlElementAttribute>(
a => a.ElementName == elementName
)
);
if (propertyInfo == null)
{
return "NOT FOUND";
}
return propertyInfo.Name;
}
}
public static class PropertyInfoExtensions
{
public static bool HasAttributeWithValue<TAttribute>(this PropertyInfo pi, Func<TAttribute, bool> hasValue)
{
TAttribute attribute =
(TAttribute)pi.GetCustomAttributes(typeof(TAttribute), true).SingleOrDefault();
if (attribute == null)
{
return false;
}
return hasValue(attribute);
}
}
class MyClass
{
[XmlElement(ElementName = "Foo", Form = XmlSchemaForm.None)]
public string Rumplestiltskin { get; set; }
}
}