Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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# 属性和类_C#_Attributes - Fatal编程技术网

C# 属性和类

C# 属性和类,c#,attributes,C#,Attributes,我正在搜索如何在属性定义中知道我应用属性的类是否有另一个属性 例如: [My1Attribute] public class MyClass { [My2Attribute] int aux{get;set;} } internal sealed class My1Attribute : Attribute { public My1Attribute { // How can I Know i

我正在搜索如何在属性定义中知道我应用属性的类是否有另一个属性
例如:

[My1Attribute]  
public class MyClass  
{   
    [My2Attribute]  
    int aux{get;set;}  

}        

internal sealed class My1Attribute : Attribute
{ 
     public My1Attribute
     {
           // How can  I Know if 'MyClass' has My2Attribute applied ???
     }
}

属性本身不知道它所附加到的类。您将需要使用其他服务/助手功能/任何东西来将它们配对

但是,您可能会发现以下内容很有用:

public static bool HasAttribute<T, TAttribute>() where TAttribute : Attribute
{
    return typeof (T).GetCustomAttributes(typeof (TAttribute), true).Any();
}
public static bool hasaAttribute(),其中tatAttribute:Attribute
{
返回typeof(T).GetCustomAttributes(typeof(tatAttribute),true).Any();
}
编辑:用于查找成员字段上的属性

/// <summary>
/// Returns all the (accessible) fields or properties that for a given type that
/// have the "T" attribute declared on them.
/// </summary>
/// <param name="type">Type object to search</param>
/// <returns>List of matching members</returns>
public static List<MemberInfo> FindMembers<T>(Type type) where T : Attribute
{
    return FindMembers<T>(type, MemberTypes.Field | MemberTypes.Property);
}

/// <summary>
/// Returns all the (accessible) fields or properties that for a given type that
/// have the "T" attribute declared on them.
/// </summary>
/// <param name="type">Type object to search</param>
/// <returns>List of matching members</returns>
public static List<MemberInfo> FindMembers<T>(Type type, MemberTypes memberTypesFlags) where T : Attribute
{
    const BindingFlags FieldBindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

    List<MemberInfo> members = new List<MemberInfo>();
    members.AddRange(type.FindMembers(
                            memberTypesFlags,
                            FieldBindingFlags,
                            HasAttribute<T>, // Use delegate from below...
                            null)); // This arg is ignored by the delegate anyway...

    return members;
}

public static bool HasAttribute<T>(MemberInfo mi) where T : Attribute
{
    return GetAttribute<T>(mi) != null;
}

public static bool HasAttribute<T>(MemberInfo mi, object o) where T : Attribute
{
    return GetAttribute<T>(mi) != null;
}
//
///返回给定类型的所有(可访问)字段或属性
///在它们上声明“T”属性。
/// 
///键入要搜索的对象
///配对成员名单
公共静态列表FindMembers(类型),其中T:Attribute
{
返回FindMembers(类型,MemberTypes.Field | MemberTypes.Property);
}
/// 
///返回给定类型的所有(可访问)字段或属性
///在它们上声明“T”属性。
/// 
///键入要搜索的对象
///配对成员名单
公共静态列表FindMembers(类型类型,MemberTypes memberTypesFlags),其中T:Attribute
{
const BindingFlags FieldBindingFlags=BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
列表成员=新列表();
members.AddRange(type.FindMembers(
成员类型标签,
战旗,
HasAttribute,//从下面使用委托。。。
null));//无论如何委托都会忽略此参数。。。
返回成员;
}
公共静态bool HasAttribute(MemberInfo mi),其中T:Attribute
{
返回GetAttribute(mi)!=null;
}
公共静态bool hasaAttribute(MemberInfo mi,object o),其中T:Attribute
{
返回GetAttribute(mi)!=null;
}

您试过了吗?另外,这里有一个相关的问题,您可能会觉得很有帮助:

在这种情况下,您需要定义有关如何确定要检查哪些成员的规则。在您的示例中,您正在属性上使用属性修饰,因此假设您有一个
MyClass
Type
实例(例如
typeof(MyClass)
),您可以获取属性:

var property = type.GetProperty("aux", BindingFlags.Instance | BindingFlags.NonPublic);
if (property.IsDefined(typeof(My1Attribute))) 
{
    // Property has the attribute.
}
(这是假设您确实想要获取该非公共实例属性,如果不是,请调整您的
BindingFlags

如果确实要使用该属性,请执行以下操作:

var attib = property.GetCustomAttributes(typeof(My1Attribute), false)[0];
// Do something with the attribute instance.

我猜你的意思是,一般来说,找出任何具有MyAttribute1的类是否具有My2Attribute(而不是特定的MyClass)。我能想到的唯一方法是从反射中获取所有类的列表,并对它们进行迭代,检查哪些类具有Attribute1,然后检查它们是否具有Attribute2


我认为您不能做任何聪明的事情,比如自动检索具有当前属性的类列表

我想做一些和你说的相似的事情。假设我有一个“Orderable”属性,我需要一个“DefaultOrderAttribute”。如果我定义了一个类似“Orderable”的类,我需要检查DEfaultOrder是否也是setBut,但我不能在attributeWell中使用它,你可以将它作为一个静态函数,但如果我是你,我会将它用作扩展方法或静态帮助器类。非常感谢!!但是我怎么知道属性本身内部绑定的类来应用它呢!!?!!?!例如,我有一个“OrderableAttribute”和一个“DefaultOrderAttribute”。如果我用“OrderableAttribute”定义一个类,我需要检查是否也设置了DefaultOrderAttribute