static Type ResolveHostObjectType(
            InteractiveDependencyResolver dependencyResolver,
            TargetCompilationConfiguration configuration,
            AgentType agentType)
        {
            if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(
                    ".NET Core", StringComparison.OrdinalIgnoreCase))
            {
                return(typeof(object));
            }

            using (var assemblyContext = new EvaluationAssemblyContext()) {
                string globalStateAssemblyCachePath = null;
                if (configuration.GlobalStateAssembly.Content.PEImage != null)
                {
                    globalStateAssemblyCachePath =
                        dependencyResolver.CacheRemoteAssembly(
                            configuration.GlobalStateAssembly);
                }

                var resolvedAssemblies = dependencyResolver
                                         .Resolve(new [] { configuration.GlobalStateAssembly })
                                         .Select(r => new AssemblyDefinition(r.AssemblyName, r.Path));

                assemblyContext.AddRange(resolvedAssemblies);

                var globalStateAssemblyDef = resolvedAssemblies.First(
                    assembly => ResolvedAssembly.NameEqualityComparer.Default.Equals(
                        assembly.Name,
                        configuration.GlobalStateAssembly.Name));

                netStandardAssembly = netStandardAssembly ??
                                      Assembly.ReflectionOnlyLoadFrom(
                    new FilePath(Assembly.GetExecutingAssembly().Location)
                    .ParentDirectory
                    .Combine("netstandard.dll"));

                xiAssembly = xiAssembly ??
                             Assembly.ReflectionOnlyLoadFrom(
                    new FilePath(Assembly.GetExecutingAssembly().Location)
                    .ParentDirectory
                    .Combine("Xamarin.Interactive.dll"));

                Assembly globalStateAssembly;
                if (globalStateAssemblyDef.Name.Name == "Xamarin.Interactive")
                {
                    globalStateAssembly = xiAssembly;
                }
                else
                {
                    globalStateAssembly = Assembly.ReflectionOnlyLoadFrom(
                        globalStateAssemblyCachePath ?? globalStateAssemblyDef.Content.Location);
                }

                return(globalStateAssembly.GetType(configuration.GlobalStateTypeName));
            }
        }
Example #2
0
        static Type ResolveHostObjectType(
            InteractiveDependencyResolver dependencyResolver,
            TargetCompilationConfiguration configuration)
        {
            if (configuration.GlobalStateType?.Name == null)
            {
                return(typeof(object));
            }

            if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(
                    ".NET Core", StringComparison.OrdinalIgnoreCase))
            {
                // .NET Core does not support reflection-only-load and due to a bad design decision
                // in Roslyn's scripting support, we cannot pass SRM or Roslyn type symbols to
                // the compiler configuration for it to know the global API shape.
                // cf. https://github.com/dotnet/corefx/issues/2800
                // cf. https://github.com/dotnet/roslyn/issues/20920
                //
                // Employ a hack here to get our base globals type working by checking the name
                // of the type and assuming it is or dervives from EvaluationContextGlobalObject.
                //
                // Unfortunately this still makes agent-specific API such as "MainWindow" unavailable
                // in the Web UX (since Roslyn is running under ASP.NET Core).
                if (dncGlobalStateTypeHackRegex.IsMatch(configuration.GlobalStateType.Name))
                {
                    return(typeof(EvaluationContextGlobalObject));
                }

                return(typeof(object));
            }

            using (var assemblyContext = new EvaluationAssemblyContext()) {
                string globalStateAssemblyCachePath = null;
                if (configuration.GlobalStateType.Assembly.Content.PEImage != null)
                {
                    globalStateAssemblyCachePath =
                        dependencyResolver.CacheRemoteAssembly(
                            configuration.GlobalStateType.Assembly);
                }

                var resolvedAssemblies = dependencyResolver
                                         .Resolve(new [] { configuration.GlobalStateType.Assembly })
                                         .Select(r => new AssemblyDefinition(r.AssemblyName, r.Path));

                assemblyContext.AddRange(resolvedAssemblies);

                var globalStateAssemblyDef = resolvedAssemblies.First(
                    assembly => ResolvedAssembly.NameEqualityComparer.Default.Equals(
                        assembly.Name,
                        configuration.GlobalStateType.Assembly.Name));

                netStandardAssembly = netStandardAssembly ??
                                      Assembly.ReflectionOnlyLoadFrom(
                    new FilePath(Assembly.GetExecutingAssembly().Location)
                    .ParentDirectory
                    .Combine("netstandard.dll"));

                xiAssembly = xiAssembly ??
                             Assembly.ReflectionOnlyLoadFrom(
                    new FilePath(Assembly.GetExecutingAssembly().Location)
                    .ParentDirectory
                    .Combine("Xamarin.Interactive.dll"));

                Assembly globalStateAssembly;
                if (globalStateAssemblyDef.Name.Name == "Xamarin.Interactive")
                {
                    globalStateAssembly = xiAssembly;
                }
                else
                {
                    globalStateAssembly = Assembly.ReflectionOnlyLoadFrom(
                        globalStateAssemblyCachePath ?? globalStateAssemblyDef.Content.Location);
                }

                return(globalStateAssembly.GetType(configuration.GlobalStateType.Name));
            }
        }