FAQ Database Discussion Community


C# Attribute To Auto Run Method

c#-4.0,custom-attributes
I'm not sure if this is completely possible. But what I would like to do is create an attribute that when I call a run method then all methods that have a particular run attribute are then ran. I realize this can be done with delegates but I feel it...

Attribute constructor can't use console.writeline

c#,custom-attributes
So I was curious about what would happen if I applied the CallerMemberName attribute to an Attribute's constructor's parameter. Here is my very simple code. class MyAttribute : Attribute { public MyAttribute(string message, [CallerMemberName] string name = "none specified") { Console.WriteLine("\{message}: \{name}"); } } [My("Program")] class Program { [My("Main")] static...

Lambda expressions in attribute

c#,custom-attributes,lambda
I have a static class called Mod with a static function called Map: public static class Mod<TModel> { public static string Map<TValue>(Expression<Func<TModel, TValue>> expression) { throw new Exception("Not implemented"); } } I can execute the method like this (with no problem): var test = Mod<string>.Map<string>(x => x.ToLower()); I have an...

ASP.NET MVC: Retrieving Information Stored in Attributes

c#,asp.net-mvc,custom-attributes
I have a view in my site that displays all controller and its action methods of my application: Action Method: public ActionResult GetAllController() { var controllers = typeof (MvcApplication).Assembly.GetTypes().Where(typeof (IController).IsAssignableFrom); return View(controllers.ToList()); } View: <ul> @foreach (var item in Model) { <li> @item.Name <ul> @foreach (var action in item.GetMethods(BindingFlags.Public |...

Can I restrict a custom attribute to void methods only?

c#,attributes,custom-attributes,postsharp
I have a custom attribute which I would like to restrict to methods with return type void. I know I can restrict to methods using [AttributeUsage(AttributeTargets.Method)] but there doesn't seem to be a way to restrict the return type or any other aspect of the methods signature. The [System.Diagnostics.Conditional] attribute...

Get expected Action Parameter type in OnActionExecuting

c#,asp.net-mvc-4,query-string,custom-attributes,onactionexecuting
Question: Is it possible to know the type of parameter that is expected by the action being called? For example, I have some action as: [TestCustomAttr] public ActionResult TestAction(int a, string b) { ... and TestCustomAttr is defined as: public class TestCustomAttr : System.Web.Mvc.ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext)...

How to get declaring type of an atrribute from sub types tree in C#

c#,custom-attributes
I spend a lot of times to find how to get the type which declare the attribute in sub type hierarchy, but no find out any way yet, hope any one know how to help me. Example we have some stuffs as below: I have a attribute class to define...

How to Change the Contents (“value”) of a Custom Attribute

jquery,html5,custom-attributes
I have the following HTML5 Custom Attribute: <input ... class="my-input" data-counted="N"> How do I change its value (from "N" to "Y") in jquery? Here is what I have and it does not work: $(document).on('change', '.my_input', function() { $(this).attr('data-counted').val('Y'); }); ...

Constructor Dependency Injection WebApi Attributes

asp.net-web-api,dependency-injection,custom-attributes,structuremap3
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...

Usage of AttributeTargets.Delegate

c#,custom-attributes
According to this stackoverflow answer you can't apply a custom attribute to an anonymous method. What is AttributeTargets.Delegate then for? Is it for another .NET language?...

Change WCF custom attributes by code

.net,vb.net,wcf,custom-attributes
first of all, I want to be clear : I don't know that much on WCF attributes and I'm there to learn about it. Here is the context : we have a project where we have different forms. Each form contains datas. Some datas are different and some are the...

JSON.NET Parsing Full Key-Value Pair as Key

c#,json,json.net,asp.net-web-api2,custom-attributes
I'm rolling my own ValidateAntiForgeryToken attribute for Web API 2.2 for one of my controllers that processes form data through AJAX calls. I'm sending the data to my controller as JSON and it looks like this (pulled from Fiddler): {"__RequestVerificationToken":"E8EoBCaFbqSOXhQZiuM93jciTcOAYeLjZj682-3SZRaQ6OOtrm-caZI_IWnX1FH_nwe_AuWnWwxy5ulS0Ynz0STlNptqN09Lu69HxyTeA9PUln8h73yjahB24QPxqI010","ProjectInfo.Description":"Test Description 2"} I was trying to get the __RequestVerificationToken data...

Disabling items in PropertyGrid using Custom Attributes

.net,custom-attributes,propertygrid
I am able to selectively enable / disable items in a PropertyGrid by setting BrowsableAttributes to an array containing CategoryAttribute objects. However, I wish to enable some items within a category and disable others within the same category, so I thought I would create my own custom attribute class and...

Creating Attribute Aliases?

c#,custom-attributes
I'm trying to create a generic layer between the frameworks I am using and my application's code and have been blocked by a framework's need to decorate my classes with attributes. Is there a way to be able to somehow map attributes to other attributes? Example: Class A is decorated...

GetCustomAttributes for an enum value return an empty Array

c#,enums,custom-attributes,windows-universal,windows-10
I'm currently working on upgrading a windows 8.1 universal app to a windows 10 UWP app. There is a part of code that was working perfectly before that doesn't work anymore in my Windows 10 UWP app. I have an enum that looks like this : public enum EStaticFile {...

Get object by attribute value [duplicate]

c#,reflection,custom-attributes,spring.net
This question already has an answer here: How enumerate all classes with custom class attribute? 4 answers I have a set of classes which implement a common interface and are annotated with a business domain attribute. By design, each class is annotated with different parametrization [Foo(Bar=1)] public class EntityA...

Advantage of using CustomAttributes vs GetCustomAttributes()

c#,.net,reflection,custom-attributes
I noticed today that some new properties had appeared in my intellisense on the System.Type object for my .NET 4.5 projects. Among these was one called CustomAttributes. I was intrigued by this since I previously had understood that GetCustomAttributes was one of the most expensive reflection calls (DynamicInvoke and the...

Get other property attributes while validating a custom attribute

.net,validation,reflection,data-annotations,custom-attributes
I have created a validation attribute. And the IsValid method is as follows [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] public sealed class BlaBlaAttribute : ValidationAttribute { private readonly object _typeId = new object(); //........... protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var otherProperties = validationContext.ObjectType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).ToList();...

TypeBuilder - Adding attributes

c#,dynamic,properties,custom-attributes,typebuilder
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,...

Knockout.js - Set an attribute on drop-down without permanently binding to model

javascript,asp.net-mvc,knockout.js,custom-attributes,dropdownbox
I think I have a pretty generic question, but it is for a specific application. I would simply like to know how it might be possible to set a custom attribute on a dropdown that equals the dropdown's initial value. That value will be bound from a KnockOut view model...

Add Attributes to instance of object at runtime

c#,razor,model-view-controller,data-annotations,custom-attributes
I am trying to generate a dynamic form with question coming from a database. Each question has its type and other property. Example of my classes: public class QuestionnaireBase { public string Text { get; set; } public int Sequence { get; set; } } public abstract class Question :...