Is there a way to restrict customize attribute to be only put on one property in a model class. If client side put the attribute on more than one properties, let the compiler generate warning in IDE.
A contrived example below to demonstrate the need: The Partition Key Attribute should be only placed on one property in the model class, because it represents the partition key in a cosmos database's collection.
void Main()
{
var data = new Person{ LogDate = DateTime.Today, Age = 35, Tier = 3, Name = "Biyardee" };
var props = data.GetType().GetProperties();
var keyProp = props.FirstOrDefault(p => System.Attribute.IsDefined(p, typeof(PartitionKeyAttribute)));
Console.WriteLine(keyProp.GetValue(data));
Console.WriteLine(keyProp.PropertyType.Name);
}
public class Person
{
public DateTime LogDate { get; set; }
[PartitionKeyAttribute]
public int Age { get; set; }
public int Tier { get; set; }
public string Name { get; set; }
}
public sealed class PartitionKeyAttribute : Attribute
{ }
Aucun commentaire:
Enregistrer un commentaire