custom-attributes

Attribute.IsDefined doesn't see attributes applied with MetadataType class

雨燕双飞 提交于 2019-11-26 16:07:28
问题 If I apply attributes to a partial class via the MetadataType attribute, those attributes are not found via Attribute.IsDefined(). Anyone know why, or what I'm doing wrong? Below is a test project I created for this, but I'm really trying to apply custom attributes to a LINQ to SQL entity class - like this answer in this question. Thanks! using System; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace MetaDataTest { class Program { static void Main(string[] args)

Find methods that have custom attribute using reflection

我的梦境 提交于 2019-11-26 15:53:22
问题 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);

How enumerate all classes with custom class attribute?

心已入冬 提交于 2019-11-26 15:01:00
Question based on MSDN example . Let's say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute would be used to list possible menu options, selecting item will bring to screen instance of such class. Number of classes/items will grow slowly, but this way we can avoid enumerating them all elsewhere, I think. Yes, absolutely. Using Reflection: static IEnumerable<Type> GetTypesWithHelpAttribute(Assembly assembly) { foreach(Type type in assembly

Get enum from enum attribute

六眼飞鱼酱① 提交于 2019-11-26 13:39:21
I've got public enum Als { [StringValue("Beantwoord")] Beantwoord = 0, [StringValue("Niet beantwoord")] NietBeantwoord = 1, [StringValue("Geselecteerd")] Geselecteerd = 2, [StringValue("Niet geselecteerd")] NietGeselecteerd = 3, } with public class StringValueAttribute : Attribute { private string _value; public StringValueAttribute(string value) { _value = value; } public string Value { get { return _value; } } } And I would like to put the value from the item I selected of a combobox into a int: int i = (int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue); //<- WRONG Is this

Custom attributes in Android fragments

时光怂恿深爱的人放手 提交于 2019-11-26 13:05:46
问题 I\'d like to define custom attributes in Android fragment using XML (without using bundle additional parameters) like declare-styleable in custom controls. But there are no constructors with AttrSet parameters, so is it possible? Can i just override public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState) in order to get attributes support? 回答1: The Link for Support4Demos is changed or can be changed so posting the complete

How to pass custom component parameters in java and xml

☆樱花仙子☆ 提交于 2019-11-26 10:19:00
问题 When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor. It is often suggested that when creating a component in java that you simply use the default constructor, i.e. new MyComponent(context); rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I\'ve tried to create an attrs object and it doesn\'t seem either easy or at all possible

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

主宰稳场 提交于 2019-11-26 08:16:25
问题 This question already has answers here : ASP.NET MVC - How to show unauthorized error on login page? (7 answers) Closed 6 years ago . 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

Constructor Dependency Injection WebApi Attributes

你说的曾经没有我的故事 提交于 2019-11-26 07:47:32
问题 I have been looking around for a non Parameter injection option for the WebApi attributes. My question is simply whether this is actually possible using Structuremap? I have been googling around but keep coming up with either property injection (which I prefer not to use) or supposed implementations of constructor injection that I have thus far been unable to replicate. My container of choice is Structuremap however any example of this will suffice as I am able to convert it. Anyone ever

How enumerate all classes with custom class attribute?

烈酒焚心 提交于 2019-11-26 05:57:21
问题 Question based on MSDN example. Let\'s say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute would be used to list possible menu options, selecting item will bring to screen instance of such class. Number of classes/items will grow slowly, but this way we can avoid enumerating them all elsewhere, I think. 回答1: Yes, absolutely. Using

Adding attributes to customer entity

风流意气都作罢 提交于 2019-11-26 05:21:36
问题 my current goal is to add a new customer attribute (with int type) which should appear as select with predefined options (loaded from a model with entries editable in backend, which is done). I\'m struggling with proper use of $installer->addAttribute() method, especially specifying correct source option. Other problem is the new attribute isn\'t saved to eav_entity_attribute table I\'m on Magento CE 1.5.1.0 回答1: This is the code for a basic int attribute with text renderer: $installer =