Chcę uzyskać wszystkie pola, które mają wartości zerowe ale aint nawet uzyskanie wszelkich polach:dostać pola z refleksji
[Serializable()]
public class BaseClass
{
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
FixNullString(this);
}
public void FixNullString(object type)
{
try
{
var properties = type.GetType().GetFields();
foreach (var property in from property in properties
let oldValue = property.GetValue(type)
where oldValue == null
select property)
{
property.SetValue(type, GetDefaultValue(property));
}
}
catch (Exception)
{
}
}
public object GetDefaultValue(System.Reflection.FieldInfo value)
{
try
{
if (value.FieldType == typeof(string))
return "";
if (value.FieldType == typeof(bool))
return false;
if (value.FieldType == typeof(int))
return 0;
if (value.FieldType == typeof(decimal))
return 0;
if (value.FieldType == typeof(DateTime))
return new DateTime();
}
catch (Exception)
{
}
return null;
}
}
A potem mam klasy:
[Serializable()]
public class Settings : BaseClass
{
public bool Value1 { get; set; }
public bool Value2 { get; set; }
}
Ale kiedy przychodzi do
var properties = type.GetType().GetFields();
następnie otrzymuję 0 pól, powinien znaleźć 2 pola.
Czy typ.getType(). GetFields() jest niewłaściwy w użyciu? czy wysyłam niewłaściwą klasę do klasy bazowej?