当前位置: 首页>>代码示例>>C#>>正文


C# TestContext.GenerateSource方法代码示例

本文整理汇总了C#中TestContext.GenerateSource方法的典型用法代码示例。如果您正苦于以下问题:C# TestContext.GenerateSource方法的具体用法?C# TestContext.GenerateSource怎么用?C# TestContext.GenerateSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TestContext的用法示例。


在下文中一共展示了TestContext.GenerateSource方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestNotReusedOnAssemblyDiffers

        internal static void TestNotReusedOnAssemblyDiffers(string projectLanguage)
        {
            var metadataSources = new[]
            {
                @"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class D {}",
                @"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class D {}"
            };

            using (var context = new TestContext(projectLanguage))
            {
                var projectId = ProjectId.CreateNewId();
                var metadataProject = context.CurrentSolution
                    .AddProject(projectId, "Metadata", "Metadata", LanguageNames.CSharp).GetProject(projectId)
                    .AddMetadataReference(TestReferences.NetFx.v4_0_30319.mscorlib)
                    .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release));

                var references = new List<MetadataReference>();

                foreach (var source in metadataSources)
                {
                    var newDoc = metadataProject.AddDocument("MetadataSource", source);
                    metadataProject = newDoc.Project;
                    references.Add(MetadataReference.CreateFromImage(metadataProject.GetCompilationAsync().Result.EmitToArray()));
                    metadataProject = metadataProject.RemoveDocument(newDoc.Id);
                }

                var project = context.DefaultProject.AddMetadataReference(references[0]);
                var a = context.GenerateSource("D", project);

                project = project.RemoveMetadataReference(references[0]).AddMetadataReference(references[1]);
                var b = context.GenerateSource("D", project);

                context.VerifyDocumentNotReused(a, b);
            }
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:35,代码来源:AbstractMetadataAsSourceTests.cs

