カスタム属性を作成してCore.dllに配置しました。PropertyInfo他のDLLからGetCustomAtributes
public class DBColumnReference : Attribute
{
string m_column;
public string ColumnName {
get { return m_column; }
}
public DBColumnReference(string column)
{
m_column = column;
}
}
次に、Core.dllを参照している自分のアプリケーションを作成しました。
私のアプリに独自のオブジェクトを作成し、いくつかのプロパティでCore.dllのカスタム属性を使用します。
public class TestingObject4
{
string m_table = "TESTING_CORE_OBJECT4";
public string Table
{
get { return m_table; }
}
private int m_id = 0;
[DBColumnReference("ID")]
public int Id
{
get { return m_id; }
set { m_id = value; }
}
私はコアメソッド "FilterProperties(typeof(TestingObject4))"をコールします。これは属性でフィルタプロパティを指定します。
private static Dictionary<string, PropertyInfo> FilterProperties(Type type)
{
Dictionary<string, PropertyInfo> result = new Dictionary<string, PropertyInfo>();
if(type == null)
return result;
PropertyInfo[] properties = type.GetProperties();
foreach(PropertyInfo prop in properties)
{
// Attribute[] atributes = Attribute.GetCustomAttributes(prop, true);
object[] atributes = prop.GetCustomAttributes(typeof(DBColumnReference), true);
if(atributes != null && atributes.Length != 0)
{
DBColumnReference reference = atributes[0] as DBColumnReference;
result.Add(reference.ColumnName, prop);
}
}
return result;
}
Attributes[]
属性は常に空です。どのように属性を正しく取得できますか?
あなたがから属性を取得しているタイプを投稿してもらえますか? – Xharze
質問に追加されました。 –
タイプ( 'object [] atributes = prop.GetCustomAttributes(true);')を指定せずに 'GetCustomAttributes()'を試してみて、どうなるか見てみてください。 –