custom-attributes

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

不羁岁月 提交于 2019-11-28 16:49:42
I have a Java library I'm considering porting to C#. The Java library makes extensive use of annotations (at both build time and run time.) I've never used C# attributes, but understand that they are the rough equivalent of Java annotations. If I proceed with the port using attributes to replace annotations, what do I need to know? What's going to be the same? Different? What's going to bite me? 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

Get a List<string> of my enum attributes with a generic method

≡放荡痞女 提交于 2019-11-28 10:23:52
At start, we have this basic enum. public enum E_Levels { [ValueOfEnum("Low level")] LOW, [ValueOfEnum("Normal level")] NORMAL, [ValueOfEnum("High level")] HIGH } And I would like to get a List<string> whatever the enum . Something like Extensions.GetValuesOfEnum<E_Levels>() which could return a List<string> with "Low level", "Normal level" and "High level" in it. StackOF helped me to get one value attribute : public static class Extensions { public static string ToValueOfEnum(this Enum value) { FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); ValueOfEnum[] attribs = fieldInfo

Specify required base class for .NET attribute targets

人盡茶涼 提交于 2019-11-28 10:05:50
I tried to create a custom .NET attribute with the code below but accidentally left off the subclass. This generated an easily-fixed compiler error shown in the comment. // results in compiler error CS0641: Attribute 'AttributeUsage' is // only valid on classes derived from System.Attribute [AttributeUsage(AttributeTargets.Class)] internal class ToolDeclarationAttribute { internal ToolDeclarationAttribute() { } } My question is how does the compiler know the [AttributeUsage] attribute can only be applied to a subclass of System.Attribute ? Using .NET Reflector I don't see anything special on

What's the simplest most elegant way to utilize a custom attribute

ぐ巨炮叔叔 提交于 2019-11-28 09:07:04
问题 So a little confession, I've never written an attribute class. I understand they serve the purpose of decorating classes with flags or extra functionality possibly. Can someone give me a quick example of not just creating and applying an attribute to a class, but rather utilizing the attribute from another block of code. The only code samples I've ever seen to utilize any form of attributes was doing so with reflection, though I've always hoped there's a way of using them without reflection.

How to make configurable DisplayFormat attribute

只谈情不闲聊 提交于 2019-11-28 07:54:30
问题 I got a date format like: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime? CreatedOn { get; set; } Now, I want to make every datetime format in my application read from one config file. like: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = SomeClass.GetDateFormat())] public DateTime? CreatedOn { get; set; } but this will not compile. How can I do this? 回答1: The problem with this is .NET only allows you to put compile-time

Adding custom property attributes in Entity Framework code

▼魔方 西西 提交于 2019-11-28 06:20:02
Is there any way to add custom attributes to properties in EF generated code? The only thing I can see as a plausible solution would be to come up with a custom T4 template. However, because of the nature of the attribute it would be impossible to determine the correct attribute parameter per EF property. You can do this by specifying a metadata type that mirrors the properties and is used simply for attribution. [MetadataType(typeof(Dinner_Validation))] public partial class Dinner {} public class Dinner_Validation { [Required] public string Title { get; set; } } Steve Smith blogs about it

Is HTML 5 supported by all the main browsers?

微笑、不失礼 提交于 2019-11-28 03:54:33
问题 I am looking at the custom attributes feature of html 5 here at this link http://ejohn.org/blog/html-5-data-attributes/ This look like the perfect thing for when I am using jquery/javascript. My question, Is HTML 5 supported by all the main browsers? example <li class="user" data-name="John Resig" data-city="Boston" data-lang="js" data-food="Bacon"> <b>John says:</b> <span>Hello, how are you?</span> </li> 回答1: Various portions of HTML5 are supported by the different browsers, for various

Custom Attributes on ActionResult

痞子三分冷 提交于 2019-11-28 03:17:39
问题 This is probably a rookie question but; Let's say I have an ActionResult that I only want to grant access to after hours. Let's also say that I want to decorate my ActionResult with a custom attribute. So the code might look something like; [AllowAccess(after="17:00:00", before="08:00:00")] public ActionResult AfterHoursPage() { //Do something not so interesting here; return View(); } How exactly would I get this to work? I've done some research on creating Custom Attributes but I think I'm

Uppercase attribute that converts the input to uppercase

*爱你&永不变心* 提交于 2019-11-28 00:37:35
问题 I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the model value to be converted to uppercase when it arrived at the server. At the moment I have the following code within the model: [Required] [Display(Name="Account Code")] [StringValidation(RegExValidation.AccountCode, Uppercase=true)] public string Account { get { return _account; } set { if (value != null) _account = value.ToUpper(); }

Adding custom attributes to an asp:CheckBox control

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 23:40:23
问题 I am trying to add a custom data-required attribute to an asp Checkbox control, it works fine for all other controls I've used it on, but on checkboxs it renders the checkbox inside a span that has the custom attribute on. I've tried adding the attribute in the markup and tried adding it to the control in the code behind. Any ideas? 回答1: use InputAttributes 回答2: Here is the code I have used: cb.InputAttributes.Add("data-group", "chkbox"); Then you can use JQuery to target all check-boxes. $("