示例2: TestSymbolIdMatchesMetadata

        internal static void TestSymbolIdMatchesMetadata(string projectLanguage)
        {
            var metadataSource = @"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C { }";
            var symbolName = "C";

            using (var context = new TestContext(projectLanguage, SpecializedCollections.SingletonEnumerable(metadataSource)))
            {
                var metadataSymbol = context.ResolveSymbol(symbolName);
                var metadataSymbolId = metadataSymbol.GetSymbolKey();
                var generatedFile = context.GenerateSource(symbolName);
                var generatedDocument = context.GetDocument(generatedFile);
                var generatedCompilation = generatedDocument.Project.GetCompilationAsync().Result;
                var generatedSymbol = generatedCompilation.Assembly.GetTypeByMetadataName(symbolName);
                Assert.False(generatedSymbol.Locations.Where(loc => loc.IsInSource).IsEmpty());
                Assert.True(SymbolKey.GetComparer(ignoreCase: true, ignoreAssemblyKeys: false).Equals(metadataSymbolId, generatedSymbol.GetSymbolKey()));
            }
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:17,代码来源:AbstractMetadataAsSourceTests.cs

示例3: FormatMetadataAsSource

 public void FormatMetadataAsSource()
 {
     using (var context = new TestContext(LanguageNames.CSharp))
     {
         var file = context.GenerateSource("System.Console", project: context.DefaultProject);
         var document = context.GetDocument(file);
         Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(document).Wait();
     }
 }
开发者ID:nemec,项目名称:roslyn,代码行数:9,代码来源:MetadataAsSourceTests.cs

示例4: TestNotReusedGeneratingForDifferentLanguage

        public void TestNotReusedGeneratingForDifferentLanguage()
        {
            using (var context = new TestContext(LanguageNames.CSharp))
            {
                var projectId = ProjectId.CreateNewId();
                var project = context.CurrentSolution.AddProject(projectId, "ProjectB", "ProjectB", LanguageNames.VisualBasic).GetProject(projectId)
                    .WithMetadataReferences(context.DefaultProject.MetadataReferences)
                    .WithCompilationOptions(new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                var a = context.GenerateSource(project: context.DefaultProject);
                var b = context.GenerateSource(project: project);
                context.VerifyDocumentNotReused(a, b);
            }
        }
开发者ID:nemec,项目名称:roslyn,代码行数:14,代码来源:MetadataAsSourceTests.cs

示例5: TestReuseRepeatGeneration

 public void TestReuseRepeatGeneration()
 {
     using (var context = new TestContext())
     {
         var a = context.GenerateSource();
         var b = context.GenerateSource();
         context.VerifyDocumentReused(a, b);
     }
 }
开发者ID:nemec,项目名称:roslyn,代码行数:9,代码来源:MetadataAsSourceTests.cs

示例6: TestReuseGenerateMemberOfGeneratedType

        public void TestReuseGenerateMemberOfGeneratedType()
        {
            var metadataSource = "public class C { public bool Is; }";

            using (var context = new TestContext(LanguageNames.CSharp, SpecializedCollections.SingletonEnumerable(metadataSource)))
            {
                var a = context.GenerateSource("C");
                var b = context.GenerateSource("C.Is");
                context.VerifyDocumentReused(a, b);
            }
        }
开发者ID:nemec,项目名称:roslyn,代码行数:11,代码来源:MetadataAsSourceTests.cs

示例7: TestThrowsOnGenerateNamespace

        public void TestThrowsOnGenerateNamespace()
        {
            var namespaceSymbol = CodeGenerationSymbolFactory.CreateNamespaceSymbol("Outerspace");

            using (var context = new TestContext())
            {
                Assert.Throws<ArgumentException>(() =>
                {
                    try
                    {
                        context.GenerateSource(namespaceSymbol);
                    }
                    catch (AggregateException ae)
                    {
                        throw ae.InnerException;
                    }
                });
            }
        }
开发者ID:nemec,项目名称:roslyn,代码行数:19,代码来源:MetadataAsSourceTests.cs

示例8: TestNavigationViaReducedExtensionMethodVB

        public void TestNavigationViaReducedExtensionMethodVB()
        {
            var metadata = @"Imports System.Runtime.CompilerServices
Namespace NS
    Public Module StringExtensions
        <Extension()>
        Public Sub M(ByVal o As String, x As Integer)
        End Sub
    End Module
End Namespace";
            var sourceWithSymbolReference = @"
Imports NS.StringExtensions
Public Module C
    Sub M()
        Dim s = ""Yay""
        s.[|M|](1)
    End Sub
End Module";
            var expected = [email protected]"#Region ""{FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null""
' {CodeAnalysisResources.InMemoryAssembly}
#End Region
Imports System.Runtime.CompilerServices

Namespace NS
    <Extension> 
    Public Module StringExtensions
        <Extension> Public Sub [|M|](o As String, x As Integer)
    End Module
End Namespace";

            using (var context = new TestContext(
                LanguageNames.VisualBasic,
                SpecializedCollections.SingletonEnumerable(metadata),
                includeXmlDocComments: false,
                sourceWithSymbolReference: sourceWithSymbolReference))
            {
                var navigationSymbol = context.GetNavigationSymbol();
                var metadataAsSourceFile = context.GenerateSource(navigationSymbol);
                context.VerifyResult(metadataAsSourceFile, expected);
            }
        }
开发者ID:nemec,项目名称:roslyn,代码行数:41,代码来源:MetadataAsSourceTests.cs

示例9: TestNavigationViaReducedExtensionMethodCS

        public void TestNavigationViaReducedExtensionMethodCS()
        {
            var metadata = @"using System;
public static class ObjectExtensions
{
    public static void M(this object o, int x) { }
}";
            var sourceWithSymbolReference = @"
class C
{
    void M()
    {
        new object().[|M|](5);
    }
}";
            var expected = [email protected]"#region {FeaturesResources.Assembly} ReferencedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// {CodeAnalysisResources.InMemoryAssembly}
#endregion
 
public static class ObjectExtensions
{{
    public static void [|M|](this object o, int x);
}}
";

            using (var context = new TestContext(
                LanguageNames.CSharp,
                SpecializedCollections.SingletonEnumerable(metadata),
                includeXmlDocComments: false,
                sourceWithSymbolReference: sourceWithSymbolReference))
            {
                var navigationSymbol = context.GetNavigationSymbol();
                var metadataAsSourceFile = context.GenerateSource(navigationSymbol);
                context.VerifyResult(metadataAsSourceFile, expected);
            }
        }
开发者ID:nemec,项目名称:roslyn,代码行数:36,代码来源:MetadataAsSourceTests.cs


注:本文中的TestContext.GenerateSource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。