custom-attributes

Generating additional code through a custom attribute

不想你离开。 提交于 2019-12-12 10:57:39
问题 I am still fairly new to C# and I have a question regarding attributes. Is it possible to write a custom attribute which generates additional code at compile time. For example: [Forever] public void MyMethod() { // Code } Turns into: public void MyMethod() { while (true) { // Code } } 回答1: Out of the box, no, this isn't something that can be done. Using PostSharp though, this can be achieved: http://www.sharpcrafters.com/aop.net/compiletime-weaving 回答2: Afterthought works similar to PostSharp

asp.net mvc custom attributes

烈酒焚心 提交于 2019-12-12 10:24:07
问题 I am trying to create a custom attribute in mvc to use it's parameters in a view as breadCrumb. well, this is the code of the attribute [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] public class BreadCrumbAttribute : Attribute { public BreadCrumbAttribute(string title, string parent, string url) { this._title = title; this._parent = parent; this._url = url; } #region named parameters properties private string _title; public string Title { get { return _title; } } private string

How to pass compiler checked property names / Expression tree to a custom attribute

别说谁变了你拦得住时间么 提交于 2019-12-12 08:14:40
问题 In a few places, I've noticed expression trees passed as arguments to methods to allow compiler checking of property names. For example, Caliburn Micro has the following method signature in its PropertyChangedBase class: public virtual void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property); I have a custom attribute which I would like to have the same type of compiler checking of property names in the constructor, to enable me to type: [MyCustomAttribute(() =>

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

半腔热情 提交于 2019-12-12 07:59:16
问题 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 -

Is it a bad practice to add extra attributes to HTML elements?

寵の児 提交于 2019-12-12 07:17:14
问题 Sometimes I add an attribute to some of my controls. Like: <a href id="myLlink" isClimber="True">Chris Sharma</a> I know it is not a valid html. But it helps me in some cases. Is this considered as a bad practice? A friend of mine says that it is ok for Intranet environment but on internet it might not be find friendly by search engines. If it is not a good practice, what are the best practicess? Thanks 回答1: Yes. It is considered a bad practice. Your HTML (if it's 4.0) won't validate

Conditional method depending on class attribute

人走茶凉 提交于 2019-12-12 05:28:50
问题 I'm wondering if a conditional method can have his condition on a class attribute. For instance : class Class1 { public bool _doStuff; [Conditional(_doStuff)] public static void Stuff() { // Do the stuff } } Like the [Conditonal("DEBUG")] . Does anyone know about this? 回答1: This doesn't exist. More to the point, it's illogical. A method marked as Conditional doesn't take part in the build process if the condition isn't met. This decision can't be made in runtime. The code is omitted as if it

Execute/Reject function based on customs attribute value in dotnet core C#

纵然是瞬间 提交于 2019-12-12 05:17:05
问题 I'm trying to learn the attributes in C# dotnet core, so I wrote the 2 below classes. Attribute class : using System; namespace attribute { // [AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.All)] public class MyCustomAttribute : Attribute { public string SomeProperty { get; set; } } //[MyCustom(SomeProperty = "foo bar")] public class Foo { [MyCustom(SomeProperty = "user")] internal static void fn() { Console.WriteLine("hi"); } } } Main class : using System; using

Accessing Magento Custom Attribute

时光怂恿深爱的人放手 提交于 2019-12-12 03:08:46
问题 I have two custom attributes, one called 'amz_prod_description' and one called 'upc'. According to this > http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/0_-_theming_in_magento/data_accessing_methods_from_within_various_scopes#accessing_the_value_of_a_product_s_custom_attribute I should be able to access them both $product->getUpc() and $product->getAmzProdDescription() where $product. In the same switch, I am using both of these. The 'upc' attribute var works fine,

Custom attribute to change property value

眉间皱痕 提交于 2019-12-12 01:38:31
问题 I have a class called say Class1 public string store { get; set; } What I want is to decorate it with something like this; Class1 [GetStoreNumberFromName] [IsNumeric] public string store {get; set; } So the value might be 1234 , or it might be 1234 - Store name What I need to do is check to see if the value passed has only numbers in it. If it doesn't then I need, in the second example, to grab the first 4 chrs and change the value of the property to that. So if the passed in value was 1234 -

Can one use [CustomAttribute] with anonymous in-line delegates?

只愿长相守 提交于 2019-12-11 19:58:54
问题 With the [attribute] notation I can attach custom attributes to class instance methods. But if I write code like menu.handlers[MOUSECLICK] += (clickEvent)delegate(event e) { ... }; the [attribute] notation seems not to be available. Am I limited to attributes for methods declared out of line, or is there a way to somehow do them for in-line anonymous methods declared using delegate() { ... }? (I happen to like the inline style: you have easy access to in-scope variables and I think it makes