Get enum from enum attribute

Here’s a helper method that should point you in the right direction. protected Als GetEnumByStringValueAttribute(string value) { Type enumType = typeof(Als); foreach (Enum val in Enum.GetValues(enumType)) { FieldInfo fi = enumType.GetField(val.ToString()); StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes( typeof(StringValueAttribute), false); StringValueAttribute attr = attributes[0]; if (attr.Value == value) { return (Als)val; } } throw new ArgumentException(“The value ‘” … Read more

Using an array reference as an XML attribute for custom android view

Just going to piggyback off your question here, since your post shows up first if you google something like “array reference XML attribute custom view”, so someone might find this helpful. If you want your custom view to reference an array of strings, you can use Android’s existing android:entries XML attribute, instead of creating a … Read more

Custom attributes in Android fragments

The Link for Support4Demos is changed or can be changed so posting the complete solution. Here it goes. Create attrs.xml file in res/values folder. Or add the below content if file already exists. <?xml version=”1.0″ encoding=”utf-8″?> <resources> <declare-styleable name=”MyFragment”> <attr name=”my_string” format=”string”/> <attr name=”my_integer” format=”integer”/> </declare-styleable> Override the onInflate delegate of fragment and read attributes … Read more

What’s the difference between implementing FilterAttribute, IActionFilter and inheriting from ActionFilterAttribute in asp.net mvc 3?

Basically FilterAttribute provides the most low level behaviour of MVC Attributes and implements the IMvcFilter that provides the Order and AllowMultiple properties. ActionFilterAttribute is the basis for filtering actions and results, since is a implementation of IActionFilter, IResultFilter and inherit from FilterAttribute. Your MySecondFilterAttribute implementation leads to ActionFilterAttribute without IResultFilter capabilities (OnResultExecuting and OnResultExecuted).

Custom attribute on property – Getting type and value of attributed property

Something like the following,, this will use only the first property it comes accross that has the attribute, of course you could place it on more than one.. public object GetIDForPassedInObject(T obj) { var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1); object ret = prop !=null ? prop.GetValue(obj, null) : null; return … Read more

How do I read an attribute on a class at runtime?

public string GetDomainName<T>() { var dnAttribute = typeof(T).GetCustomAttributes( typeof(DomainNameAttribute), true ).FirstOrDefault() as DomainNameAttribute; if (dnAttribute != null) { return dnAttribute.Name; } return null; } UPDATE: This method could be further generalized to work with any attribute: public static class AttributeExtensions { public static TValue GetAttributeValue<TAttribute, TValue>( this Type type, Func<TAttribute, TValue> valueSelector) where TAttribute : … Read more

An attribute argument must be a constant expression, …- Create an attribute of type array

This supplements the information Simon already gave. I found some documentation here: Attribute parameter types: The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are: One of the following types: bool, byte, char, double, float, int, long, sbyte, short, string, uint, ulong, ushort. The type … Read more

What are the similarities and differences between Java Annotations and C# Attributes?

Control over when your metadata is made accessible is different between the two languages. Java provides the java.lang.annotation.Retention annotation and java.lang.annotation.RetentionPolicy enum to control when annotation metadata is accessible. The choices vary from Runtime (most common – annotation metadata retained in class files), to Source (metadata discarded by compiler). You tag your custom annotation interface … Read more

Lambda expression in attribute constructor

Having a generic attribute is not possible in a conventional way. However C# and VB don’t support it but the CLR does. If you want to write some IL code it’s possible. Let’s take your code: [AttributeUsage(AttributeTargets.Property)] public class RelatedPropertyAttribute: Attribute { public string RelatedProperty { get; private set; } public RelatedPropertyAttribute(string relatedProperty) { RelatedProperty … Read more