Esempio n. 1
0
            /// <summary>
            /// Normalizes specified source path with respect to base file path.
            /// </summary>
            /// <param name="path">The source path to normalize. May be absolute or relative.</param>
            /// <param name="baseFilePath">Path of the source file that contains the <paramref name="path" /> (may also be relative), or null if not available.</param>
            /// <returns>
            /// Normalized path, or null if <paramref name="path" /> can't be normalized. The resulting path doesn't need to exist.
            /// </returns>
            public override string NormalizePath(string path, string baseFilePath)
            {
                // Try to see if it is import user type options
                ImportUserTypeOptions options = ImportUserTypeOptions.ParseString(path);

                if (options != null)
                {
                    return(options.Serialize());
                }

                // Normalize path
                string result = originalSourceResolver.NormalizePath(path, baseFilePath);

                return(result);
            }
Esempio n. 2
0
            /// <summary>
            /// Opens a <see cref="T:System.IO.Stream" /> that allows reading the content of the specified file.
            /// </summary>
            /// <param name="resolvedPath">Path returned by <see cref="M:Microsoft.CodeAnalysis.SourceReferenceResolver.ResolveReference(System.String,System.String)" />.</param>
            /// <returns></returns>
            public override Stream OpenRead(string resolvedPath)
            {
                ImportUserTypeCode code;

                if (codeGenCode.TryGetValue(resolvedPath, out code))
                {
                    return(new MemoryStream(Encoding.UTF8.GetBytes(code.Code)));
                }

                ImportUserTypeOptions options = ImportUserTypeOptions.ParseString(resolvedPath);

                if (options != null)
                {
                    code = GenerateCode(options);
                    AddCode(code);
                    return(new MemoryStream(Encoding.UTF8.GetBytes(code.Code)));
                }

                return(originalSourceResolver.OpenRead(resolvedPath));
            }
Esempio n. 3
0
            /// <summary>
            /// Resolves specified path with respect to base file path.
            /// </summary>
            /// <param name="path">The path to resolve. May be absolute or relative.</param>
            /// <param name="baseFilePath">Path of the source file that contains the <paramref name="path" /> (may also be relative), or null if not available.</param>
            /// <returns>
            /// Normalized path, or null if the file can't be resolved.
            /// </returns>
            public override string ResolveReference(string path, string baseFilePath)
            {
                // Try to see if it is import user type options
                ImportUserTypeOptions options = ImportUserTypeOptions.ParseString(path);

                if (options != null)
                {
                    return(options.Serialize());
                }

                // Do resolve reference
                string result = originalSourceResolver.ResolveReference(path, baseFilePath);

                if (string.IsNullOrEmpty(result))
                {
                    result = ResolvePath(path, baseFilePath);
                }

                return(result);
            }
Esempio n. 4
0
            /// <summary>
            /// Resolves the reference.
            /// </summary>
            /// <param name="reference">The reference.</param>
            /// <param name="baseFilePath">The base file path.</param>
            /// <param name="properties">The properties.</param>
            /// <returns></returns>
            public override ImmutableArray <PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
            {
                // Check if we are referencing CodeGen assembly
                ImportUserTypeAssembly codeGenAssembly;

                if (codeGenAssemblies.TryGetValue(reference, out codeGenAssembly))
                {
                    using (MemoryStream stream = new MemoryStream(codeGenAssembly.AssemblyBytes))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromStream(stream, properties, null, codeGenAssembly.AssemblyPath)));
                    }
                }

                // Check the previous resolver
                var result = previousResolver.ResolveReference(reference, baseFilePath, properties);

                if (result.Length > 0)
                {
                    return(result);
                }

                // Try to use file resolver
                try
                {
                    string path = ResolvePath(reference, baseFilePath);

                    if (!string.IsNullOrEmpty(path))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromFile(path, properties)));
                    }
                }
                catch
                {
                }

                // Check if reference holds xml for CodeGen
                ImportUserTypeOptions options = ImportUserTypeOptions.ParseString(reference);

                if (options != null)
                {
                    foreach (ImportUserTypeAssembly assembly in codeGenAssemblies.Values)
                    {
                        if (assembly.Options.Equals(options))
                        {
                            // TODO: Compare that used PDBs have same GUID.
                            codeGenAssembly = assembly;
                            break;
                        }
                    }

                    if (codeGenAssembly == null)
                    {
                        codeGenAssembly = GenerateAssembly(options);
                        AddAssembly(codeGenAssembly);
                    }

                    using (MemoryStream stream = new MemoryStream(codeGenAssembly.AssemblyBytes))
                    {
                        return(ImmutableArray.Create(MetadataReference.CreateFromStream(stream, properties, null, codeGenAssembly.AssemblyPath)));
                    }
                }

                return(ImmutableArray <PortableExecutableReference> .Empty);
            }