这是什么语法在C#是什么意思? [英] What does this syntax mean in c#?

查看:105
本文介绍了这是什么语法在C#是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能没有它正确的,但我看到这样的事情上面一个WebMethod:

I may not have it correct, but I saw something like this above a WebMethod:

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

}

我看到一个VB版第一,我用一个转换器将其转换为C#。我从来没有见过它。这是一个VB 6 ASMX文件。

I saw a vb version first and I used a converter to convert it to c#. I have never seen it before. It's in a vb 6 asmx file.

推荐答案

这是一个属性的目标,而且它的使用在你的榜样,以消除歧义的使用来自其他元素的返回值:

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.

有关可用目标的列表,在这里看到:
http://msdn.microsoft.com/en-us/library/system .attributetargets.aspx

For a list of available targets see here:
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) {
        }
    }
}

这篇关于这是什么语法在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