System.Xml.Serialization.XmlSerializerImplementation.CanSerialize C# (CSharp) Method

CanSerialize() public method

public CanSerialize ( Type type ) : bool
type System.Type
return bool
        public virtual bool CanSerialize(Type type) { throw new NotSupportedException(); }
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializerImplementation.GetSerializer"]/*' />

Usage Example

Example #1
0
        /// <devdoc>
        ///    <para>
        ///    Attempts to load pre-generated serialization assembly.
        ///    First check for the [XmlSerializerAssembly] attribute
        ///    </para>
        /// </devdoc>
        // SxS: This method does not take any resource name and does not expose any resources to the caller.
        // It's OK to suppress the SxS warning.
        internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
        {
            Assembly serializer = null;

            contract = null;
            string serializerName = null;

            //BinCompat TODO: Check if this needs to come back
            // Packaged apps do not support loading generated serializers.

            /*if (Microsoft.Win32.UnsafeNativeMethods.IsPackagedProcess.Value)
             * {
             *  return null;
             * }*/

            // check to see if we loading explicit pre-generated assembly
            object[] attrs = type.GetCustomAttributes(typeof(XmlSerializerAssemblyAttribute), false);
            if (attrs.Length == 0)
            {
                // Guess serializer name: if parent assembly signed use strong name
                AssemblyName name = type.GetTypeInfo().Assembly.GetName();
                serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
                // use strong name
                name.Name = serializerName;
                //BinCompat TODO: Check if this was actually needed
                //name.CodeBase = null;
                //name.CultureInfo = CultureInfo.InvariantCulture;
                try
                {
                    serializer = Assembly.Load(name);
                }
                catch (Exception e)
                {
                    if (e is OutOfMemoryException)
                    {
                        throw;
                    }
                    byte[] token = name.GetPublicKeyToken();
                    if (token != null && token.Length > 0)
                    {
                        // the parent assembly was signed, so do not try to LoadWithPartialName
                        return(null);
                    }
                    //BinCompat TODO: Test this
                    throw new NotImplementedException(string.Format("Could not load assembly ", name));

                    /*#pragma warning disable 618
                    *                   serializer = Assembly.LoadWithPartialName(serializerName, null);
                    #pragma warning restore 618*/
                }
                if (serializer == null)
                {
                    return(null);
                }
            }
            else
            {
                XmlSerializerAssemblyAttribute assemblyAttribute = (XmlSerializerAssemblyAttribute)attrs[0];
                if (assemblyAttribute.AssemblyName != null && assemblyAttribute.CodeBase != null)
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlPregenInvalidXmlSerializerAssemblyAttribute, "AssemblyName", "CodeBase"));
                }

                // found XmlSerializerAssemblyAttribute attribute, it should have all needed information to load the pre-generated serializer
                if (assemblyAttribute.AssemblyName != null)
                {
                    serializerName = assemblyAttribute.AssemblyName;
                    //BinCompat TODO: Test this
                    throw new NotImplementedException(string.Format("Could not load assembly ", serializerName));

                    /*#pragma warning disable 618
                    *                   serializer = Assembly.LoadWithPartialName(serializerName, null);
                    #pragma warning restore 618*/
                }
                else if (assemblyAttribute.CodeBase != null && assemblyAttribute.CodeBase.Length > 0)
                {
                    serializerName = assemblyAttribute.CodeBase;
                    //BinCompat TODO: Test this
                    throw new NotImplementedException(string.Format("Could not load assembly ", serializerName));
                    //serializer = Assembly.LoadFrom(serializerName);
                }
                else
                {
                    serializerName = type.GetTypeInfo().Assembly.FullName;
                    serializer     = type.GetTypeInfo().Assembly;
                }
                if (serializer == null)
                {
                    throw new FileNotFoundException(null, serializerName);
                }
            }
            Type contractType = GetTypeFromAssembly(serializer, "XmlSerializerContract");

            contract = (XmlSerializerImplementation)Activator.CreateInstance(contractType);
            if (contract.CanSerialize(type))
            {
                return(serializer);
            }

            return(null);
        }
All Usage Examples Of System.Xml.Serialization.XmlSerializerImplementation::CanSerialize
XmlSerializerImplementation