custom-attributes

HTML5 custom attributes - Why would I use them?

只谈情不闲聊 提交于 2019-11-27 22:54:51
I can't seem to understand why I should be happy with HTML5 allowing custom attributes? Why would I use them? zzzzBov I assume you're referencing the HTML5 [data-*] attributes. The advantage is that you can easily associate some scripting data (still semantic, but not for display) with your elements without having to insert inline javascript all over the place, and it will be valid HTML5. To do the same thing in HTML4 would require specifying a custom namespace, and add some namespaced attributes. Say you've got a list of items for sale, you may want to store the numeric price without trying

get all types in assembly with custom attribute

心已入冬 提交于 2019-11-27 21:08:05
Is there an elegant way to get all the types in an assembly that have a custom attribute? So if I have a class [Findable] public class MyFindableClass {} I would like to be able to find it in a collection of types returned by Assembly.GetTypes(...) I can do it with a big vile hack, but I'm sure someone has a nicer way. I wouldn't think you can dodge enumerating every type in the assembly, checking for the attribute, but you could use LINQ to make the query easier to understand: Assembly assembly = ... var types = from type in assembly.GetTypes() where Attribute.IsDefined(type, typeof

How to get Custom Attribute values for enums?

一曲冷凌霜 提交于 2019-11-27 21:01:35
I have an enum where each member has a custom attribute applied to it. How can I retrieve the value stored in each attribute? Right now I do this: var attributes = typeof ( EffectType ).GetCustomAttributes ( false ); foreach ( object attribute in attributes ) { GPUShaderAttribute attr = ( GPUShaderAttribute ) attribute; if ( attr != null ) return attr.GPUShader; } return 0; Another issue is, if it's not found, what should I return? 0 is implcity convertible to any enum, right? That's why I returned that. Forgot to mention, the above code returns 0 for every enum member. It is a bit messy to do

How to put conditional Required Attribute into class property to work with WEB API?

牧云@^-^@ 提交于 2019-11-27 20:08:56
I just want to put conditional Required Attribute which is work with WEB API Example public sealed class EmployeeModel { [Required] public int CategoryId{ get; set; } public string Email{ get; set; } // If CategoryId == 1 then it is required } I am using Model State validation via ( ActionFilterAttribute ) You can implement your own ValidationAttribute . Perhaps something like this: public class RequireWhenCategoryAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var employee = (EmployeeModel) validationContext

How to get a custom attribute from object instance in C#

孤街浪徒 提交于 2019-11-27 19:46:26
Let's say I have a class called Test with one property called Title with a custom attribute: public class Test { [DatabaseField("title")] public string Title { get; set; } } And an extension method called DbField. I am wondering if getting a custom attribute from an object instance is even possible in c#. Test t = new Test(); string fieldName = t.Title.DbField(); //fieldName will equal "title", the same name passed into the attribute above Can this be done? Here is an approach. The extension method works, but it's not quite as easy. I create an expression and then retrieve the custom attribute

Real world use of custom .NET attributes

萝らか妹 提交于 2019-11-27 16:34:28
问题 What kind of things have you used custom .NET attributes for in the real world? I've read several articles about them, but I have never used custom attributes. I feel like I might be overlooking them when they could be useful. I am talking about attributes that you create, not ones that are already included in the framework. 回答1: I've used them "custom" attributes for validation (ie. marking a field to be validated with my own "credit card validation") and custom LinqToLucene analyzers I've

Change custom attribute's parameter at runtime

放肆的年华 提交于 2019-11-27 15:30:57
I need change attribute’s paramater during runtime. I simplified my problem to simple example. Attribute class: [AttributeUsage(AttributeTargets.Property)] public class MyAttribute : Attribute { public string Name { get; set; } } Simple entity wich has decorated properties with attributes: public class MyEntity { [MyAttribute(Name="OldValue1")] public string Data1{ get; set; } [MyAttribute(Name = "OldValue2")] public string Data2 { get; set; } } I created instace of class MyEntity. I can change value of object’s properties, but I can’t change value of attribute’s property Name on object entity

Getting the type of a MemberInfo with reflection

荒凉一梦 提交于 2019-11-27 13:36:35
问题 I'm using reflection to load a treeview with the class structure of a project. Each of the members in a class have a custom attribute assigned to them. I don't have a problem getting the attributes for a class using MemberInfo.GetCustomAttributes() however I need a way of working out if a class member is a custom class and then needs parsing itself to return the custom attributes. So far, my code is: MemberInfo[] membersInfo = typeof(Project).GetProperties(); foreach (MemberInfo memberInfo in

Find methods that have custom attribute using reflection

穿精又带淫゛_ 提交于 2019-11-27 12:17:56
I have a custom attribute: public class MenuItemAttribute : Attribute { } and a class with a few methods: public class HelloWorld { [MenuItemAttribute] public void Shout() { } [MenuItemAttribute] public void Cry() { } public void RunLikeHell() { } } How can I get only the methods that are decorated with the custom attribute? So far, I have this: string assemblyName = fileInfo.FullName; byte[] assemblyBytes = File.ReadAllBytes(assemblyName); Assembly assembly = Assembly.Load(assemblyBytes); foreach (Type type in assembly.GetTypes()) { System.Attribute[] attributes = System.Attribute

Using action parameters in custom Authorization Attribute in ASP.NET MVC3

我们两清 提交于 2019-11-27 11:47:49
问题 I have a controller which should only request authorization when loaded with specific parameters. Like when the parameter ID is 8 for example. I came up with using a custom validation attribute like this: public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { if (/* Action's inputparameter ID = 8 */) { return base.AuthorizeCore(httpContext); } return true; } } My action looks like this (not that it is interesting)