Пример #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);
        }
        internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
        {
            Assembly serializer = null;

            contract = null;
            string serializerName = null;

            // Packaged apps do not support loading generated serializers.
            if (Microsoft.Win32.UnsafeNativeMethods.IsPackagedProcess.Value)
            {
                return(null);
            }

            bool logEnabled = DiagnosticsSwitches.PregenEventLog.Enabled;

            // 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 = GetName(type.Assembly, true);
                serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
                // use strong name
                name.Name        = serializerName;
                name.CodeBase    = null;
                name.CultureInfo = CultureInfo.InvariantCulture;
                try {
                    serializer = Assembly.Load(name);
                }
                catch (Exception e) {
                    if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                    {
                        throw;
                    }
                    if (logEnabled)
                    {
                        Log(e.Message, EventLogEntryType.Information);
                    }
                    byte[] token = name.GetPublicKeyToken();
                    if (token != null && token.Length > 0)
                    {
                        // the parent assembly was signed, so do not try to LoadWithPartialName
                        return(null);
                    }
#pragma warning disable 618
                    serializer = Assembly.LoadWithPartialName(serializerName, null);
#pragma warning restore 618
                }
                if (serializer == null)
                {
#if !FEATURE_PAL // EventLog
                    if (logEnabled)
                    {
                        Log(Res.GetString(Res.XmlPregenCannotLoad, serializerName), EventLogEntryType.Information);
                    }
#endif //!FEATURE_PAL // EventLog
                    return(null);
                }
                if (!IsSerializerVersionMatch(serializer, type, defaultNamespace, null))
                {
#if !FEATURE_PAL // EventLog
                    if (logEnabled)
                    {
                        Log(Res.GetString(Res.XmlSerializerExpiredDetails, serializerName, type.FullName), EventLogEntryType.Error);
                    }
#endif //!FEATURE_PAL // EventLog
                    return(null);
                }
            }
            else
            {
                XmlSerializerAssemblyAttribute assemblyAttribute = (XmlSerializerAssemblyAttribute)attrs[0];
                if (assemblyAttribute.AssemblyName != null && assemblyAttribute.CodeBase != null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.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;
#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;
                    serializer     = Assembly.LoadFrom(serializerName);
                }
                else
                {
                    serializerName = type.Assembly.FullName;
                    serializer     = type.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);
            }

#if !FEATURE_PAL // EventLog
            if (logEnabled)
            {
                Log(Res.GetString(Res.XmlSerializerExpiredDetails, serializerName, type.FullName), EventLogEntryType.Error);
            }
#endif //!FEATURE_PAL // EventLog
            return(null);
        }
Пример #3
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;

            // check to see if we loading explicit pre-generated assembly
            object[] attrs = type.GetCustomAttributes(typeof(System.Xml.Serialization.XmlSerializerAssemblyAttribute), false);
            if (attrs.Length == 0)
            {
                // Guess serializer name: if parent assembly signed use strong name
                AssemblyName name = type.Assembly.GetName();
                serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
                // use strong name
                name.Name        = serializerName;
                name.CodeBase    = null;
                name.CultureInfo = CultureInfo.InvariantCulture;

                string serializerPath = null;

                try
                {
                    if (!string.IsNullOrEmpty(type.Assembly.Location))
                    {
                        serializerPath = Path.Combine(Path.GetDirectoryName(type.Assembly.Location), serializerName + ".dll");
                    }

                    if ((string.IsNullOrEmpty(serializerPath) || !File.Exists(serializerPath)) && !string.IsNullOrEmpty(Assembly.GetEntryAssembly().Location))
                    {
                        serializerPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), serializerName + ".dll");
                    }

                    if (!string.IsNullOrEmpty(serializerPath))
                    {
                        serializer = Assembly.LoadFile(serializerPath);
                    }
                }
                catch (Exception e)
                {
                    if (e is ThreadAbortException || e is StackOverflowException || 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);
                    }
                }

                if (serializer == null)
                {
                    if (XmlSerializer.Mode == SerializationMode.PreGenOnly)
                    {
                        throw new Exception(SR.Format(SR.FailLoadAssemblyUnderPregenMode, serializerName));
                    }

                    return(null);
                }

#if !FEATURE_SERIALIZATION_UAPAOT
                if (!IsSerializerVersionMatch(serializer, type, defaultNamespace))
                {
                    XmlSerializationEventSource.Log.XmlSerializerExpired(serializerName, type.FullName);
                    return(null);
                }
