/// <summary> /// Assuming that the member is already "legal", now check its parameters. /// </summary> /// <param name="_method">The method information that needs to be checked.</param> /// <param name="_isDeserializer">If TRUE, then check the member to see if it has an "CWorkingObject" parameter.</param> /// <returns>The surrogate information IF the member is a valid surrogate.</returns> private CImplicitSurrogate CreateImplicitSurrogate(MethodInfo _method, bool _isDeserializer) { var parms = _method.GetParameters(); var imp = new CImplicitSurrogate(_method, parms.Length); for (var i = 0; i < imp.m_parameterCount; i++) { var pi = parms[i]; if (_isDeserializer) { if (ProcessParameterType(pi, typeof(CWorkingObject), _method, ref imp.m_indexObject)) { continue; } } if (ProcessParameterType(pi, typeof(XmlNode), _method, ref imp.m_indexXml)) { continue; } if (ProcessParameterType(pi, typeof(CFramework), _method, ref imp.m_indexFramework)) { continue; } throw new XInvalidImplicitSerializer(m_type, _method, "an implicit (de)serializer may not have a parameter of type: " + pi.ParameterType.ToString()); } return(imp); }
/// <summary> /// Given a class member that has already been established as having a <see cref="AImplicitDeserializer"/> /// attribute, figure out if its a legitimate memeber to have that attribute. If it is, set the /// Implicit Deserializer property of this object to this member. If it is not, throw an exception. /// </summary> /// <param name="_member">The member of the type to examine</param> private void EstablishImplicitDeserializer(MemberInfo _member) { if (m_implicitDeserializer != null) { throw new XInvalidImplicitSerializer(m_type, _member, "a class can only have one implicit deserializer, and " + m_implicitDeserializer.m_method.Name + " has already been registered."); } var mi = _member as MethodInfo; if (mi == null) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Deserializers must be Methods, not " + _member.MemberType.ToString()); } if (!mi.IsStatic) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Deserializers must be static."); } if (mi.ReturnType != typeof(bool) && mi.ReturnType != typeof(void)) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Deserializers must return VOID or BOOL."); } var surrogate = CreateImplicitSurrogate(mi, true); if (surrogate.m_indexObject == -1) { throw new XInvalidImplicitSerializer(m_type, null, "There must exist a 'CWorkingObject' parameter to be a deserializer"); } if (surrogate.m_indexXml == -1) { throw new XInvalidImplicitSerializer(m_type, null, "There must exist an 'XmlNode' parameter to be a deserializer"); } m_implicitDeserializer = surrogate; }
/// <summary> /// Given a class member that has already been established as having a <see cref="AImplicitSerializer"/> /// attribute, figure out if its a legitimate member to have that attribute. If it is, set the /// Implicit Serializer property of this object to the member. If it is not, throw an exception. /// </summary> /// <param name="_member">The member of the type in question</param> private void EstablishImplicitSerializer(MemberInfo _member) { if (m_implicitSerializer != null) { throw new XInvalidImplicitSerializer(m_type, _member, "A class can only have one implicit serializer, and " + m_implicitSerializer.m_method.Name + " has already been established."); } var mi = _member as MethodInfo; if (mi == null) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Serializers must be Methods, not " + _member.MemberType.ToString()); } if (mi.IsStatic) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Serializers may not be static."); } if (mi.IsAbstract) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Serializers may not be abstract."); } if (mi.ReturnType != typeof(bool) && mi.ReturnType != typeof(void)) { throw new XInvalidImplicitSerializer(m_type, _member, "Implicit Serializers must return VOID or BOOL."); } var surrogate = CreateImplicitSurrogate(mi, false); if (surrogate.m_indexXml == -1) { throw new XInvalidImplicitSerializer(m_type, null, "There must exist an 'XmlElement' parameter to be a Serializer"); } m_implicitSerializer = surrogate; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // The rest of the file contains the code to actually serialize or deserialize using the data constructed above. // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Create the parameter array for an implicit surrogate using the data created by the rest of this class. /// </summary> /// <param name="_implicit">The implicit surrogate</param> /// <param name="_xmlToSerializeTo">The XML that the object will serialize itself to</param> /// <param name="_framework">The Context that the object will use (optionally) to serialize the data.</param> /// <param name="_object">The "Working Object" useful to a deserializer</param> /// <returns>an object[] containing the parameter data as dictated by the implicitSurrogate object</returns> private static object[] BuildParamArray(CImplicitSurrogate _implicit, XmlElement _xmlToSerializeTo, CFramework _framework, CWorkingObject _object) { var arr = new object[_implicit.m_parameterCount]; arr[_implicit.m_indexXml] = _xmlToSerializeTo; if (_implicit.m_indexFramework != -1) { arr[_implicit.m_indexFramework] = _framework; } if (_implicit.m_indexObject != -1) { arr[_implicit.m_indexObject] = _object; } return(arr); }