custom-attributes

MVC3 - Where to place custom attribute classes

岁酱吖の 提交于 2019-12-22 02:00:56
问题 I am delving into custom validation attributes and am curious to know how others structure the projects. Where do you typically store custom attributes? My first thought was to simply create a new folder and be done with it. Any suggestions? 回答1: I use 2 different approaches. Set up a common Class Library to store common validation that will be used on many MVC applications. Then reference this library from your MVC application. You can use http://dataannotationsextensions.org/ to view the

TypeBuilder - Adding attributes

感情迁移 提交于 2019-12-21 23:37:06
问题 I have a helper class that uses a TypeBuilder to construct a dynamic type. It is used as follows : var tbh = new TypeBuilderHelper("MyType"); tbh.AddProperty<float>("Number", 0.0f); tbh.AddProperty<string>("String", "defaultStringValue"); tbh.Close(); var i1 = tbh.CreateInstance(); var i2 = tbh.CreateInstance(); I now want to add support for property attributes (existing attribute types, not dynamically generated types), along the lines of : public class TypeBuilderHelper { public void

How to refer android attribute layout_width from custom attribute?

岁酱吖の 提交于 2019-12-21 09:39:51
问题 I want to separate the usage of my application on xlarge devices and usage on other devices to restrict layout_width parameter by 720dp for xlarge. For this purpose I create values/attrs.xml : <?xml version="1.0" encoding="UTF-8"?> <resources> <attr name="layoutWidth" format="reference|dimension" /> </resources> with custom parameter layoutWidth to set it in android:layout_width="?layoutWidth" Furtner, I need to specify two themes.xml files for xlarge and ordinary devices in values-xlarge and

using C# Custom Attributes for exception and audit trail logging

久未见 提交于 2019-12-21 09:33:38
问题 is it possible to create a custom feature that captures exceptions made in a method that are set by a custom attribute? im planning to do something like this: [Logging(FeatureEnum.SomeFeature, IntentEnum.SomeIntent, "some comment")] public void SomeMethodThatDoesATask() { try { var doSomeAction = new LazyProcess(); doSomeAction.WhoDunnit(); } catch(Exception ex) { StaticMethodThatDoesLogging.CatchError(ex); } } Question is: How do I capture the Method name where this attribute was placed and

How to plug method parameters into custom attribute

蹲街弑〆低调 提交于 2019-12-21 07:19:14
问题 I have a custom Attribute called AuthoriseAttribute whose constructor looks like this: public AuthoriseAttribute(int userId) { .. blah } This is used with a method called GetUserDetails() like this: [Authorise(????????)] public UserDetailsDto GetUserDetails(int userId) { .. blah } At runtime, the presence of the Authorise attribute causes some authorisation code to execute which requires the ID of the user. Obviously, this can be extracted from the parameter of the GetUserDetails() method,

Can't get Custom Attribute Value in MVC3 HTML Helper

£可爱£侵袭症+ 提交于 2019-12-21 05:05:23
问题 I've extended the HTML helper with a method that needs an attribute value from the property of the model. So I've defined a custom attribute as such. public class ChangeLogFieldAttribute : Attribute { public string FieldName { get; set; } } It's used like this in my model. [Display(Name = "Style")] [ChangeLogField(FieldName = "styleid")] public string Style { get; set; } In my helper method, I've got the following code to get the FieldName value of my attribute, if the attribute is used for

How to get all descriptions of enum values with reflection?

醉酒当歌 提交于 2019-12-21 05:00:47
问题 So I need to get a List<string> from my enum Here is what I have done so far: enum definition [Flags] public enum ContractorType { [Description("Recipient")] RECIPIENT = 1, [Description("Deliver")] DELIVER = 2, [Description("Recipient / Deliver")] RECIPIENT_DELIVER = 4 } HelperClass with method to do what I need: public static class EnumUtils { public static IEnumerable<string> GetDescrptions(Type enumerator) { FieldInfo[] fi = enumerator.GetFields(); List<DescriptionAttribute> attributes =

Custom Attribute not being hit

给你一囗甜甜゛ 提交于 2019-12-20 05:05:29
问题 I've created a custom attribute which writes to the console when it's hit, however it doesn't seem to be hit. It's the microsoft tutorial (http://msdn.microsoft.com/en-us/library/sw480ze8.aspx) and is being run on 2010, .net 4. I'm guessing it must be me that's doing something wrong, but I can't see what it is. Can anyone help? This is the attribute, whose code is never being hit [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class Author : Attribute { private

Custom Attribute not being hit

◇◆丶佛笑我妖孽 提交于 2019-12-20 05:04:48
问题 I've created a custom attribute which writes to the console when it's hit, however it doesn't seem to be hit. It's the microsoft tutorial (http://msdn.microsoft.com/en-us/library/sw480ze8.aspx) and is being run on 2010, .net 4. I'm guessing it must be me that's doing something wrong, but I can't see what it is. Can anyone help? This is the attribute, whose code is never being hit [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class Author : Attribute { private

Custom Attribute not being hit

别来无恙 提交于 2019-12-20 05:04:42
问题 I've created a custom attribute which writes to the console when it's hit, however it doesn't seem to be hit. It's the microsoft tutorial (http://msdn.microsoft.com/en-us/library/sw480ze8.aspx) and is being run on 2010, .net 4. I'm guessing it must be me that's doing something wrong, but I can't see what it is. Can anyone help? This is the attribute, whose code is never being hit [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class Author : Attribute { private