Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means the attribute return in c#?

Tags:

c#

I have a piece of code like this:

[return: XmlElement("return", Namespace = "", IsNullable = false, DataType = "base64Binary")]
public byte[] WORK([XmlElement(Namespace = "http://www.example.com/xml/someapi", DataType = "string", Form = XmlSchemaForm.Qualified)] string guid, [XmlElement(Namespace = "http://www.example.com/xml/someapi", DataType = "base64Binary", Form = XmlSchemaForm.Qualified)] byte[] data) {

   // some work
}

what does the attribute return: mean?

like image 716
user3112115 Avatar asked Jun 30 '15 04:06

user3112115


1 Answers

I'd never come across it before, but it seems to be described as attribute targets in Disabmbiguating Attribute Targets

This sort of situation arises frequently when marshaling. To resolve the ambiguity, C# has a set of default targets for each kind of declaration, which can be overridden by explicitly specifying attribute targets. C#

// default: applies to method 
[SomeAttr] 
int Method1() { return 0; } 

// applies to method 
[method: SomeAttr] 
int Method2() { return 0; } 

// applies to return value 
[return: SomeAttr] 
int Method3() { return 0; } 

Note that this is independent of the targets on which SomeAttr is defined to be valid; that is, even if SomeAttr were defined to apply only to return values, the return target would still have to be specified. In other words, the compiler will not use AttributeUsage information to resolve ambiguous attribute targets. For more information, see AttributeUsage (C# Programming Guide). The syntax for attribute targets is as follows: [target : attribute-list]

like image 160
Mr Moose Avatar answered Nov 01 '22 08:11

Mr Moose