Exemple #1
0
        private AssemblyAndLocation Load(string reference)
        {
            MetadataShadowCopy copy = null;

            try
            {
                if (_shadowCopyProvider != null)
                {
                    // All modules of the assembly has to be copied at once to keep the metadata consistent.
                    // This API copies the xml doc file and keeps the memory-maps of the metadata.
                    // We don't need that here but presumably the provider is shared with the compilation API that needs both.
                    // Ideally the CLR would expose API to load Assembly given a byte* and not create their own memory-map.
                    copy = _shadowCopyProvider.GetMetadataShadowCopy(reference, MetadataImageKind.Assembly);
                }

                var result = _runtimeAssemblyLoader.LoadFromPath((copy != null) ? copy.PrimaryModule.FullPath : reference);

                if (_shadowCopyProvider != null && result.GlobalAssemblyCache)
                {
                    _shadowCopyProvider.SuppressShadowCopy(reference);
                }

                return(result);
            }
            catch (FileNotFoundException)
            {
                return(default(AssemblyAndLocation));
            }
            finally
            {
                // copy holds on the file handle, we need to keep the handle
                // open until the file is locked by the CLR assembly loader:
                copy?.DisposeFileHandles();
            }
        }
        public void Errors()
        {
            Assert.Throws <ArgumentNullException>(() => _provider.NeedsShadowCopy(null));
            Assert.Throws <ArgumentException>(() => _provider.NeedsShadowCopy("c:foo.dll"));
            Assert.Throws <ArgumentException>(() => _provider.NeedsShadowCopy("bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.NeedsShadowCopy(@"\bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.NeedsShadowCopy(@"../bar.dll"));

            Assert.Throws <ArgumentNullException>(() => _provider.SuppressShadowCopy(null));
            Assert.Throws <ArgumentException>(() => _provider.SuppressShadowCopy("c:foo.dll"));
            Assert.Throws <ArgumentException>(() => _provider.SuppressShadowCopy("bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.SuppressShadowCopy(@"\bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.SuppressShadowCopy(@"../bar.dll"));

            Assert.Throws <ArgumentNullException>(() => _provider.GetReference(null));
            Assert.Throws <ArgumentException>(() => _provider.GetReference("c:foo.dll"));
            Assert.Throws <ArgumentException>(() => _provider.GetReference("bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.GetReference(@"\bar.dll"));
            Assert.Throws <ArgumentException>(() => _provider.GetReference(@"../bar.dll"));

            Assert.Throws <ArgumentOutOfRangeException>(() => _provider.GetMetadataShadowCopy(@"c:\foo.dll", (MetadataImageKind)Byte.MaxValue));
            Assert.Throws <ArgumentNullException>(() => _provider.GetMetadataShadowCopy(null, MetadataImageKind.Assembly));
            Assert.Throws <ArgumentException>(() => _provider.GetMetadataShadowCopy("c:foo.dll", MetadataImageKind.Assembly));
            Assert.Throws <ArgumentException>(() => _provider.GetMetadataShadowCopy("bar.dll", MetadataImageKind.Assembly));
            Assert.Throws <ArgumentException>(() => _provider.GetMetadataShadowCopy(@"\bar.dll", MetadataImageKind.Assembly));
            Assert.Throws <ArgumentException>(() => _provider.GetMetadataShadowCopy(@"../bar.dll", MetadataImageKind.Assembly));

            Assert.Throws <ArgumentOutOfRangeException>(() => _provider.GetMetadata(@"c:\foo.dll", (MetadataImageKind)Byte.MaxValue));
            Assert.Throws <ArgumentNullException>(() => _provider.GetMetadata(null, MetadataImageKind.Assembly));
            Assert.Throws <ArgumentException>(() => _provider.GetMetadata("c:foo.dll", MetadataImageKind.Assembly));
        }