的代碼來測試有關DataContract屬性問題:
public class ObjectGraphValidator
{
List<object> _knownObjects = new List<object>();
Dictionary<Type, int> _encounteredCount = new Dictionary<Type, int>();
List<Type> _nonReferenceTypes = new List<Type>();
public void ValidateObjectGraph(object obj)
{
Type type = obj.GetType();
if (_encounteredCount.ContainsKey(type))
_encounteredCount[type]++;
else
{
_encounteredCount.Add(type, 1);
if (type.IsValueType)
_nonReferenceTypes.Add(type);
DataContractAttribute att = Attribute.GetCustomAttribute(type, typeof(DataContractAttribute)) as DataContractAttribute;
if (att == null || !att.IsReference)
_nonReferenceTypes.Add(type);
}
if (obj.GetType().IsValueType)
return;
if (_knownObjects.Contains(obj))
return;
_knownObjects.Add(obj);
if (obj is IEnumerable)
foreach (object obj2 in (obj as IEnumerable))
ValidateObjectGraph(obj2);
foreach (PropertyInfo property in type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
if (property.GetIndexParameters().Count() == 0)
{
object value = property.GetValue(obj, null);
if (value == null)
continue;
ValidateObjectGraph(value);
}
}
}
您可以確認這會導致該堆棧溢出實體有正確的'IsReference'屬性?即你在某處沒有錯過某些屬性? –
感謝您的建議,我正在編寫一個對象圖驗證器來告訴我對象圖中哪些類缺少該屬性。 – Alireza
運行測試..沒有自定義類有任何問題...我會發布測試代碼。 – Alireza