public static void DoitNow(object obj)
{
Queue<PropertyInfo> properties = new Queue<PropertyInfo>(obj.GetType().GetProperties());
while (properties.Count != 0)
{
PropertyInfo property = properties.Dequeue();
if (property.GetValue(obj) is string)
{
string stringField = property.GetValue(obj).ToString();
String sanitizedField = sanitizeSensitiveString(stringField);
property.SetValue(obj, sanitizedField);
}
else if (property.GetValue(obj) is IList)
{
IList y = (IList)property.GetValue(obj);
if (y.Count != 0)
{
if (y[0] is string)
{
for (int i = 0; i < y.Count; i++)
y[i] = sanitizeSensitiveString((string)y[i]);
}
else if (IsNested(y[0]))
{
for (int i = 0; i < y.Count; i++)
{
DoitNow(y[i]);
}
}
}
}
else if (IsNested(property.GetValue(obj)))
{
var l = property.GetValue(obj);
DoitNow(l);
}
}
}
public static string sanitizeSensitiveString(string sensitiveString)
{
return Regex.Replace(sensitiveString, @"\d{6}-?\d{4}[0-9]*", " XXXXXX-XXXX ");
}
public static bool IsNested(object value)
{
Type t = value.GetType();
if (t.IsPrimitive || value is string)
{
return false;
}
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance);
if (fields.Any())
{
return true;
}
return false;
}
public static void DoitNow(object obj)
{
Queue<PropertyInfo> properties = new Queue<PropertyInfo>(obj.GetType().GetProperties());
while (properties.Count != 0)
{
PropertyInfo property = properties.Dequeue();
if (property.GetValue(obj) is string)
{
string stringField = property.GetValue(obj).ToString();
String sanitizedField = sanitizeSensitiveString(stringField);
property.SetValue(obj, sanitizedField);
}
else if (property.GetValue(obj) is IList)
{
IList y = (IList)property.GetValue(obj);
if (y.Count != 0)
{
if (y[0] is string)
{
for (int i = 0; i < y.Count; i++)
y[i] = sanitizeSensitiveString((string)y[i]);
}
else if (IsNested(y[0]))
{
for (int i = 0; i < y.Count; i++)
{
DoitNow(y[i]);
}
}
}
}
else if (IsNested(property.GetValue(obj)))
{
var l = property.GetValue(obj);
DoitNow(l);
}
}
}
public static string sanitizeSensitiveString(string sensitiveString)
{
return Regex.Replace(sensitiveString, @"\d{6}-?\d{4}[0-9]*", " XXXXXX-XXXX ");
}
public static bool IsNested(object value)
{
Type t = value.GetType();
if (t.IsPrimitive || value is string)
{
return false;
}
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance);
if (fields.Any())
{
return true;
}
return false;
}
Now, the method DoitNowDoitNow is the core method here, it takes any object as a parameter, and uses projection to look through all the properties of it. If a field is a string field, we use the method sanitizeSensitiveStringsanitizeSensitiveString to sanitize the string. IIf a property is itself a object (I use the method isNestedisNested to determine this) I will call the method recursively on it. If a property is a List type, I look into the list and sanitize if it contains stirngsstrings, and recurse if it contains complex objects.
I would like to get some tips on my IsnestedIsNested method, am I using projection right? In general am I using projection In a way that makes sense to accesaccess the fields and modify them?
Also, In order to be able to operate with many list types, I use the IListIList interface, however this has led me to using for-loops in a way that I find very un-elegant, can I replace some of my loops with some functional type function calls? I also use if (list.count != 0) to test if a list is empty, I just find this plain stupid.