Sprache: C#
Mit Hilfe dieser Klasse könnt Ihr zur Laufzeit prüfen ob Attribute auf Klassen definiert sind, falls dies der Fall ist könnt ihr mit den anderen beiden Methoden abfragen ob das Value des Attributes true ist oder das Value als string auslesen.
internal class AttributeLogic
{
internal static readonly AttributeLogic Instance = new AttributeLogic();
internal string GetAttributeValue(Type targetType, Type attributeType, string attributePropertyName)
{
if (Attribute.IsDefined(targetType, attributeType))
{
Object att = Attribute.GetCustomAttribute(targetType, attributeType);
PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
if (pi == null)
{
throw new Exception(String.Format("The {0} attribute on the {1} type dosen't define the {2} property",
new object[] { attributeType, targetType, attributePropertyName }));
}
else
{
return pi.GetValue(att, null).ToString();
}
}
else
{
throw new Exception(String.Format("The {0} attribute isn't set to the type {1}", new object[] { attributeType, targetType }));
}
}
internal bool isAttributeTrue(Type targetType, Type attributeType, string attributePropertyName)
{
if (Attribute.IsDefined(targetType, attributeType))
{
Object att = Attribute.GetCustomAttribute(targetType, attributeType);
PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
if (pi == null)
{
return false;
}
else
{
return Convert.ToBoolean(pi.GetValue(att, null));
}
}
else
{
return false;
}
}
}
internal class AttributeLogic
{
internal static readonly AttributeLogic Instance = new AttributeLogic();
internal string GetAttributeValue(Type targetType, Type attributeType, string attributePropertyName)
{
if (Attribute.IsDefined(targetType, attributeType))
{
Object att = Attribute.GetCustomAttribute(targetType, attributeType);
PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
if (pi == null)
{
throw new Exception(String.Format("The {0} attribute on the {1} type dosen't define the {2} property",
new object[] { attributeType, targetType, attributePropertyName }));
}
else
{
return pi.GetValue(att, null).ToString();
}
}
else
{
throw new Exception(String.Format("The {0} attribute isn't set to the type {1}", new object[] { attributeType, targetType }));
}
}
internal bool isAttributeTrue(Type targetType, Type attributeType, string attributePropertyName)
{
if (Attribute.IsDefined(targetType, attributeType))
{
Object att = Attribute.GetCustomAttribute(targetType, attributeType);
PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
if (pi == null)
{
return false;
}
else
{
return Convert.ToBoolean(pi.GetValue(att, null));
}
}
else
{
return false;
}
}
}
Alte URL:
/snippet/attribute-zur-laufzeit-abfragen/769
Zur Laufzeit ist das ja ganz nett -no offense- aber zur Compile Zeit Attributes abzufragen ist viel spannender! Damit könnte man den eigenen Code endlich in Stufe 2 Grammatiken umwandeln, oder zumindest pre/post conditions definieren! Das wär n echter Hammer!
@Yoka: Dann schau dir mal PostSharp an.