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

Xai*_*oft 6 .net c#

我可能没有正确的,但我在WebMethod上面看到了类似的东西:

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

}
Run Code Online (Sandbox Code Playgroud)

我先看了一个vb版本,然后使用转换器将其转换为c#.我以前从未见过它.它位于vb 6 asmx文件中.

Jam*_*son 16

它是一个属性目标,它在您的示例中用于消除其他元素返回值的使用:

// 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; } 
Run Code Online (Sandbox Code Playgroud)

创建属性时,您可以指定可以应用属性的语言元素.这在下面的示例中说明.

有关可用目标的列表,请参阅此处: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) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*ers 5

它是一个属性,用于修改方法的返回值如何序列化为XML.

通常,[return: Attribute]语法用于指示该属性适用于方法的返回值.