custom-attributes

Log function calls parameter values & return values in c#

為{幸葍}努か 提交于 2020-01-03 02:47:12
问题 Say I have a class that looks like this. public static class Config { public static string GetAppSetting(string key) { return ConfigurationManager.AppSettings[key].ToString(); } } And I wanted to log every call to this method along with the key parameter & return value. The only code change I want to make is this: [Log] public static class Config { public static string GetAppSetting(string key) { return ConfigurationManager.AppSettings[key].ToString(); } } I'll most likely use log4net to log

How to test that a method argument is decorated with an attribute?

≯℡__Kan透↙ 提交于 2020-01-01 07:06:12
问题 This is probably a duplicate, but I can't find the question I'm looking for, so I'm asking it. How do you test that a method argument is decorated with an attribte? For example, the following MVC action method, using FluentValidation's CustomizeValidatorAttribute : [HttpPost] [OutputCache(VaryByParam = "*", Duration = 1800)] public virtual ActionResult ValidateSomeField( [CustomizeValidator(Properties = "SomeField")] MyViewModel model) { // code } I'm sure I'll have to use reflection,

htmlpurifier custom attributes

给你一囗甜甜゛ 提交于 2020-01-01 02:16:28
问题 How to allow custom (html5 data-*) attributes in HtmlPurifier? Input: <img src="/my.jpg" data-type="5" alt="" /> leads to an error: Attribute 'data-type' in element 'img' not supported (for information on implementing this, see the support forums) HtmlPurifier options are set to: 'HTML.AllowedAttributes' => array('img.src', 'a.href', 'img.data-type') 回答1: HTML purifier defines the matrix of attributes that are standard compliant and complains when you try to use an attribute that it is not

Attributes and Named/Optional constructor parameters not working

痞子三分冷 提交于 2019-12-30 03:44:24
问题 I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute { public string Description { get; private set; } public string Code { get; private set; } public EnumDisplayAttribute(string description = null, string code = null) { Description = description; Code = code; } } Both constructor parameters are optional. When using this attribute on a field like so public enum TransactionType { [EnumDisplay(code: "B")] Bill,

Finding custom attributes on view model properties when model binding

依然范特西╮ 提交于 2019-12-29 06:39:15
问题 I've found a lot of information on implementing a custom model binder for validation purposes but I haven't seen much about what I'm attempting to do. I want to be able to manipulate the values that the model binder is going to set based on attributes on the property in the view model. For instance: public class FooViewModel : ViewModel { [AddBar] public string Name { get; set; } } AddBar is just public class AddBarAttribute : System.Attribute { } I've not been able to find a clean way to

Specify required base class for .NET attribute targets

一笑奈何 提交于 2019-12-28 15:25:10
问题 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

Is it possible to have a delegate as attribute parameter?

江枫思渺然 提交于 2019-12-27 17:32:46
问题 Is it possible to have a delegate as the parameter of an attribute? Like this: public delegate IPropertySet ConnectionPropertiesDelegate(); public static class TestDelegate { public static IPropertySet GetConnection() { return new PropertySetClass(); } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)] public class WorkspaceAttribute : Attribute { public ConnectionPropertiesDelegate ConnectionDelegate { get; set; } public

Is it possible to have a delegate as attribute parameter?

Deadly 提交于 2019-12-27 17:32:14
问题 Is it possible to have a delegate as the parameter of an attribute? Like this: public delegate IPropertySet ConnectionPropertiesDelegate(); public static class TestDelegate { public static IPropertySet GetConnection() { return new PropertySetClass(); } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)] public class WorkspaceAttribute : Attribute { public ConnectionPropertiesDelegate ConnectionDelegate { get; set; } public

Get custom attribute from specific object property/field

╄→гoц情女王★ 提交于 2019-12-25 07:30:01
问题 I've been searching for a while now and tested several methods, but i didn't find the answer i was looking for. I'll try to explain. I have an object with several fields/properties. These properties have custom attributes. What i want is to get the custom attribute from a specific propertie without all the knowlege of the object. The are the base classes // FieldAttr has a public Text propery public class TestObject { // Declare fields [FieldAttr("prop_testfld1")] public FLDtype1 testfld1 =

MVC 3 Custom Validation Attribute for Multiple Checkboxes

只谈情不闲聊 提交于 2019-12-24 19:34:06
问题 I'm doing validation on a lengthy form using a custom attribute. One of the incoming fields is a long series of checkboxes that I'm using to allow users to set multiple values for the corresponsing database field (I know that I could use a MultiSelectList, but I'm not a big fan of them). I'm then assembling the various checkbox values in the POST method of the ActionResult into a single string before loading it into the database. That part works well. But, I need to validate that they've