custom-attributes

How to get all descriptions of enum values with reflection?

拟墨画扇 提交于 2019-12-03 16:29:06
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 = new List<DescriptionAttribute>(); foreach (var i in fi) { try { yield return attributes.Add((

How to display custom attribute in cart (Magento)

北战南征 提交于 2019-12-03 15:37:50
问题 I have tried a lot of stuf but none of them work. I think I can get the custom attributes on the product page, but I was wondering: how to get them in shopping cart page? (the attribute is just a simple written text) 回答1: $_item->getProduct()->load() will reload all product data from the database. While this will work, bear in mind that every time you call load() Magento will execute a database query. The same can be done with much better performance by loading the attribute along with the

How do I get http verb attribute of an action using refection - ASP.NET Web API

こ雲淡風輕ζ 提交于 2019-12-03 13:53:42
I have an ASP.NET Web API project. Using reflection, how can I get the Http verb ( [HttpGet] in the example below) attribute which decorates my action methods? [HttpGet] public ActionResult Index(int id) { ... } Assume that I have the above action method in my controller. So far, by using reflection I have been able to get a MethodInfo object of the Index action method which i have stored in a variable called methodInfo I tried to get the http verb using the following but it did not work - returns null: var httpVerb = methodInfo.GetCustomAttributes(typeof (AcceptVerbsAttribute), false).Cast

Get attribute values from property and list values without knowing the attribute type

做~自己de王妃 提交于 2019-12-03 13:01:48
问题 I want to pass in the attribute name and return the value. This will be in a generic util and it will not know the attribute type. Update This is the actual working code if someone needs to do this. I needed a way to have the core code parse the attributes without knowing what they were. public void LoadPropertiesToGrid(BaseGridPropertyModel model) { foreach (PropertyInfo prop in ReflectionUtil.FindPublicPropeties(model)) { object editTyp = ReflectionUtil.GetPropertyAttributes(prop,

Custom section/collection in Web.Config

这一生的挚爱 提交于 2019-12-03 12:15:49
I've got a bunch of routes that I want to be able to throw in my Web.Config file. I need one key and two value fields for each section/item in the collection. Something along the lines of this... <routes> <add key="AdministrationDefault" url="Administration/" file="~Administration/Default.aspx" /> <add key="AdministrationCreateCampaign" url="Administration/CreateCampaign/" file="~/Administration/CreateCampaign.aspx" /> <add key="AdministrationLogout" url="Administration/Leave/" file="~/Administration/Leave.aspx" /> </routes> Is this possible? Giedrius Very similar sample here: http://jopinblog

Any code that duplicates how the DebuggerDisplayAttribute generates the resulting string?

送分小仙女□ 提交于 2019-12-03 12:12:51
Anyone know of any code that duplicates how the DebuggerDisplayAttribute parses and gathers the resultant string? I would like to create a custom attribute that does nearly the sample thing. Similiar to "When a breakpoint is hit..." where you can use a variable within curly braces, as in "{variable}". I already handle simple cases, such as "{Name}", but something like "{Foo.Name}" requires extra reflection code that I need help with. Basically, I want to parse a string using the rules defined in the DebuggerDisplayAttribute documentation. Currently, I can parse and resolve "I am {GetName()}".

Get list of custom attributes for current action/controller in ASP.NET MVC

你离开我真会死。 提交于 2019-12-03 11:41:12
Checking out the sample code from http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc written for ASP.NET MVC2, I noticed they can check if a custom attribute is applied to the current action or controller by accessing filterContext.ActionDescriptor and filterContext.ActionDescriptor.ControllerDescriptor respectively: public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationContext filterContext) { // snip // abort if a [RequireHttps] attribute is applied to controller or action if

Custom Attributes in Android

这一生的挚爱 提交于 2019-12-03 10:26:26
I'm trying to create a custom attribute called Tag for all editable elements. I added the following to attrs.xml <declare-styleable name="Spinner"> <attr name="tag" format="string" /> </declare-styleable> <declare-styleable name="EditText"> <attr name="tag" format="string" /> </declare-styleable> I get an error saying "Attribute tag has already been defined" for the EditText. Is it not possible to create a custom attribute of the same name on different elements? If you are going to use an attr in more than one place then put it in the root element inside the <resources> element like the

Custom Attribute above a controller function

泄露秘密 提交于 2019-12-03 06:05:26
I want to put [FirstTime] attribute above a controller function and then create a FirstTimeAttribute that has some logic that checks whether the user has entered his name, and redirects him to /Home/FirstTime if he hasn't. So Instead of doing: public ActionResult Index() { // Some major logic here if (...) return RedirectToAction("FirstTime", "Home"); return View(); } I would just do: [FirstTime] public ActionResult Index() { return View(); } Is this possible? Sure. Do something like public class FirstTimeAttribute : ActionFilterAttribute { public override void OnActionExecuting

Re-using Android Custom enum xml attributes

时光毁灭记忆、已成空白 提交于 2019-12-03 04:52:26
问题 I'm defining a custom attribute in XML that is an enum.It looks something like this: <declare-styleable name="MyControl"> <attr name="myProperty"> <enum name="None" value="0"/> <enum name="One" value="1"/> <enum name="Two" value="2"/> <enum name="Three" value="3"/> <enum name="Four" value="4"/> <enum name="Five" value="5"/> <enum name="Six" value="6"/> <enum name="Seven" value="7"/> <enum name="Eight" value="8"/> <enum name="Nine" value="9"/> <enum name="Ten" value="10"/> </attr> </declare