2

Having this simple class:

public class Book { [DataMember] [Column("Bok_Name")] [Author("AuthorName")] public string Name{ get; set; } [DataMember] [Column("Bok_Publisher")] public string Publisher{ get; set; } } 

How can i can get the PropertyInfo from a property knowing that the attribute type is Column and the value is Bok_Name. I has trying to do this with a linq query.

3
  • I didn't get your question? What are you trying to get? Can you post any example of what you want to achieve? Commented Oct 1, 2014 at 23:20
  • I think first, access all the properties and their attributes , then compare the attributes with what you want Commented Oct 1, 2014 at 23:21
  • @Ehsan. Something like PropertyInfo pInfo = t.GetProperties().Where(a => a.GetCustomAttribute(ColumnAttribute).Value == "Bok_Name");. Commented Oct 1, 2014 at 23:22

3 Answers 3

4

Using reflection and the .NET extension CustomAttributeExtensions.GetCustomAttribute{T} method, you can find the properties with the custom attribute. In this case, the custom attribute is from System.ComponentModel.DataAnnotations.Schema.ColumnAttribute.

var book = new Book { Name = "Jitterbug Perfume" }; PropertyInfo bokName = typeof(Book) .GetProperties(BindingFlags.Public | BindingFlags.Instance) // add other bindings if needed .FirstOrDefault(x => x.GetCustomAttribute<ColumnAttribute>() != null && x.GetCustomAttribute<ColumnAttribute>().Name.Equals("Bok_Name", StringComparison.OrdinalIgnoreCase)); // the above query only gets the first property with Column attribute equal to "Bok_Name" // if there are more than one, then use a .Where clause instead of FirstOrDefault. if (bokName != null) { string name = bokName.GetValue(book).ToString(); // do other stuff } 
Sign up to request clarification or add additional context in comments.

Comments

2
 var type = typeof (Book); //or var type=instance.GetType(); var res=type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Where( p => (p.GetCustomAttribute<ColumnAttribute>() ?? new ColumnAttribute()).Value == "Bok_Name"); 

1 Comment

One should also note that GetCustomAttribute is an extension method, so you need to add using System.Reflection; for this to work.
0

In the piggy bank, variant on VB NET:

Dim maxLength = ValToInt(document.GetDataAnnotationPropertyAttributeValue(fieldName, GetType(MaxLengthAttribute))) Public Function GetDataAnnotationPropertyAttributeValue(propertyName As String, attributeType As Type) As Object Dim properties = Me.GetType().GetProperties() Dim propertyInfo = properties.FirstOrDefault(Function(p) p.Name?.ToLower() = propertyName?.ToLower()) If propertyInfo Is Nothing Then Return Nothing Dim attribute = propertyInfo.CustomAttributes.FirstOrDefault(Function(a) a.AttributeType.Equals(attributeType)) Return attribute?.ConstructorArguments(0).Value End Function 

3 Comments

But this question is for C#
Ok, sry. I was looking for a solution on VB.Net, this article helped me, so I left my version here. Because the VB.Net community is too small. Hope this helps someone like me.
In scenarios like that, it makes sense to post a new question for VB, then answer your own question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.