Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-如果某个方法具有属性A但没有';你没有属性B吗?_C#_.net_Attributes - Fatal编程技术网

C#-如果某个方法具有属性A但没有';你没有属性B吗?

C#-如果某个方法具有属性A但没有';你没有属性B吗?,c#,.net,attributes,C#,.net,Attributes,如果只使用以下属性之一,如何使编译失败 public class Attribute1 : Attribute {} public class Attribute2 : Attribute {} [Attribute1()] [Attribute2()] public void SomeMethod() { } 如果一个方法有'Attribute1'属性,那么它必须有'Attribute2'属性,反之亦然。否则编译将失败,并显示正确的

如果只使用以下属性之一,如何使编译失败

    public class Attribute1 : Attribute {}
    public class Attribute2 : Attribute {}

    [Attribute1()]
    [Attribute2()]
    public void SomeMethod()
    {

    }

如果一个方法有'Attribute1'属性,那么它必须有'Attribute2'属性,反之亦然。否则编译将失败,并显示正确的消息

为此,您可以
使用C#
进行反射。()

反射的MSDN描述

可以使用反射动态创建类型实例、将类型绑定到现有对象、从现有对象获取类型并调用其方法或访问其字段和属性如果您在代码中使用属性,反射使您能够访问它们

使用以下代码访问代码中的属性

try
    {
        // Get the type of MyClass1.
        Type myType = typeof(MyClass1);
        // Get the members associated with MyClass1.
        MemberInfo[] myMembers = myType.GetMembers();

        // Display the attributes for each of the members of MyClass1.
        for(int i = 0; i < myMembers.Length; i++)
        {
            Object[] myAttributes = myMembers[i].GetCustomAttributes(true);
            if(myAttributes.Length > 0)
            {
                Console.WriteLine("\nThe attributes for the member {0} are: \n", myMembers[i]);
                for(int j = 0; j < myAttributes.Length; j++)
                    Console.WriteLine("The type of the attribute is {0}.", myAttributes[j]);
            }
        }
    }
    catch(Exception e)
    {
        Console.WriteLine("An exception occurred: {0}", e.Message);
    }
试试看
{
//获取MyClass1的类型。
类型myType=typeof(MyClass1);
//获取与MyClass1关联的成员。
MemberInfo[]myMembers=myType.GetMembers();
//显示MyClass1的每个成员的属性。
for(int i=0;i0)
{
WriteLine(“\n成员{0}的属性是:\n”,myMembers[i]);
for(int j=0;j

然后在获得与您的
类关联的所有
属性之后,继续您的需求,否则如果您没有这两个
属性
,则抛出一个
异常

如果要发布库,您需要编写的是,并将其挂接到,然后将这些生成目标打包到。

属性是运行时行为,而不是编译时。如果你真的需要有这种行为,那么你应该使用一个包含两组功能的单一属性。根本的问题是为什么你想强制类具有某些属性。它们包含什么,类为什么需要它们?有一些方法可以要求类具有某些属性或方法,或者获得编译器错误。我怀疑,如果您描述了您想要对属性做什么,就会有一个解决方案。属性不是必需的。