jeudi 16 janvier 2020

How to set null all field static class in c#

I have a problem in c# code, I have a static class and contain some filed, dynamic set value normally work perfect, but I want set null all or some filed via call function .

Write this code but not work and not Error

For all Field clear

public static void ClearCash()
{
     var flags = BindingFlags.NonPublic;
     foreach (var field in typeof(DB_Necessity).GetFields(flags))
     field.SetValue(field.FieldType, null);
 }

For some Filed Clear

public static void ClearCash(params string[] fieldName)
{
    var flags = BindingFlags.NonPublic;
    var x = typeof(DB_Necessity).GetFields(flags);
    var z = x.Where(A => fieldName.Any(F => F == A.Name)).ToList();
    foreach (var field in x)
    field.SetValue(field, null);
}
  • Static class
  • DB_Necessity is name class
  • using in asp.net mvc
  • Entity framework 4.7




Aucun commentaire:

Enregistrer un commentaire