Answered by:
Add bool attribute on property to see if we need to check for null

Question
-
User-1188570427 posted
Is it possible to add an attribute on a property to check if I need to check that property for being Null or string empty?
Then I can pass it to the AllPropertiesValid() method and if the attribute is TRUE then I need to check, if FALSE then ignore and by pass it.
Thank you.
public class MyDto { public string Prop1 { get; set; } public int? Prop2 { get; set; } }
public static bool AllPropertiesValid(object obj) { return !obj.GetType().GetProperties().All(p => { var value = p.GetValue(obj); if (value == null) { return false; } if (p.PropertyType == typeof(string)) { return string.IsNullOrEmpty((string)value); } if (p.PropertyType == typeof(int)) { return ((int)value <= 0); } if (p.PropertyType == typeof(bool)) { return (!(bool)value); } if (p.PropertyType == typeof(Guid)) { return ((Guid)value) == Guid.Empty; } return true; }); }
Tuesday, September 25, 2018 11:09 PM
Answers
-
User-893317190 posted
Hi tvb2727,
If you want to use you customized attribute, you could try the code below.
My attribute.
[AttributeUsage(AttributeTargets.Property)] public class NeedCheckAttribute:Attribute { public bool needCheck { get; set; } public NeedCheckAttribute(bool needCheck) { this.needCheck = needCheck; } }
My model
public class MyDto { [NeedCheck(true)] public string Prop1 { get; set; } [NeedCheck(false)] public int? Prop2 { get; set; } [NeedCheck(true)] public bool? Prop3 { get; set; } [NeedCheck(false)] public Guid Prop4 { get; set; } }
My test code.
protected void Page_Load(object sender, EventArgs e) { TestAttribute(new MyDto()); } public static void TestAttribute(Object obj) { var t = obj.GetType(); var properties = t.GetProperties(); foreach (var item in properties) {
// if the property has NeedCheck attribute if (item.IsDefined(typeof(NeedCheckAttribute), false)) {
// get the NeedCheck attribute var attribute = item.GetCustomAttributes(typeof(NeedCheckAttribute), false).First();
// get the value the needCheck property bool flag= (bool)attribute.GetType().GetProperty("needCheck").GetValue(attribute);
// and you could apply your own validation according to the value of flag HttpContext.Current.Response.Write(item.Name+":"+flag+"<br/>"); } } }The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 27, 2018 7:59 AM
All replies
-
User753101303 posted
Hi,
You mean https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.requiredattribute?view=netframework-4.7.2 and other annotations ?
It is validated for you. It doesn't seems you would need to write your own code.
Wednesday, September 26, 2018 3:56 PM -
User-893317190 posted
Hi tvb2727,
If you want to use you customized attribute, you could try the code below.
My attribute.
[AttributeUsage(AttributeTargets.Property)] public class NeedCheckAttribute:Attribute { public bool needCheck { get; set; } public NeedCheckAttribute(bool needCheck) { this.needCheck = needCheck; } }
My model
public class MyDto { [NeedCheck(true)] public string Prop1 { get; set; } [NeedCheck(false)] public int? Prop2 { get; set; } [NeedCheck(true)] public bool? Prop3 { get; set; } [NeedCheck(false)] public Guid Prop4 { get; set; } }
My test code.
protected void Page_Load(object sender, EventArgs e) { TestAttribute(new MyDto()); } public static void TestAttribute(Object obj) { var t = obj.GetType(); var properties = t.GetProperties(); foreach (var item in properties) {
// if the property has NeedCheck attribute if (item.IsDefined(typeof(NeedCheckAttribute), false)) {
// get the NeedCheck attribute var attribute = item.GetCustomAttributes(typeof(NeedCheckAttribute), false).First();
// get the value the needCheck property bool flag= (bool)attribute.GetType().GetProperty("needCheck").GetValue(attribute);
// and you could apply your own validation according to the value of flag HttpContext.Current.Response.Write(item.Name+":"+flag+"<br/>"); } } }The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 27, 2018 7:59 AM