attributes C# reflection variable update site:stackoverflow.com
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class AttributeHelper
{
public static TValue GetPropertyAttributeValue(
Expression> propertyExpression,
Func valueSelector)
where TAttribute : Attribute
{
var expression = (MemberExpression) propertyExpression.Body;
var propertyInfo = (PropertyInfo) expression.Member;
var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
return attr != null ? valueSelector(attr) : default(TValue);
}
}
attributes C# reflection variable update site:stackoverflow.com
private static Dictionary GetAuthors()
{
return typeof(Book).GetProperties()
.SelectMany(prop => prop.GetCustomAttributes())
.OfType()
.ToDictionary(a => a.GetType().Name.Replace("Attribute", ""), a => a.Name);
}
attributes C# reflection variable update site:stackoverflow.com
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
public static class Program
{
static void Main()
{
PropertyInfo prop = typeof(Foo).GetProperty("Bar");
var vals = GetPropertyAttributes(prop);
// has: DisplayName = "abc", Browsable = false
}
public static Dictionary GetPropertyAttributes(PropertyInfo property)
{
Dictionary attribs = new Dictionary();
// look for attributes that takes one constructor argument
foreach (CustomAttributeData attribData in property.GetCustomAttributesData())
{
if(attribData.ConstructorArguments.Count == 1)
{
string typeName = attribData.Constructor.DeclaringType.Name;
if (typeName.EndsWith("Attribute")) typeName = typeName.Substring(0, typeName.Length - 9);
attribs[typeName] = attribData.ConstructorArguments[0].Value;
}
}
return attribs;
}
}
class Foo
{
[DisplayName("abc")]
[Browsable(false)]
public string Bar { get; set; }
}
attributes C# reflection variable update site:stackoverflow.com
typeof(Book)
.GetProperty("Name")
.GetCustomAttributes(false)
.ToDictionary(a => a.GetType().Name, a => a);
attributes C# reflection variable update site:stackoverflow.com
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class AttributeHelper
{
public static TValue GetPropertyAttributeValue(
Expression> propertyExpression,
Func valueSelector)
where TAttribute : Attribute
{
var expression = (MemberExpression) propertyExpression.Body;
var propertyInfo = (PropertyInfo) expression.Member;
var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
return attr != null ? valueSelector(attr) : default(TValue);
}
}
//USAGE:
var author = AttributeHelper.GetPropertyAttributeValue(prop => prop.Name, attr => attr.Author);
// author = "AuthorName"
attributes C# reflection variable update site:stackoverflow.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
private static Dictionary GetAttribute(
Func valueFunc)
where TAttribute : Attribute
{
return typeof(TType).GetProperties()
.SelectMany(p => p.GetCustomAttributes())
.OfType()
.ToDictionary(a => a.GetType().Name.Replace("Attribute", ""), valueFunc);
}
var dictionary = GetAttribute(a => a.Name);
attributes C# reflection variable update site:stackoverflow.com
var pInfo = typeof(Book).GetProperty("Name")
.GetCustomAttribute();
var name = pInfo.Name;
attributes C# reflection variable update site:stackoverflow.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
private static Dictionary GetAttribute(
Func valueFunc)
where TAttribute : Attribute
{
return typeof(TType).GetProperties()
.SelectMany(p => p.GetCustomAttributes())
.OfType()
.ToDictionary(a => a.GetType().Name.Replace("Attribute", ""), valueFunc);
}
attributes C# reflection variable update site:stackoverflow.com
var attributeData = typeof(Book).GetProperty("Name").GetCustomAttributesData();
var attributes = typeof(Book).GetProperty("Name").GetCustomAttributes(false);
attributes C# reflection variable update site:stackoverflow.com
public static dynamic GetAttribute(Type objectType, string propertyName, Type attrType)
{
//get the property
var property = objectType.GetProperty(propertyName);
//check for object relation
return property.GetCustomAttributes().FirstOrDefault(x => x.GetType() == attrType);
}
Usage:
var objectRelAttr = GetAttribute(typeof(Person), "Country", typeof(ObjectRelationAttribute));
var displayNameAttr = GetAttribute(typeof(Product), "Category", typeof(DisplayNameAttribute));
|