I'm trying to simplify code for extracting data from property attribute.
Attribute:
[AttributeUsage(AttributeTargets.Property)]
class NameAttribute : Attribute
{
public string Name { get; }
public ColumnAttribute(string name)
{
Name = name;
}
}
Attribute contents extraction code ( null-checks removed ):
public static string GetName<T>(string propName)
{
var propertyInfo = typeof(T).GetProperty(propName);
var nameAttribute = (NameAttribute)propertyInfo.GetCustomAttributes(typeof(NameAttribute)).FirstOrDefault();
return nameAttribute.Name;
}
Sample class:
class TestClass
{
[Column("SomeName")]
public object NamedProperty { get; set; }
}
Call sample:
var name = GetName<TestClass>(nameof(TestClass.NamedProperty))
Is it any way to rewrite attribute contents extraction method to simplify/shorten its call. It's too inconvenient for me due to its length.
Something like CallerMemberNameAttribute would be great but i found nothing.
Your syntax is already pretty short. The only redundant information is the class name, everything else is needed, it won't get much shorter. You could have a shorter syntax n your call as demonstrated below, where you remove the redundancy of the class name. However, that comes at the cost or a more complex implementation. It's up to you to decide if that's worth it:
namespace ConsoleApp2
{
using System;
using System.Linq.Expressions;
using System.Reflection;
static class Program
{
// your old method:
public static string GetName<T>(string propName)
{
var propertyInfo = typeof(T).GetProperty(propName);
var nameAttribute = propertyInfo.GetCustomAttribute(typeof(NameAttribute)) as NameAttribute;
return nameAttribute.Name;
}
// new syntax method. Still calls your old method under the hood.
public static string GetName<TClass, TProperty>(Expression<Func<TClass, TProperty>> action)
{
MemberExpression expression = action.Body as MemberExpression;
return GetName<TClass>(expression.Member.Name);
}
static void Main()
{
// you had to type "TestClass" twice
var name = GetName<TestClass>(nameof(TestClass.NamedProperty));
// slightly less intuitive, but no redundant information anymore
var name2 = GetName((TestClass x) => x.NamedProperty);
Console.WriteLine(name);
Console.WriteLine(name2);
Console.ReadLine();
}
}
[AttributeUsage(AttributeTargets.Property)]
class NameAttribute : Attribute
{
public string Name { get; }
public NameAttribute(string name)
{
this.Name = name;
}
}
class TestClass
{
[Name("SomeName")]
public object NamedProperty { get; set; }
}
}
The output is the same:
SomeName
SomeName
Related
class SampleClass
{
[SampleAttribute]
public void SampleMethod()
{
}
}
If there is a method like the above code. How can I know the method has called that has a specific attribute(In this case the attribute is 'SampleAttribute')? I know how to find methods that have specific attributes. But I don't know how to figure out when the method has called that has a specific attribute
You can do it in a few ways. But first, let's add some value to the SampleAttribute, to be sure, that everything is working:
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class SampleAttribute : Attribute
{
public SampleAttribute(string text)
{
Text = text;
}
public string Text { get; private set; }
}
And specify the attribute value to the method:
public class SampleClass
{
[SampleAttribute("This is attribute text")]
public void SampleMethod() { }
}
Now, using the reflection mechanism, we can extract the attribute value from the object:
var sampleClass = new SampleClass();
ExtractSampleAttributeValue(sampleClass);
private static string ExtractSampleAttributeValue(SampleClass sampleClass)
{
var methods = sampleClass.GetType().GetMethods();
var sampleMethod = methods.FirstOrDefault(method => method.Name == nameof(sampleClass.SampleMethod));
var sampleAttribute = Attribute.GetCustomAttribute(sampleMethod, typeof(SampleAttribute)) as SampleAttribute;
return sampleAttribute.Text;
}
Or even pass the method as the parameter:
var sampleClass = new SampleClass();
ExtractSampleAttributeValue(sampleClass.SampleMethod);
private static string ExtractSampleAttributeValue(Action sampleMethod)
{
var sampleAttribute = Attribute.GetCustomAttribute(sampleMethod.Method, typeof(SampleAttribute)) as SampleAttribute;
return sampleAttribute.Text;
}
How can I access the attribute of a generic parameter? My code fails to get the attribute:
[AttributeUsage(AttributeTargets.GenericParameter)]
class MyAttribute : Attribute
{
public string name;
}
class A<[MyAttribute(name = "Genric")] Type>
{
public static void f()
{
MyAttribute w = Attribute.GetCustomAttributes(typeof(Type))[0] as MyAttribute; // fails
Console.WriteLine(w?.name);
}
}
The attribute you are looking for is related to A<> not Type. So you have to go from there. Have a look at this example:
using System;
public class Program
{
public static void Main()
{
var genericArguments = typeof(A<>).GetGenericArguments();
var attributes = Attribute.GetCustomAttributes(genericArguments[0]);
Console.WriteLine((attributes[0] as MyAttribute).Name);
}
}
[AttributeUsage(AttributeTargets.GenericParameter)]
class MyAttribute : Attribute
{
public string Name;
}
class A<[MyAttribute(Name = "MyAttributeValue")] Type>
{
}
The output is
MyAttributeValue
The attribute is applied to the generic argument, not the type itself, so your current approach wont work.
Try this instead:
MyAttribute w = typeof(A<>)
.GetGenericArguments()
.Select(t => t.GetCustomAttribute<MyAttribute>())
.SingleOrDefault();
My question is about validation using custom attributes in C#.
I don't quite understand how the validation works. I have declared an attribute with the validation rule in it but when the error should be thrown it is not.
Attribute:
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute
{
public bool IsValid(object value)
{
if (value is string && (string)value != "")
{
return false;
}
return true;
}
}
Inside the attribute I check if the property is of type string and if its value is an empty string because that is what I have to check.
The task is to check if a property is a string and if its an empty string then its not valid, otherwise it is.
My Person class:
class Person
{
[NotNull]
public string Name { get; set; }
}
Here I am applying the custom attribute.
Main method:
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "";
Console.WriteLine("Validation done");
Console.ReadKey();
}
}
This is where I instantiate the Person class and assign an empty string to the Name property. This is where the error should be thrown I guess.
So my question is why isn't the validation applied? Should I have called the IsValid method from the attribute it self somehow?
I would take some explanation about this, thank you in advance!
The attribute itself is just a "decorator" of the property. If nothing calls it, it will not be automatically executed nor used.
In your case, however, I don't see the point of using an attribute, when you can use property itself:
private string _name = "";
public string Name
{
get
{
return _name;
}
set
{
if ( string.IsNullOrEmpty(value) )
{
//throw or fallback
}
else
{
_name = value;
}
}
}
Doing basic value validation is exactly the job property setters are great for. In case someone uses an invalid value, you can throw an exception, or set a fallback value for example.
If you would still prefer using attributes, you still need to have some code that performs the validation itself. And still, anyone can assign any valid value to the property, unless validation is performed.
For example ASP.NET MVC uses attribute validation during Model Binding - it checks the validation attributes on the bound model class and verifies it before the action method begins executing.
Example of attribute validation
Here is a simple example of how to make your code work with reflection.
First here is a slightly updated version of the validation attribute:
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute
{
public bool IsValid(object value)
{
if (!string.IsNullOrEmpty(value as string))
{
return false;
}
return true;
}
}
Your code actually only allowed a null or "" value, which I guess is opposite of what you wanted. This version is valid only when the string is not null and not empty.
Now create a Validate method in your Program class:
private static bool Validate(object model)
{
foreach (var propertyInfo in model.GetType().GetProperties())
{
foreach (var attribute in propertyInfo.GetCustomAttributes(true))
{
var notNullAttribute = attribute as NotNullAttribute;
if (notNullAttribute != null)
{
if (!notNullAttribute.IsValid(propertyInfo.GetValue(model)))
{
return false;
}
}
}
}
return true;
}
This basically gathers all properties of the type of the passed in parameter, checks all attributes of the properties for NotNullAttribute and then executes the attribute's IsValid method against the current value from the model.
Finally here is how you can call it from Main:
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "d";
if (Validate(p1))
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
Console.WriteLine("Validation done");
Console.ReadKey();
}
Now, if you are planning on adding more validation attributes, I would create an interface first:
public interface IValidationAttribute
{
bool IsValid(object value);
}
Then derive all your validation attributes from IValidationAttribute and in Validate method use IValidationAttribute in place of NotNullAttribute. This way the code becomes more future-proof as you can just program against the interface and add new validation attributes anytime.
public class BankAccount
{
public enum AccountType
{
Saving,
Current
}
[Required(ErrorMessage="First Name Required")]
[MaxLength(15,ErrorMessage="First Name should not more than 1`5 character")]
[MinLength(3,ErrorMessage="First Name should be more than 3 character")]
public string AccountHolderFirstName { get; set; }
[Required(ErrorMessage="Last Name Required")]
[MaxLength(15,ErrorMessage="Last Name should not more than 1`5 character")]
[MinLength(3,ErrorMessage="Last Name should be more than 3 character")]
public string AccountHolderLastName { get; set; }
[Required]
[RegularExpression("^[0-9]+$", ErrorMessage = "Only Number allowed in AccountNumber")]
public string AccountNumber { get; set; }
public AccountType AcType { get; set; }
[AccountBalaceCheckAttribute]
public double AccountBalance { get; set; }
}
How to Validate
public class GenericValidator
{
public static bool TryValidate(object obj, out ICollection<ValidationResult> results)
{
var context = new ValidationContext(obj, serviceProvider: null, items: null);
results = new List<ValidationResult>();
return Validator.TryValidateObject(
obj, context, results,
validateAllProperties: true
);
}
}
Example
static void Main(string[] args)
{
var bankAccount = new BankAccount();
ICollection<ValidationResult> lstvalidationResult;
bool valid = GenericValidator.TryValidate(bankAccount, out lstvalidationResult);
if (!valid)
{
foreach (ValidationResult res in lstvalidationResult)
{
Console.WriteLine(res.MemberNames +":"+ res.ErrorMessage);
}
}
Console.ReadLine();
}
I frequently need a global hard-coded mapping between an enum and another object (a string in this example). I want to co-locate the enum and mapping definitions to clarify maintenance.
As you can see, in this example, an annoying class with one static field is created.
public enum EmailTemplates
{
// Remember to edit the corresponding mapping singleton!
WelcomeEmail,
ConfirmEmail
}
public class KnownTemplates
{
public static Dictionary<EmailTemplates, string> KnownTemplates;
static KnownTemplates() {
KnownTemplates.Add(EmailTemplates.WelcomeEmail, "File1.htm");
KnownTemplates.Add(EmailTemplates.ConfirmEmail, "File2.htm");
}
}
Sometimes the mapping class can have more function and a meaningful name, and the mapping activity can even be private. But that only pollutes the maintenance/correlation problem.
Anyone have a good pattern for this?
You can use attributes to annotate the enumeration and then use reflection to build the dictionary.
[AttributeUsage(AttributeTargets.Field)]
sealed class TemplateAttribute : Attribute {
public TemplateAttribute(String fileName) {
FileName = fileName;
}
public String FileName { get; set; }
}
enum EmailTemplate {
[Template("File1.htm")]
WelcomeEmail,
[Template("File2.htm")]
ConfirmEmail
}
class KnownTemplates {
static Dictionary<EmailTemplate, String> knownTemplates;
static KnownTemplates() {
knownTemplates = typeof(EmailTemplates)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(TemplateAttribute)))
.Select(
fieldInfo => new {
Value = (EmailTemplate) fieldInfo.GetValue(null),
Template = (TemplateAttribute) Attribute
.GetCustomAttribute(fieldInfo, typeof(TemplateAttribute))
}
)
.ToDictionary(x => x.Value, x => x.Template.FileName);
}
}
If you do this a lot you can create a more general generic function that combines enumeration values with an attribute associated with that enumeration value:
static IEnumerable<Tuple<TEnum, TAttribute>> GetEnumAttributes<TEnum, TAttribute>()
where TEnum : struct
where TAttribute : Attribute {
return typeof(TEnum)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(TAttribute)))
.Select(
fieldInfo => Tuple.Create(
(TEnum) fieldInfo.GetValue(null),
(TAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof(TAttribute))
)
);
}
And use it like this:
knownTemplates = GetEnumAttributes<EmailTemplate, TemplateAttribute>()
.ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2.FileName);
For even more fun you can create an extension method:
static class EmailTemplateExtensions {
static Dictionary<EmailTemplate, String> templates;
static EmailTemplateExtensions() {
templates = GetEnumAttributes<EmailTemplate, TemplateAttribute>()
.ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2.FileName);
}
public static String FileName(this EmailTemplate emailTemplate) {
String fileName;
if (templates.TryGetValue(emailTemplate, out fileName))
return fileName;
throw new ArgumentException(
String.Format("No template defined for EmailTemplate.{0}.", emailTemplate)
);
}
}
Then calling EmailTemplate.ConfirmEmail.FileName() will return File2.htm.
Here is an approach which worked pretty well for me.
public class BaseErrWarn : Attribute
{
public string Code { get; set; }
public string Description { get; set; }
public BaseErrWarn(string code, string description)
{
this.Code = code;
this.Description = description;
}
}
public enum ErrorCode
{
[BaseErrWarn("ClientErrMissingOrEmptyField", "Field was missing or empty.")] ClientErrMissingOrEmptyField,
[BaseErrWarn("ClientErrInvalidFieldValue", "Not a valid field value.")] ClientErrInvalidFieldValue,
[BaseErrWarn("ClientErrMissingValue", "No value passed in.")] ClientErrMissingValue
}
Now you can use reflection to map the Enum to the BaseErrWarn class:
public static BaseErrWarn GetAttribute(Enum enumVal)
{
return (BaseErrWarn)Attribute.GetCustomAttribute(ForValue(enumVal), typeof(BaseErrWarn));
}
private static MemberInfo ForValue(Enum errorEnum)
{
return typeof(BaseErrWarn).GetField(Enum.GetName(typeof(BaseErrWarn), errorEnum));
}
Here is an example which uses this mapping to map an Enum to an object and then pull fields off of it:
public BaseError(Enum errorCode)
{
BaseErrWarn baseError = GetAttribute(errorCode);
this.Code = baseError.Code;
this.Description = baseError.Description;
}
public BaseError(Enum errorCode, string fieldName)
{
BaseErrWarn baseError = GetAttribute(errorCode);
this.Code = baseError.Code;
this.Description = baseError.Description;
this.FieldName = fieldName;
}
Normally, when you want to add extra info or behaviors to your enum elements, that means you need a full blown class instead. You can borrow from (old-)Java the type-safe enum pattern and create something like this:
sealed class EmailTemplate {
public static readonly EmailTemplate Welcome = new EmailTemplate("File1.html");
public static readonly EmailTemplate Confirm = new EmailTemplate("File2.html");
private EmailTemplate(string location) {
Location = location;
}
public string Location { get; private set; }
public string Render(Model data) { ... }
}
Now you can associate any properties or methods to your elements, like Location and Render above.
If I apply attributes to a partial class via the MetadataType attribute, those attributes are not found via Attribute.IsDefined(). Anyone know why, or what I'm doing wrong?
Below is a test project I created for this, but I'm really trying to apply custom attributes to a LINQ to SQL entity class - like this answer in this question.
Thanks!
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace MetaDataTest
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] properties = typeof(MyTestClass).GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
// Displays:
// False
// False
// 0
}
Console.ReadLine();
}
}
[MetadataType(typeof(MyMeta))]
public partial class MyTestClass
{
public string MyField { get; set; }
}
public class MyMeta
{
[MyAttribute()]
public string MyField { get; set; }
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : System.Attribute
{
}
}
The MetadataType attribute is used to specify help specify the additional information on the data object. To access the additional attributes you would need to do something like the following:
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace MetaDataTest
{
class Program
{
static void Main(string[] args)
{
MetadataTypeAttribute[] metadataTypes = typeof(MyTestClass).GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray();
MetadataTypeAttribute metadata = metadataTypes.FirstOrDefault();
if (metadata != null)
{
PropertyInfo[] properties = metadata.MetadataClassType.GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
RequiredAttribute attrib = (RequiredAttribute)propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true)[0];
Console.WriteLine(attrib.ErrorMessage);
}
// Results:
// True
// True
// 2
// MyField is Required
}
Console.ReadLine();
}
}
[MetadataType(typeof(MyMeta))]
public partial class MyTestClass
{
public string MyField { get; set; }
}
public class MyMeta
{
[MyAttribute()]
[Required(ErrorMessage="MyField is Required")]
public string MyField { get; set; }
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : System.Attribute
{
}
}
This also includes a sample attribute to show how to extract info that was added.
I had a similar situation. I ended up writing the following extension method for it.
The idea is to hide the abstraction of looking in 2 places (main class and metadata class).
static public Tattr GetSingleAttribute<Tattr>(this PropertyInfo pi, bool Inherit = true) where Tattr : Attribute
{
var attrs = pi.GetCustomAttributes(typeof(Tattr), Inherit);
if (attrs.Length > 0)
return (Tattr)attrs[0];
var mt = pi.DeclaringType.GetSingleAttribute<MetadataTypeAttribute>();
if (mt != null)
{
var pi2 = mt.MetadataClassType.GetProperty(pi.Name);
if (pi2 != null)
return pi2.GetSingleAttribute<Tattr>(Inherit);
}
return null;
}
My solution for generic use. Get the attribute the property that you are looking for. Return null if not found.
If found, it returns the attribute itself. So you can have access to the properties inside the attribute if you wihs.
Hopes this help.
public static Attribute GetAttribute<T>(this PropertyInfo PI, T t) where T: Type
{
var Attrs = PI.DeclaringType.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
if (Attrs.Length < 1) return null;
var metaAttr = Attrs[0] as MetadataTypeAttribute;
var metaProp = metaAttr.MetadataClassType.GetProperty(PI.Name);
if (metaProp == null) return null;
Attrs = metaProp.GetCustomAttributes(t, true);
if (Attrs.Length < 1) return null;
return Attrs[0] as Attribute;
}
Given the following classes:
public partial class Person
{
public int PersonId { get; set; }
}
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
public partial class PersonMetadata
{
[Key]
public int PersonId { get; set; }
}
}
I needed to get see if Key has been defined on a property for Person class. I then needed to get the value of the property. Using #AdamGrid answer, I modified the code like this to get it:
private static object GetPrimaryKeyValue(TEntity entity)
{
MetadataTypeAttribute[] metadataTypes = typeof(TEntity).GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray();
MetadataTypeAttribute metadata = metadataTypes.FirstOrDefault();
if (metadata == null)
{
ThrowNotFound();
}
PropertyInfo[] properties = metadata.MetadataClassType.GetProperties();
PropertyInfo primaryKeyProperty =
properties.SingleOrDefault(x => Attribute.GetCustomAttribute(x, typeof(KeyAttribute)) as KeyAttribute != null);
if (primaryKeyProperty == null)
{
ThrowNotFound();
}
object primaryKeyValue = typeof(TEntity).GetProperties().Single(x => x.Name == primaryKeyProperty.Name).GetValue(entity);
return primaryKeyValue;
}
private static void ThrowNotFound()
{
throw new InvalidOperationException
($"The type {typeof(TEntity)} does not have a property with attribute KeyAttribute to indicate the primary key. You must add that attribute to one property of the class.");
}