jeudi 7 avril 2022

c# reflection for input string validation

I am new to C#. have a few doubts about using reflection for input string validation. below is the code .

here we pass string value as input and get properties of the string. it's actually Char and Int. So for Char property , in the line " var charVal = prop.GetValue(inputStr,new Object[] {0}); " I want know what exactly they are doing, are they trying to get the Char property value from object (input str) and validating if that is null .

and in the TryParse line, im getting only 1st character of the string using GetValue(inputStr,new Object[] {0}) . how can i get the complete string instead of getting characters one by one using GetValue().

var properties = inputStr.GetType().GetProperties(); foreach(PropertyInfo prop in properties) { var type = Nullable.GetUnderlyingType(prop.PropertyType)??prop.PropertyType;

if(type==typeof(char)
{
  char charTxt ='a';
  var charVal = prop.GetValue(inputStr,new Object[] {0});
  if(charVal == null)
    continue;
  if(char.TryParse(prop.GetValue(inputStr,new Object[] {0}).ToString()), out charTxt))
  {
  }
  else{
    return "invalid"
  }
}

}





Aucun commentaire:

Enregistrer un commentaire