custom-attributes

HTML5 custom attributes - Why would I use them?

纵然是瞬间 提交于 2019-11-26 23:14:16
问题 I can't seem to understand why I should be happy with HTML5 allowing custom attributes? Why would I use them? 回答1: 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

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

♀尐吖头ヾ 提交于 2019-11-26 22:16:20
问题 I have the following custom attribute, which can be applied on properties: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { } For example: public class MyClass { [Identifier()] public string Name { get; set; } public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } } There will also be other classes, to which the Identifier attribute could be added to properties of different type: public class

ASP.NET MVC 4 custom Authorize attribute - How to redirect unauthorized users to error page? [duplicate]

风流意气都作罢 提交于 2019-11-26 22:14:42
This question already has an answer here: ASP.NET MVC - How to show unauthorized error on login page? 7 answers I'm using a custom authorize attribute to authorize users' access based on their permission levels. I need to redirect unauthorized users (eg. user tries to delete an invoice without Delete acess level) to access denied page. The custom attribute is working. But in a case of unauthorized user access, nothing shown in the browser. Contoller Code. public class InvoiceController : Controller { [AuthorizeUser(AccessLevel = "Create")] public ActionResult CreateNewInvoice() { //... return

Is it possible to have a delegate as attribute parameter?

牧云@^-^@ 提交于 2019-11-26 22:10:27
Is it possible to have a delegate as the parameter of an attribute? Like this: public delegate IPropertySet ConnectionPropertiesDelegate(); public static class TestDelegate { public static IPropertySet GetConnection() { return new PropertySetClass(); } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)] public class WorkspaceAttribute : Attribute { public ConnectionPropertiesDelegate ConnectionDelegate { get; set; } public WorkspaceAttribute(ConnectionPropertiesDelegate connectionDelegate) { ConnectionDelegate = connectionDelegate; } }

named parameter type constraints

*爱你&永不变心* 提交于 2019-11-26 22:02:41
问题 I am designing a custom attribute class. public class MyAttr: Attribute { public ValueRange ValRange { get; set; } } Then I am attempting to assign this attribute to a property in an adjoining class: public class Foo { [MyAttr(ValRange= new ValueRange())] public string Prop { get; set; } } However, the compiler is complaining the following: 'ValRange' is not a valid named attribute argument because it is not a valid attribute parameter type I also tried converting the ValueRange class to a

How to get Custom Attribute values for enums?

北城余情 提交于 2019-11-26 20:30:55
问题 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

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

假如想象 提交于 2019-11-26 20:01:22
问题 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? 回答1: Here is an approach. The

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

坚强是说给别人听的谎言 提交于 2019-11-26 18:57:49
Here is my custom attribute and a class I'm using it on: [MethodAttribute(new []{new MethodAttributeMembers(), new MethodAttributeMembers()})] public class JN_Country { } public class MethodAttribute : Attribute { public MethodAttributeMembers[] MethodAttributeMembers { get; set; } public MethodAttribute(MethodAttributeMembers[] methodAttributeMemberses) { MethodAttributeMembers = methodAttributeMemberses; } } public class MethodAttributeMembers { public string MethodName { get; set; } public string Method { get; set; } public string MethodTitle { get; set; } } The syntax error, displayed on

How to create a custom attribute in C#

浪尽此生 提交于 2019-11-26 17:02:34
I have tried lots of times but still I am not able to understand the usage of custom attributes (I have already gone through lots of links). Can anyone please explain to me a very basic example of a custom attribute with code? While the code to create a custom Attribute is fairly simple, it's much important that you understand what are attributes: Attributes are metadata compiled into your program. Attributes themselves doesn't add any functionality to a class, property or module, just data. However, using reflection, one can leverage those attributes in order to create functionality. So, for

Exclude property from serialization via custom attribute (json.net)

半腔热情 提交于 2019-11-26 16:07:43
I need to be able to control how/whether certain properties on a class are serialized. The simplest case is [ScriptIgnore] . However, I only want these attributes to be honored for this one specific serialization situation I am working on - if other modules downstream in the application also want to serialize these objects, none of these attributes should get in the way. So my thought is to use a custom attribute MyAttribute on the properties, and initialize the specific instance of JsonSerializer with a hook that knows to look for that attribute. At first glance, I don't see any of the