custom-attributes

C# Attributes On Fields

How do I set an attribute on a field anywhere in my assembly, then reflect on those field attributes in my entire assembly and get/set the field values that the attribute is attached too? ...

How to determine the attached type from within a custom attribute?

I have a custom attribute which can be assigned to a class, [FooAttribute]. What I would like to do, from within the attribute, is determine which type has actually used me. e.g. If I have: [FooAttribute] public class Bar { } In the code for FooAttribute, how can I determine it was Bar class that added me? I'm not specifically looking...

Accessing attributes applied to method in derived class from base class

So I've got a case where I'd like to be able to apply attributes to a (virtual) method in a derived class, but I'd like to be able to give a default implementation that uses those attributes in my base class. My original plan for doing this was to override the method in the derived class and just call the base implementation, applying t...

Custom attributes on interfaces or the classes that implement them?

When employing custom attributes to store meta-data, is it best to decorate the interface, or the class that implements the interface, assuming that any class that implements the interface would have the same data in the attribute? Update: Basically i'm writing a custom data storage mechanism for a project, and the objects represent the...

What are the similarities and differences between Java Annotations and C# Attributes?

I have a Java library I'm considering porting to C#. The Java library makes extensive use of annotations (at both build time and run time.) I've never used C# attributes, but understand that they are the rough equivalent of Java annotations. If I proceed with the port using attributes to replace annotations, what do I need to know? W...

Harnessing the power of .NET Attributes

I would like to know if attributes can be used to "mix-in" functionality to a class / method / property. Something like: [TrackChanges] public Foo { get; set; } Does anyone how this would be implemented, if at all possible? ...

CategoryAttribute problem passing through WCF

Hi, I have a problem with WCF seemingly not including attributes such as Category when passing entities around. I have a class wrapped around an EF entity, used to populate a property grid on a thick client. I am trying to retrieve information from the server, using WCF, and to replicate the property grid on the client side. I have ...

C# - how enumerate all classes with custom class attribute?

Question based on MSDN example: http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx Let's say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute would be used to list possibl...

Custom Attributes on Class Members

I am using a Custom Attribute to define how a class's members are mapped to properties for posting as a form post (Payment Gateway). I have the custom attribute working just fine, and am able to get the attribute by "name", but would like to get the attribute by the member itself. For example: getFieldName("name"); vs getFieldName(...

Add metadata to an XSD definition

Hi. Not all that familiar with XSD, I wonder if it is possible to do the following: <xs:group name="SomeGroup"> <xs:sequence> <xs:element name="Groupingcode" type="OurType" origin="DB" /> <xs:element name="Description" type="StringType" origin="XML" /> To explain: I have an XSD schema file. I need to generate an X...

How can I access a method's parameter from an associated attribute?

Hi. Given the following classes, how can i intercept Class1.SampleMethod's Value from SampleAttribute? Thanks Public Class Class1 <SampleAttribute()> _ Public Function SampleMethod(ByVal Value As Integer) As Boolean Return True End Function End Class <AttributeUsage(AttributeTargets.Method)> _ Public Class SampleAtt...

Is it possible to query custom Attributes in C# during compile time ( not run-time )

In other words could it be possible to create assembly, which does not even compile (assuming the checking code is not removed ) if each one of the Classes does not have ( "must have" ) custom attributes ( for example Author and Version ) ? Here is the code I have used for querying during run time : using System; using System.Reflectio...

Do you think C# attributes (or similar mechanisms) are a good idea or do you discourage the use of them?

I have based many designs and frameworks that use C# Attributes over the past 4 or 5 years. But lately I see many people openingly discouraging their use or changing their frameworks to reduce the need or use of them. I found them to be a godsend but now I am starting to wonder what I am missing. To clarify: Use Convention over Conf...

ASP.NET MVC How to apply role-based or authentication-based View rendering?

Hi, i want to show/hide certain parts of a View based on Authentication-status or Roles. For my controller actions I have extended ActionFilterAttribute so I can attribute certain Actions. <RequiresRole(Role:="Admin")> _ Function Action() as ActionResult Return View() End Function Is there a similar way (attributing) which I ca...

AllowMultiple does not work with Property Attributes?

I'm tying to collect all Custom Attributes placed over a Property. There are more than one Attributes of the same type assigned to the Property, but when collecting them , the resulting collection only contains the first Attribute of the specific type: The Attribute class [AttributeUsage(System.AttributeTargets.Property, ...

Using custom attributes

Hello, I am watching a video about [LINQ][1] and came across a problem. In this video, Mike uses some custom attributes for database name and that does not work for me. My code (which works fine): class MyContext : DataContext { public MyContext(string conStr) : base(conStr) { } } class Program { static void Main(stri...

Using ContextAttribute with a Method

The attribute which targets method is not working. The code is below. What could be the problem? using System; namespace AttributeProgram { class Program:ContextBoundObject { [TestAttribute("Hello")] public void Print() { Console.WriteLine("How are you?"); } static void Main(s...

Magento - Show Custom Attributes in Grouped Product table.

I need to find a way to show the value of a custom attribute in place of the "Product Name" shown in the image below. I'm working with /app/design/frontend/default/defaultx/template/catalog/product/view/type/grouped.php The code below doesn't work(the custom attribute is yearmade): <?php if (count($_associatedProducts)): ?> <?p...

Real world use of custom .NET attributes

What kind of things have you used custom .NET attributes for in the real world? I've read several articles about them, but I have never used custom attributes. I feel like I might be overlooking them when they could be useful. EDIT I am talking about attributes that you create, not ones that are already included in the framework. ...

Adding custom property attributes in Entity Framework code

Is there any way to add custom attributes to properties in EF generated code? The only thing I can see as a plausible solution would be to come up with a custom T4 template. However, because of the nature of the attribute it would be impossible to determine the correct attribute parameter per EF property. ...