public void TestAnalyzerLoading_Error() { var analyzerSource = @" using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using System.Runtime.InteropServices; [DiagnosticAnalyzer(LanguageNames.CSharp)] [StructLayout(LayoutKind.Sequential, Size = 10000000)] public class TestAnalyzer : DiagnosticAnalyzer { public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { throw new NotImplementedException(); } } public override void Initialize(AnalysisContext context) { throw new NotImplementedException(); } }"; var dir = Temp.CreateDirectory(); dir.CopyFile(typeof(System.Reflection.Metadata.MetadataReader).Assembly.Location); dir.CopyFile(typeof(AppDomainUtils).Assembly.Location); var immutable = dir.CopyFile(typeof(ImmutableArray).Assembly.Location); var analyzer = dir.CopyFile(typeof(DiagnosticAnalyzer).Assembly.Location); var test = dir.CopyFile(typeof(FromFileLoader).Assembly.Location); var analyzerCompilation = CSharp.CSharpCompilation.Create( "MyAnalyzer", new SyntaxTree[] { CSharp.SyntaxFactory.ParseSyntaxTree(analyzerSource) }, new MetadataReference[] { TestReferences.NetStandard20.NetStandard, TestReferences.NetStandard20.SystemRuntimeRef, MetadataReference.CreateFromFile(immutable.Path), MetadataReference.CreateFromFile(analyzer.Path) }, new CSharp.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var analyzerFile = dir.CreateFile("MyAnalyzer.dll").WriteAllBytes(analyzerCompilation.EmitToArray()); var loadDomain = AppDomainUtils.Create("AnalyzerTestDomain", basePath: dir.Path); try { // Test analyzer load failure. var remoteTest = (RemoteAnalyzerFileReferenceTest)loadDomain.CreateInstanceAndUnwrap(typeof(RemoteAnalyzerFileReferenceTest).Assembly.FullName, typeof(RemoteAnalyzerFileReferenceTest).FullName); var exception = remoteTest.LoadAnalyzer(analyzerFile.Path); Assert.NotNull(exception as TypeLoadException); } finally { AppDomain.Unload(loadDomain); } }
public void TestAnalyzerLoading() { var dir = Temp.CreateDirectory(); var test = dir.CopyFile(typeof(FromFileLoader).Assembly.Location); var analyzerFile = TestHelpers.CreateCSharpAnalyzerAssemblyWithTestAnalyzer(dir, "MyAnalyzer"); var loadDomain = AppDomainUtils.Create("AnalyzerTestDomain", basePath: dir.Path); try { var remoteTest = (RemoteAnalyzerFileReferenceTest)loadDomain.CreateInstanceAndUnwrap(typeof(RemoteAnalyzerFileReferenceTest).Assembly.FullName, typeof(RemoteAnalyzerFileReferenceTest).FullName); remoteTest.SetAssert(RemoteAssert.Instance); remoteTest.TestSuccess(analyzerFile.Path); } finally { AppDomain.Unload(loadDomain); } }
private static RuntimeData CreateRuntimeData() { AppDomain appDomain = null; try { var appDomainProxyType = typeof(RuntimeAssemblyManager); var thisAssembly = appDomainProxyType.Assembly; appDomain = AppDomainUtils.Create("HostedRuntimeEnvironment"); var manager = (RuntimeAssemblyManager)appDomain.CreateInstanceAndUnwrap(thisAssembly.FullName, appDomainProxyType.FullName); return(new RuntimeData(manager, appDomain)); } catch { if (appDomain != null) { AppDomain.Unload(appDomain); } throw; } }
public void TestAnalyzerLoading_AppDomain() { var dir = Temp.CreateDirectory(); dir.CopyFile(typeof(AppDomainUtils).Assembly.Location); dir.CopyFile(typeof(RemoteAnalyzerFileReferenceTest).Assembly.Location); var analyzerFile = DesktopTestHelpers.CreateCSharpAnalyzerAssemblyWithTestAnalyzer(dir, "MyAnalyzer"); var loadDomain = AppDomainUtils.Create("AnalyzerTestDomain", basePath: dir.Path); try { // Test analyzer load success. var remoteTest = (RemoteAnalyzerFileReferenceTest)loadDomain.CreateInstanceAndUnwrap(typeof(RemoteAnalyzerFileReferenceTest).Assembly.FullName, typeof(RemoteAnalyzerFileReferenceTest).FullName); var exception = remoteTest.LoadAnalyzer(analyzerFile.Path); Assert.Null(exception); } finally { AppDomain.Unload(loadDomain); } }