Created
March 24, 2014 13:58
-
-
Save rodrimaia/9740602 to your computer and use it in GitHub Desktop.
PreventNulls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AbstractViewModel<T> | |
{ | |
public AbstractViewModel<T> PreventNulls() | |
{ | |
// Verifica se alguma propriedade eh nula e insere um valor valido | |
var properties = GetType().GetProperties(); | |
foreach (var property in properties ) | |
{ | |
var value = property.GetValue(this, null); | |
if (value == null) | |
{ | |
if (property.PropertyType == typeof (string)) | |
{ | |
property.SetValue(this, string.Empty); | |
} | |
else if (typeof (IList<ComboItemViewModel>).IsAssignableFrom(property.PropertyType)) | |
{ | |
//se for lista nula | |
property.SetValue(this, new List<ComboItemViewModel>()); | |
} | |
else if (typeof (IList<ComboItemChildrenViewModel>).IsAssignableFrom(property.PropertyType)) | |
{ | |
//se for lista nula | |
property.SetValue(this, new List<ComboItemChildrenViewModel>()); | |
} | |
else if (property.PropertyType == typeof (bool)) | |
{ | |
// se for boolean | |
property.SetValue(this, false); | |
} | |
else if (typeof (IList<string>).IsAssignableFrom(property.PropertyType)) | |
{ | |
property.SetValue(this, new List<string>() {""}); | |
} | |
} | |
else if (property.PropertyType == typeof (string) && value.ToString().Contains('"')) | |
{ | |
property.SetValue(this,value.ToString().Replace('"',' ')); | |
} | |
else if (typeof (IList<ComboItemViewModel>).IsAssignableFrom(property.PropertyType)) | |
{ | |
var campaignsList = (IList<ComboItemViewModel>)value; | |
foreach (var comboItemViewModel in campaignsList.Where(comboItemViewModel => comboItemViewModel.Name.Contains('"'))) | |
{ | |
comboItemViewModel.Name = comboItemViewModel.Name.Replace('"', ' '); | |
} | |
} | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment