C# 读取属性的属性并检查它

C# 读取属性的属性并检查它,c#,asp.net-core,attributes,C#,Asp.net Core,Attributes,我试图做的是使用自定义属性从类中的属性获取属性。 有关的自定义属性如下所示: [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true, Inherited = false)] public class PrimarykeyAttribute : Attribute { bool key; public PrimarykeyAttribute(bool

我试图做的是使用自定义属性从类中的属性获取属性。 有关的自定义属性如下所示:

[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class PrimarykeyAttribute : Attribute
    {
        bool key;
        public PrimarykeyAttribute(bool key)
            {
                this.key = key;
            }

        public bool GetKey()
        {
            return key;
        }
    }
public class Company
    {
        [Primarykey(true)]
        public int CompanyID { get; set; }
        public string Name { get; set; }
        public string Adress { get; set; }
当应用于类中的属性时,它将如下所示:

[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class PrimarykeyAttribute : Attribute
    {
        bool key;
        public PrimarykeyAttribute(bool key)
            {
                this.key = key;
            }

        public bool GetKey()
        {
            return key;
        }
    }
public class Company
    {
        [Primarykey(true)]
        public int CompanyID { get; set; }
        public string Name { get; set; }
        public string Adress { get; set; }
现在我要做的是通读这个类中的所有属性,检查它们是否有属性,如果有,根据属性做一些事情,因此在这个例子中,为了自动ID的缘故,在生成SQL查询时忽略这个属性。 我已经尝试了很多问题,但没有一个对我有效,通常返回一个arror,表示不适用于布尔值。我已经尝试过的是:

public static class AttributeHelper
    {
        public static object GetPropertyAttributes(PropertyInfo prop, string attributeName)
        {
            // look for an attribute that takes one constructor argument
            foreach (CustomAttributeData attribData in prop.GetCustomAttributesData())
            {
                string typeName = attribData.Constructor.DeclaringType.Name;
                if (attribData.ConstructorArguments.Count == 1 &&
                    (typeName == attributeName || typeName == attributeName + "Attribute"))
                {
                    return attribData.ConstructorArguments[0].Value;
                }
            }
            return null;

问题是它在参数中是硬编码的,而我需要它是动态的,而不是简单地检查一个属性。

您可以使用它

    var keys = typeof(T).GetProperties()
               .Where(e => e.GetCustomAttribute<PrimarykeyAttribute>(true) != null)

如果我理解正确,您希望有一个方法在特定属性具有
PrimaryKey
属性时返回true,如果该属性的值为true,是否正确?这很简单,我们只需检查属性是否具有我们的属性,如果有,则获取
键的值。这是您的新
PrimaryKey
属性:

公共类PrimaryKeyAttribute:属性
{
公共bool IsKey{get;}
public PrimaryKeyAttribute(bool isKey)
{
IsKey=IsKey;
}
}
下面是检查属性是否具有该属性以及
IsKey
是否为true的新方法:

公共静态类AttributeHelper
{
public static bool HasPrimaryKeyTrue(此属性为自身信息)
{
var属性=self.GetCustomAttribtue(true);
if(属性==null)
return false;//此属性上不存在属性
return attribute.IsKey;//返回我们设置的值
}
}
虽然删除
IsKey
属性并仅将
PrimaryKey
属性应用于属性是更容易的,因为
[PrimaryKey(false)]
与根本没有主键属性相同


用法:您现在可以以多种方式使用它,但最适合您的方法是在LINQ表达式中使用它。差不多

var公司=。。。;
var companyPks=company.GetType().GetProperties().Where(x=>x.HasPrimaryKeyTrue).ToList();

companyPks
现在是一个属性信息列表,仅当相应属性具有
PrimaryKey
属性,并且其中
IsKey`设置为true时

这是否回答了您的问题?“我已经尝试了很多问题,但没有一个对我有效,通常返回一个arror,意思是不适用于布尔值。”请告诉我们您尝试了什么,告诉我们您得到的确切错误消息,并解释您不理解的错误消息。那我们就可以帮你了。否则,很可能有人会将此关闭为您已经尝试过的某个内容的副本,这将是一种耻辱。@Heinzi我编辑了问题“虽然我需要它是动态的,而不是简单地检查1个属性。”-您总是阅读一个属性的属性。如果您需要所有属性的属性,则必须获取属性列表并循环它们(
GetType().GetProperties()
)。相关:这确实回答了我的问题是,但是[Primarykey(false)]的原因是什么在此上下文中有用的是,如果为true,则假定为自动,如果为false,则假定为非自动,并由SQL语句添加。谢谢你的帮助。