Ejemplo n.º 1
0
        public void HelloWorld()
        {
            // declare
            // namespace MyApp.HelloWorld {
            var ns = NamespaceSignature.Parse("MyApp.HelloWorld");
            // public class Program {
            var programType = TypeSignature.Class("Program", ns, Accessibility.APublic);
            // public static int Main() {
            var mainMethod = MethodSignature.Static("Main", programType, Accessibility.APublic, returnType: TypeSignature.Int32);

            // get the Console.WriteLine reference
            var writeLineRef = MethodReference.FromLambda(() => Console.WriteLine(""));

            var body = new [] {
                Expression.StaticMethodCall(writeLineRef, Expression.Constant("Hello world!"))
            }.ToBlock(
                result: Expression.Constant(0)
                );

            var type = TypeDef.Empty(programType).AddMember(
                MethodDef.Create(mainMethod, body)
                );

            var cx = MetadataContext.Create();

            cx.AddType(type);
            var csharp = cx.EmitToString();

            check.CheckString(csharp, fileExtension: "cs");
        }
Ejemplo n.º 2
0
    public static FrameworkEntry Index(string frameworkName, IEnumerable <string> assemblyPaths)
    {
        var references = new List <MetadataReference>();

        foreach (var path in assemblyPaths)
        {
            var metadata = MetadataReference.CreateFromFile(path);
            references.Add(metadata);
        }

        var metadataContext = MetadataContext.Create(references);

        var assemblyEntries = new List <AssemblyEntry>();

        foreach (var assembly in metadataContext.Assemblies)
        {
            var entry = AssemblyEntry.Create(assembly);
            if (entry.Apis.Any())
            {
                assemblyEntries.Add(entry);
            }
        }

        return(FrameworkEntry.Create(frameworkName, assemblyEntries));
    }
Ejemplo n.º 3
0
    public async Task <PackageEntry> Index(string id, string version)
    {
        var dependencies     = new Dictionary <string, PackageArchiveReader>();
        var frameworkEntries = new List <FrameworkEntry>();

        try
        {
            using (var root = await _store.GetPackageAsync(id, version))
            {
                var targetNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                foreach (var item in GetReferenceItems(root))
                {
                    targetNames.Add(item.TargetFramework.GetShortFolderName());
                }

                var targets = targetNames.Select(NuGetFramework.Parse).ToArray();

                if (!targets.Any())
                {
                    return(null);
                }

                foreach (var target in targets)
                {
                    var referenceGroup = GetReferenceItems(root, target);

                    Debug.Assert(referenceGroup != null);

                    await GetDependenciesAsync(dependencies, root, target);

                    // Add references

                    var referenceMetadata = new List <MetadataReference>();

                    foreach (var path in referenceGroup.Items)
                    {
                        var metadata = await AssemblyStream.CreateAsync(root.GetStream(path), path);

                        referenceMetadata.Add(metadata);
                    }

                    // Add dependencies

                    var dependencyMetadata = new List <MetadataReference>();

                    foreach (var dependency in dependencies.Values)
                    {
                        var dependencyReferences = GetReferenceItems(dependency, target);
                        if (dependencyReferences != null)
                        {
                            foreach (var path in dependencyReferences.Items)
                            {
                                var metadata = await AssemblyStream.CreateAsync(dependency.GetStream(path), path);

                                dependencyMetadata.Add(metadata);
                            }
                        }
                    }

                    // Add framework

                    var platformPaths = GetPlatformSet(target);

                    if (platformPaths == null)
                    {
                        if (!IsKnownUnsupportedPlatform(target))
                        {
                            Console.WriteLine($"error: can't resolve platform references for {target}");
                        }
                        continue;
                    }

                    foreach (var path in platformPaths)
                    {
                        var metadata = MetadataReference.CreateFromFile(path);
                        dependencyMetadata.Add(metadata);
                    }

                    var metadataContext = MetadataContext.Create(referenceMetadata, dependencyMetadata);

                    var assemblyEntries = new List <AssemblyEntry>();

                    foreach (var reference in metadataContext.Assemblies)
                    {
                        var entry = AssemblyEntry.Create(reference);
                        assemblyEntries.Add(entry);
                    }

                    frameworkEntries.Add(FrameworkEntry.Create(target.GetShortFolderName(), assemblyEntries));
                }
            }

            return(PackageEntry.Create(id, version, frameworkEntries));
        }
        finally
        {
            foreach (var package in dependencies.Values)
            {
                package.Dispose();
            }
        }
    }