#endif
            }
            else
            {
                System.Xml.Serialization.XmlSerializerAssemblyAttribute assemblyAttribute = (System.Xml.Serialization.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;
#pragma warning disable 618
                    serializer = Assembly.LoadWithPartialName(serializerName);
#pragma warning restore 618
                }
                else if (assemblyAttribute.CodeBase != null && assemblyAttribute.CodeBase.Length > 0)
                {
                    serializerName = assemblyAttribute.CodeBase;
                    serializer     = Assembly.LoadFrom(serializerName);
                }
                else
                {
                    serializerName = type.Assembly.FullName;
                    serializer     = type.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);
        }
Пример #4
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;

            // check to see if we loading explicit pre-generated assembly
            object[] attrs = type.GetCustomAttributes(typeof(System.Xml.Serialization.XmlSerializerAssemblyAttribute), false);
            if (attrs.Length == 0)
            {
                // Guess serializer name: if parent assembly signed use strong name
                AssemblyName name = type.Assembly.GetName();
                serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
                // use strong name
                name.Name        = serializerName;
                name.CodeBase    = null;
                name.CultureInfo = CultureInfo.InvariantCulture;
                try
                {
                    serializer = Assembly.LoadFile(Path.GetFullPath(serializerName) + ".dll");
                }
                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);
                    }
                }
                if (serializer == null)
                {
                    return(null);
                }
            }
            else
            {
                System.Xml.Serialization.XmlSerializerAssemblyAttribute assemblyAttribute = (System.Xml.Serialization.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;
#pragma warning disable 618
                    serializer = Assembly.LoadWithPartialName(serializerName);
#pragma warning restore 618
                }
                else if (assemblyAttribute.CodeBase != null && assemblyAttribute.CodeBase.Length > 0)
                {
                    serializerName = assemblyAttribute.CodeBase;
                    serializer     = Assembly.LoadFrom(serializerName);
                }
                else
                {
                    serializerName = type.Assembly.FullName;
                    serializer     = type.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);
        }
 internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
 {
     Assembly serializer = null;
     contract = null;
     string partialName = null;
     bool enabled = DiagnosticsSwitches.PregenEventLog.Enabled;
     object[] customAttributes = type.GetCustomAttributes(typeof(XmlSerializerAssemblyAttribute), false);
     if (customAttributes.Length == 0)
     {
         AssemblyName parent = GetName(type.Assembly, true);
         partialName = Compiler.GetTempAssemblyName(parent, defaultNamespace);
         parent.Name = partialName;
         parent.CodeBase = null;
         parent.CultureInfo = CultureInfo.InvariantCulture;
         try
         {
             serializer = Assembly.Load(parent);
         }
         catch (Exception exception)
         {
             if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
             {
                 throw;
             }
             if (enabled)
             {
                 Log(exception.Message, EventLogEntryType.Information);
             }
             byte[] publicKeyToken = parent.GetPublicKeyToken();
             if ((publicKeyToken != null) && (publicKeyToken.Length > 0))
             {
                 return null;
             }
             serializer = Assembly.LoadWithPartialName(partialName, null);
         }
         if (serializer == null)
         {
             if (enabled)
             {
                 Log(Res.GetString("XmlPregenCannotLoad", new object[] { partialName }), EventLogEntryType.Information);
             }
             return null;
         }
         if (!IsSerializerVersionMatch(serializer, type, defaultNamespace, null))
         {
             if (enabled)
             {
                 Log(Res.GetString("XmlSerializerExpiredDetails", new object[] { partialName, type.FullName }), EventLogEntryType.Error);
             }
             return null;
         }
     }
     else
     {
         XmlSerializerAssemblyAttribute attribute = (XmlSerializerAssemblyAttribute) customAttributes[0];
         if ((attribute.AssemblyName != null) && (attribute.CodeBase != null))
         {
             throw new InvalidOperationException(Res.GetString("XmlPregenInvalidXmlSerializerAssemblyAttribute", new object[] { "AssemblyName", "CodeBase" }));
         }
         if (attribute.AssemblyName != null)
         {
             partialName = attribute.AssemblyName;
             serializer = Assembly.LoadWithPartialName(partialName, null);
         }
         else if ((attribute.CodeBase != null) && (attribute.CodeBase.Length > 0))
         {
             partialName = attribute.CodeBase;
             serializer = Assembly.LoadFrom(partialName);
         }
         else
         {
             partialName = type.Assembly.FullName;
             serializer = type.Assembly;
         }
         if (serializer == null)
         {
             throw new FileNotFoundException(null, partialName);
         }
     }
     Type typeFromAssembly = GetTypeFromAssembly(serializer, "XmlSerializerContract");
     contract = (XmlSerializerImplementation) Activator.CreateInstance(typeFromAssembly);
     if (contract.CanSerialize(type))
     {
         return serializer;
     }
     if (enabled)
     {
         Log(Res.GetString("XmlSerializerExpiredDetails", new object[] { partialName, type.FullName }), EventLogEntryType.Error);
     }
     return null;
 }