custom-attributes

How to put conditional Required Attribute into class property to work with WEB API?

懵懂的女人 提交于 2019-12-17 15:46:15
问题 I just want to put conditional Required Attribute which is work with WEB API Example public sealed class EmployeeModel { [Required] public int CategoryId{ get; set; } public string Email{ get; set; } // If CategoryId == 1 then it is required } I am using Model State validation via ( ActionFilterAttribute ) 回答1: You can implement your own ValidationAttribute . Perhaps something like this: public class RequireWhenCategoryAttribute : ValidationAttribute { protected override ValidationResult

WCF service attribute to log method calls and exceptions

ぐ巨炮叔叔 提交于 2019-12-17 07:20:26
问题 I have a requirement to log each method call in a WCF service, and any exceptions thrown. This has led to a lot of redundant code, because each method needs to include boilerplate similar to this: [OperationContract] public ResultBase<int> Add(int x, int y) { var parameters = new object[] { x, y } MyInfrastructure.LogStart("Add", parameters); try { // actual method body goes here } catch (Exception ex) { MyInfrastructure.LogError("Add", parameters, ex); return new ResultBase<int>("Oops, the

Change custom attribute's parameter at runtime

∥☆過路亽.° 提交于 2019-12-17 06:17:35
问题 I need change attribute’s paramater during runtime. I simplified my problem to simple example. Attribute class: [AttributeUsage(AttributeTargets.Property)] public class MyAttribute : Attribute { public string Name { get; set; } } Simple entity wich has decorated properties with attributes: public class MyEntity { [MyAttribute(Name="OldValue1")] public string Data1{ get; set; } [MyAttribute(Name = "OldValue2")] public string Data2 { get; set; } } I created instace of class MyEntity. I can

How to create a custom attribute in C#

倖福魔咒の 提交于 2019-12-17 03:46:16
问题 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? 回答1: While the code to create a custom Attribute is fairly simple, it's very important that you understand what attributes are: Attributes are metadata compiled into your program. Attributes themselves do not add any functionality to a class, property or module - just

Sequence contains no matching element - Return SiteMapNode matching custom attribute using LINQ

做~自己de王妃 提交于 2019-12-14 04:19:07
问题 I have a Web.sitemap file using the siteMapNode elements in XML. I have added custom attributes to each tag. I am trying to extract the value of the custom attribute id . I want to find a single siteMapNode in the SiteMapNodeCollection which matches the custom attribute id . I am able to achieve this using a foreach loop, but I want to use LINQ to make it short and sweet. --DOESN'T WORK-- The function is as follows: private SiteMapNode FindNodeById(SiteMapNodeCollection nodes, int

What do other people think of using “custom” attributes to make things easier (in jQuery)

一曲冷凌霜 提交于 2019-12-14 04:17:20
问题 I am currently jQuerifying (if there is such a word) my website. I am not good at JavaScript at all, and not great on jQuery. Object orientation is way passed me in JS, but the rest of my site is object oriented VB.net. I noticed, from some experience I had of jQuery a while back, just how difficult it can be when loading pages for a particular row - you have to somehow find out what ID, for example, to submit and stuff like that. Just recently I tried using something like: <li class=

Creating Attribute Aliases?

时光怂恿深爱的人放手 提交于 2019-12-13 10:53:54
问题 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 with Attribute B At runtime, map Attribute B to Attribute A Class A is seen as decorated by Attribute A throughout the application's life. 回答1: What you are trying to do sounds like bad design. Attributes are not

Use attribute routing parameters for other attributes

荒凉一梦 提交于 2019-12-13 06:08:00
问题 I use Attribute Routing (MVC) to call Controller Methods and also an Authorizationattribute with custom Properties: [Route("{id:int}")] [UserAuth(ProjectId=3)] public ActionResult Select(int id) { return JsonGet(Magic.DoSomethingMagic()); } UserAuth is just a simple AuthorizationAttribute: public class UserAuthAttribute:AuthorizeAttribute { public int ProjectId { get;set;} protected override bool AuthorizeCore(HttpContextBase contextBase) { var currentProject=new Project(ProjectId); return

Using an attribute to prematurely return from a method

血红的双手。 提交于 2019-12-13 05:26:03
问题 Is it possible to make an attribute that checks a condition and then based on that condition either stop the decorated method from executing or let the decorated method continue on executing? And now the obvious question. If so, how? If not, then is there a work around worth checking out? The overall goal of this attribute is to, in ASP.NET MVC, check if a user is authorized and either return a particular JsonResult (defined in the attribute) if they aren't authorized or let the decorated

Rename Property from Deserialized Javascript

南楼画角 提交于 2019-12-13 03:43:23
问题 How do I take deserialize JavaScript and change the name of the property being processed? For example: class MyClass { public string CreateDate {get;set;} } public class DeserializeMyClass { //How do I take "create_date" and convert it to CreateDate? Is there an attribute I //can use? var json = @"{""create_date"":""04/12/2013""}"; var ds = new JavaScriptSerializer(); ds.Deserialize<MyClass>(json); } Thanks in advance! 回答1: Looks like you can't do it easily for JavaScriptSerializer, but you