StayOnTarget
StayOnTarget

Reputation: 13008

Is there a "get:" attribute target?

In Microsoft's docs I noticed the following example:

[get: System.Security.SecurityCritical]
public virtual System.Windows.Media.Imaging.BitmapSource Thumbnail { get; }

Notice that the target "get:" is applied to the attribute.

However in their C# documentation there is no such target. They list only:

assembly, module, field, event, method, param, property, return, type

I don't see anything version-specific on either of these pages.


Furthermore, the C# language specification doesn't include get: either (page 395).


I tried using it anyway in a sample in VS 2015, and the IDE reported the error:

'get' is not a recognized attribute location. Valid attribute locations for this declaration are 'property'. All attributes in this block will be ignored.

Is get: valid in some circumstances? Is it a mistake in their documentation?


FYI it is very hard if not impossible to search for the string "get:" on SO. I expected to find an answer to this already but that made it pretty hard to do so.

Upvotes: 6

Views: 157

Answers (2)

StayOnTarget
StayOnTarget

Reputation: 13008

As pointed out by @nineberry, the example in the documentation shown in the question is not the same as the actual code of the .NET Framework. The question has:

[get: System.Security.SecurityCritical]
public virtual System.Windows.Media.Imaging.BitmapSource Thumbnail { get; }

But the actual code is:

public virtual BitmapSource Thumbnail
        {
            [SecurityCritical ]
            get
            {
                ...

It was also suggested that this might have been a flaw in how the docs were auto-generated. I don't know how the docs are maintained but they have since been modified.

As of today what the documentation shows is:

public virtual System.Windows.Media.Imaging.BitmapSource Thumbnail 
{ [System.Security.SecurityCritical] get; }

which is consistent with the code and does not use the ephemeral get: target.

This seems pretty convincing that there is no (and never has been) such a target.

Upvotes: 0

Aleks Andreev
Aleks Andreev

Reputation: 7054

I can't find anything about get: target, but it is possible to apply atrribute only on get part of property or different attributes on set and get. Please see example below

[AttributeUsage(AttributeTargets.Method)]
public class MyAttributeAttribute : Attribute
{
    private readonly string name;

    public MyAttributeAttribute(string name)
    {
        this.name = name;
    }
}

public class Test
{
    public int Value
    {
        [MyAttribute("Get")]get;
        [MyAttribute("Set")]set;
    }
}

EDIT:
Also dotPeek decompiler shows me Thumbnail property like

public virtual BitmapSource Thumbnail
{
  [SecurityCritical] get
  {

so it looks like that get: target does not exists

Upvotes: 1

Related Questions