साथ खेतों मैं शून्य मान है कि सभी क्षेत्रों पाने के लिए चाहते हैं, लेकिन मैं किसी भी क्षेत्रों में हो रही है नहीं:प्रतिबिंब
[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;
}
}
और फिर मैं एक वर्ग है:
[Serializable()]
public class Settings : BaseClass
{
public bool Value1 { get; set; }
public bool Value2 { get; set; }
}
लेकिन जब मैं आता है
var properties = type.GetType().GetFields();
तो मुझे 0 फ़ील्ड मिलते हैं, इसे 2 फ़ील्ड मिलना चाहिए।
टाइप.get टाइप()। GetFields() का उपयोग करने में गलत है? या मैं गलत वर्ग में बेस क्लास में भेज रहा हूं?