private void InitializeContract(JsonContract contract)
{
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType);
if (containerAttribute != null)
{
contract.IsReference = containerAttribute._isReference;
}
#if !PocketPC && !NET20
else
{
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType);
// doesn't have a null value
if (dataContractAttribute != null && dataContractAttribute.IsReference)
contract.IsReference = true;
}
#endif
contract.Converter = ResolveContractConverter(contract.UnderlyingType);
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
|| contract.CreatedType.IsValueType)
{
#if !PocketPC && !SILVERLIGHT
contract.DefaultCreator = LateBoundDelegateFactory.CreateDefaultConstructor(contract.CreatedType);
#else
ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(contract.CreatedType, true);
contract.DefaultCreator = () =>
{
if (contract.CreatedType.IsValueType)
return Activator.CreateInstance(contract.CreatedType);
return constructorInfo.Invoke(null);
};
#endif
contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType &&
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
}
foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
// compact framework errors when getting parameters for a generic method
// lame, but generic methods should not be callbacks anyway
if (method.ContainsGenericParameters)
continue;
Type prevAttributeType = null;
ParameterInfo[] parameters = method.GetParameters();
#if !PocketPC && !NET20
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType))
{
contract.OnSerializing = method;
}
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType))
{
contract.OnSerialized = method;
}
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType))
{
contract.OnDeserializing = method;
}
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType))
{
contract.OnDeserialized = method;
}
#endif
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType))
{
contract.OnError = method;
}
}
}