EDIT: I was able to get what I needed via this code, which pulled the list of buddy classes associated to my type - t is the type of my non-buddy class.
MetadataTypeAttribute[] metaAttr = (MetadataTypeAttribute[])t.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
Question is in the commenting in code as well -
I have a custom attribute that is being applied to a buddy class (I'm using EF-DB first). However, when I try to get the memberinfo, I don't see the custom attribute. How do I pull the value of this attribute, using an expression like below?
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication1
{
// I have a custom attribute...
[System.AttributeUsage(System.AttributeTargets.Property)]
public class ExportNameAttribute : System.Attribute
{
public string DisplayName;
public ExportNameAttribute(string displayName)
{
DisplayName = displayName;
}
}
// And I have a class with a metadata buddy class (to simulate how I need to do this with EF-DB first)
[MetadataType(typeof(AttributeTestMetaData))]
public partial class AttributeTest
{
public string myAttribute { get; set; }
}
public class AttributeTestMetaData
{
[ExportName("test")]
public string myAttribute { get; set; }
}
// However, when I pull the member info for this property via an expression, I don't get the attribute back.
class Program
{
static void Main(string[] args)
{
var mInfo = GetMemberInfo((AttributeTest at) => at.myAttribute);
Console.WriteLine(mInfo.CustomAttributes.Count().ToString()); // Outputs 0
}
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression)
{
var member = expression.Body as MemberExpression;
if (member != null)
return member.Member;
throw new ArgumentException("Expression is not a member access", "expression");
}
}
}
I figured out how to do this:
MetadataTypeAttribute[] metaAttr = (MetadataTypeAttribute[])t.GetCustomAttributes(typeof(MetadataTypeAttribute), true);