What I'm trying to do is get the attribute from a property within my class using a custom attribute. The custom attribute in question looks like this:
[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;
}
}
And when applied to a property in a class it would look like this:
public class Company
{
[Primarykey(true)]
public int CompanyID { get; set; }
public string Name { get; set; }
public string Adress { get; set; }
Now what I'm trying to do is reading through all the properties within this class and checking whether they have any attributes and if so do something depending on the attribute, ergo in this case, ignore this property when making a generated SQL Query for the sake of automated ID's. I've tried multiple things from questions already asked here but none worked for me, usually returning an arror along the lines of not working for a boolean. What I've already tried is this:
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;
The issue with this is that it is hard coded in the argument it will take, whilst I need it to be dynamic, not checking simply 1 property.