tags:

views:

146

answers:

2

Hi,

I had asked this question http://stackoverflow.com/questions/2442149/adding-more-attributes-to-linq-to-sql-entity

Now, when I add Browsable attribute to generated entity in designer,it works.But,when I use the MetaDataType approach,and add Browsable attribute in partial class,it does not work

"I added a MetaDataType class, and added browsable attribute to property,but seems to have no effect"

+2  A: 

Adding the MetadataTypeAttribute will only be useful when you have written custom code that detects the BrowsableAttribute. The .NET framework doesn't handle MetadataTypeAttribute any differently than any other attribute and doesn't 'merge' your type with the meta data type.

When you have written your own code that detects the BrowsableAttribute, you can change it, so it also detects a MetadataTypeAttribute on a type and if it exists, you can go to the referred metadata class to search for properties decorated with the BrowsableAttribute. When the logic using the BrowsableAttribute has not been written by you (for instance, this is part of the .NET framework, because it is used by the Visual Studio designer), there is no way of getting this to work.

Currently there are only a few parts of the .NET framework that know about the MetadataTypeAttribute. MVC for instance uses it for validation and with .NET 4.0 DataAnnotations (that defines the attribute) also has a validator. Enterprise Library 5.0 (currently in beta) will also detect this attribute for validation.

While more and more applications and part of the framework might be able to handle this attribute, in most situations using it is useless.

Steven
A: 

I'm using it so that I can allow my Linq-To-SQL classes to also have Json properties to ease deserialization of Json objects:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel.DataAnnotations; 

[MetadataType(typeof(User_JsonProperties))] 
public partial class User 
{} 

public class User_JsonProperties

{ 
   [JsonProperty("user_id")] 
   public int UserId { get; set; } 
}

Since the other author didn't include source code, I thought I would so that you'd see what it looks like.

George Stocker