Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Attributes to C# Property set parameter

Right now, without using properties, I've got this:

public void SetNumber([Array(new int[]{8})] Byte[] number)

As you can see, I am adding the ArrayAttribute attribute to the parameter.

What I want to do is the same but on a property setter. This doesn't work:

[Array(new int[]{8})]
public Byte[] SetNumber
{
  set
  {
  }
  get
  {
    return null;
  }
}

Is there any way of attaching the attribute to the set_SetNumber value method parameter?

Also, a related question. The two methods generated (the get/set) don't have the custom attribute. Can anyone explain to me why is that?

like image 698
tlg Avatar asked Nov 03 '10 18:11

tlg


People also ask

What are attributes in C?

Attributes are a mechanism by which the developer can attach extra information to language entities with a generalized syntax, instead of introducing new syntactic constructs or keywords for each feature.

What is attribute in embedded C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is __ attribute __ packed in C?

4.11 The __packed__ Attribute This attribute, attached to struct or union type definition, specifies that each member (other than zero-width bitfields) of the structure or union is placed to minimize the memory required. When attached to an enum definition, it indicates that the smallest integral type should be used.

What is naked function in C?

The naked storage-class attribute is a Microsoft-specific extension to the C language. For functions declared with the naked storage-class attribute, the compiler generates code without prolog and epilog code. You can use this feature to write your own prolog/epilog code sequences using inline assembler code.


1 Answers

You need to use the param attribute target on the set:

public Byte[] SetNumber {
  [param: Array(new int[] { 8 })]
  set {
  }
  get {
    return null;
  }
} 

As for the second question, the custom attribute is set on the property itself, not on the property accessor methods.

Also, if your ArrayAttribute only ever applies to parameters, it could be defined like this:

[AttributeUsage(AttributeTargets.Parameter)]
public class ArrayAttribute : Attribute {
  // ...
}
like image 166
Jordão Avatar answered Oct 14 '22 14:10

Jordão