简体   繁体   English

这个语法在c#中意味着什么?

[英]What does this syntax mean in c#?

I may not have it correct, but I saw something like this above a WebMethod: 我可能没有正确的,但我在WebMethod上面看到了类似的东西:

[return:(XmlElement("Class2"),IsNullable = false)]
public Class2 MEthod1()
{

}

I saw a vb version first and I used a converter to convert it to c#. 我先看了一个vb版本,然后使用转换器将其转换为c#。 I have never seen it before. 我以前从未见过它。 It's in a vb 6 asmx file. 它位于vb 6 asmx文件中。

It's an attribute target, and it's being used in your example to disambiguate usage on the return value from other elements: 它是一个属性目标,它在您的示例中用于消除其他元素返回值的使用:

// 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; } 

When creating attributes you can specify which language elements an attribute can be applied to. 创建属性时,您可以指定可以应用属性的语言元素。 This is illustrated in the example below. 这在下面的示例中说明。

For a list of available targets see here: 有关可用目标的列表,请参见此处:
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx

namespace AttTargsCS 
{
    // This attribute is only valid on a class.
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassTargetAttribute : Attribute {
    }

    // This attribute is only valid on a method.
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodTargetAttribute : Attribute {
    }

    // This attribute is only valid on a constructor.
    [AttributeUsage(AttributeTargets.Constructor)]
    public class ConstructorTargetAttribute : Attribute {
    }

    // This attribute is only valid on a field.
    [AttributeUsage(AttributeTargets.Field)]
    public class FieldTargetAttribute : Attribute {
    }

    // This attribute is valid on a class or a method.
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
    public class ClassMethodTargetAttribute : Attribute {
    }

    // This attribute is valid on any target.
    [AttributeUsage(AttributeTargets.All)]
    public class AllTargetsAttribute : Attribute {
    }

    [ClassTarget]
    [ClassMethodTarget]
    [AllTargets]
    public class TestClassAttribute {
        [ConstructorTarget]
        [AllTargets]
        TestClassAttribute() {
        }

        [MethodTarget]
        [ClassMethodTarget]
        [AllTargets]
        public void Method1() {
        }

        [FieldTarget]
        [AllTargets]
        public int myInt;

        static void Main(string[] args) {
        }
    }
}

It's an attribute modifying how the return value of the method is serialized into XML. 它是一个属性,用于修改方法的返回值如何序列化为XML。

In general, the [return: Attribute] syntax is used to indicate that the attribute applies to the return value of a method. 通常, [return: Attribute]语法用于指示该属性适用于方法的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM