3

I have a LINQ generated class called Project.

I wanted to add some attributes to the generated properties, so I defined a partial class with the MetadataType attribute.

[MetadataType(typeof(Project_HiddenProps))]
public partial class Project : IProject
{
    // There are also a few additional properties defined here.
}

public class Project_HiddenProps
{
    [HiddenColumn]
    public System.Guid Id { get; set; } 
    // Id is an auto-generated property that I've added a custom attribute to
}

Later, I use reflection to try to get the attributes of this property.

var customAttributes = prop.GetCustomAttributes(false);

I only get one attribute, though, of type System.Data.Linq.Mapping.ColumnAttribute. My HiddenColumn attribute is not included in the collection. My question is how to get at these metadata properties using reflection.

Any help would be greatly appreciated.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
  • Are you using something like LINQ to SQL or LINQ to Entities? – Joshua Rodgers Oct 12 '11 at 15:05
  • I've found an answer for your problem (and mine) here: http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class – Efekt Sep 30 '13 at 07:20

1 Answers1

6

You can reflect the other class, using a convention approach:

var type = Type.GetType(entityType.FullName + "_HiddenProps," + entityType.AssemblyQualifiedName);
type.GetProperty("Id").GetCustomAttributes(false);

The buddy class cannot be automatically merged with the core component. That is only used for internal framework metadata (assume this is MVC, or maybe dynamic data?)

As @CyanLite mentioned in the comments, you can use the Meta class from the metadata attribute described here (the link Cyan added).

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • It's dynamic data, but with web forms. – Jeremy Wiggins Oct 12 '11 at 15:12
  • OK. The result will still be the same unfortunately though. – Brian Mains Oct 12 '11 at 15:51
  • Trying to use this, but getting an exception of "The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)" – Jeremy Wiggins Oct 12 '11 at 15:54
  • Then I was probably wrong when I said: entityType.AssemblyQualifiedName, essentially define the name of the assembly here, so it generates a string like: "type,assemblyname", as in: "MyNS.Project_HiddenProps,MyAssembly" – Brian Mains Oct 12 '11 at 15:56
  • Got it - instead of entityType.AssemblyQualifiedName it's entityType.Assembly.FullName. Thanks for the help! – Jeremy Wiggins Oct 12 '11 at 16:07
  • This answer is WRONG. You can dynamically fetch the Meta classes with the MetadataAttribute. See http://stackoverflow.com/a/2364766/613620 – Tim P. Feb 26 '13 at 22:22
  • @CyanLite It's not wrong; it's a perfectly viable convention-based solution, and does a very similar function as the link you provided. Your link works great too; I added it to the post with your credit. – Brian Mains Feb 27 '13 at 02:55