本文整理汇总了C#中System.IO.MemoryStream.ToImmutable方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.ToImmutable方法的具体用法?C# MemoryStream.ToImmutable怎么用?C# MemoryStream.ToImmutable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.ToImmutable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitToArray
internal static ImmutableArray<byte> EmitToArray(
this Compilation compilation,
bool metadataOnly = false,
CompilationTestData testData = null,
DiagnosticDescription[] expectedWarnings = null)
{
var stream = new MemoryStream();
var emitResult = compilation.Emit(
peStream: stream,
outputName: null,
pdbFilePath: null,
pdbStream: null,
xmlDocumentationStream: null,
cancellationToken: default(CancellationToken),
win32Resources: null,
manifestResources: null,
metadataOnly: metadataOnly,
testData: testData);
Assert.True(emitResult.Success, "Diagnostics:\r\n" + string.Join("\r\n, ", emitResult.Diagnostics.Select(d => d.ToString())));
if (expectedWarnings != null)
{
emitResult.Diagnostics.Verify(expectedWarnings);
}
return stream.ToImmutable();
}
示例2: EmitToArray
internal static ImmutableArray<byte> EmitToArray(
this Compilation compilation,
EmitOptions options = null,
CompilationTestData testData = null,
DiagnosticDescription[] expectedWarnings = null)
{
var stream = new MemoryStream();
MemoryStream pdbStream =
(compilation.Options.OptimizationLevel == OptimizationLevel.Debug) && !CLRHelpers.IsRunningOnMono()
? new MemoryStream()
: null;
var emitResult = compilation.Emit(
peStream: stream,
pdbStream: pdbStream,
xmlDocumentationStream: null,
win32Resources: null,
manifestResources: null,
options: options,
testData: testData,
getHostDiagnostics: null,
cancellationToken: default(CancellationToken));
Assert.True(emitResult.Success, "Diagnostics:\r\n" + string.Join("\r\n", emitResult.Diagnostics.Select(d => d.ToString())));
if (expectedWarnings != null)
{
emitResult.Diagnostics.Verify(expectedWarnings);
}
return stream.ToImmutable();
}
示例3: EmitToArray
internal static ImmutableArray<byte> EmitToArray(
this Compilation compilation,
EmitOptions options = null,
CompilationTestData testData = null,
DiagnosticDescription[] expectedWarnings = null,
Stream pdbStream = null,
IMethodSymbol debugEntryPoint = null,
Stream sourceLinkStream = null,
IEnumerable<EmbeddedText> embeddedTexts = null)
{
var peStream = new MemoryStream();
if (pdbStream == null && compilation.Options.OptimizationLevel == OptimizationLevel.Debug && options?.DebugInformationFormat != DebugInformationFormat.Embedded)
{
if (MonoHelpers.IsRunningOnMono())
{
options = (options ?? EmitOptions.Default).WithDebugInformationFormat(DebugInformationFormat.PortablePdb);
}
pdbStream = new MemoryStream();
}
var emitResult = compilation.Emit(
peStream: peStream,
pdbStream: pdbStream,
xmlDocumentationStream: null,
win32Resources: null,
manifestResources: null,
options: options,
debugEntryPoint: debugEntryPoint,
sourceLinkStream: sourceLinkStream,
embeddedTexts: embeddedTexts,
testData: testData,
cancellationToken: default(CancellationToken));
Assert.True(emitResult.Success, "Diagnostics:\r\n" + string.Join("\r\n", emitResult.Diagnostics.Select(d => d.ToString())));
if (expectedWarnings != null)
{
emitResult.Diagnostics.Verify(expectedWarnings);
}
return peStream.ToImmutable();
}
示例4: EmitDifference
internal static CompilationDifference EmitDifference(
this Compilation compilation,
EmitBaseline baseline,
ImmutableArray<SemanticEdit> edits,
CompilationTestData testData = null)
{
testData = testData ?? new CompilationTestData();
var pdbName = Path.ChangeExtension(compilation.SourceModule.Name, "pdb");
// keep the stream open, it's passed to CompilationDifference
var pdbStream = new MemoryStream();
using (MemoryStream mdStream = new MemoryStream(), ilStream = new MemoryStream())
{
var updatedMethodTokens = new List<uint>();
var result = compilation.EmitDifference(
baseline,
edits,
mdStream,
ilStream,
pdbStream,
updatedMethodTokens,
testData,
default(CancellationToken));
pdbStream.Seek(0, SeekOrigin.Begin);
return new CompilationDifference(
mdStream.ToImmutable(),
ilStream.ToImmutable(),
pdbStream,
result.Baseline,
testData,
result,
updatedMethodTokens.ToImmutableArray());
}
}
示例5: CreateSnPublicKeyBlob
private static ImmutableArray<byte> CreateSnPublicKeyBlob(BlobHeader header, RsaPubKey rsa, byte[] pubKeyData)
{
var snPubKey = new SnPublicKeyBlob()
{
SigAlgId = AlgorithmId.RsaSign,
HashAlgId = AlgorithmId.Sha,
PublicKeySize = (UInt32)(s_offsetToKeyData + pubKeyData.Length)
};
using (var ms = new MemoryStream(160))
using (var binaryWriter = new BinaryWriter(ms))
{
binaryWriter.Write(snPubKey.SigAlgId);
binaryWriter.Write(snPubKey.HashAlgId);
binaryWriter.Write(snPubKey.PublicKeySize);
binaryWriter.Write(header.Type);
binaryWriter.Write(header.Version);
binaryWriter.Write(header.Reserved);
binaryWriter.Write(header.AlgId);
binaryWriter.Write(rsa.Magic);
binaryWriter.Write(rsa.BitLen);
binaryWriter.Write(rsa.PubExp);
binaryWriter.Write(pubKeyData);
return ms.ToImmutable();
}
}
示例6: AddResourceToModule
public void AddResourceToModule()
{
for (int metadataOnlyIfNonzero = 0; metadataOnlyIfNonzero < 2; metadataOnlyIfNonzero++)
{
var metadataOnly = metadataOnlyIfNonzero != 0;
Func<Compilation, Stream, ResourceDescription[], CodeAnalysis.Emit.EmitResult> emit;
emit = (c, s, r) => c.Emit(s, manifestResources: r, options: new EmitOptions(metadataOnly: metadataOnly));
var sourceTree = SyntaxFactory.ParseSyntaxTree("");
// Do not name the compilation, a unique guid is used as a name by default. It prevents conflicts with other assemblies loaded via Assembly.ReflectionOnlyLoad.
var c1 = CSharpCompilation.Create(
Guid.NewGuid().ToString(),
new[] { sourceTree },
new[] { MscorlibRef },
TestOptions.ReleaseModule);
var resourceFileName = "RoslynResourceFile.foo";
var output = new MemoryStream();
const string r1Name = "some.dotted.NAME";
const string r2Name = "another.DoTtEd.NAME";
var arrayOfEmbeddedData = new byte[] { 1, 2, 3, 4, 5 };
var resourceFileData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = emit(c1, output,
new ResourceDescription[]
{
new ResourceDescription(r1Name, () => new MemoryStream(arrayOfEmbeddedData), true),
new ResourceDescription(r2Name, resourceFileName, () => new MemoryStream(resourceFileData), false)
});
Assert.False(result.Success);
Assert.NotEmpty(result.Diagnostics.Where(x => x.Code == (int)ErrorCode.ERR_CantRefResource));
result = emit(c1, output,
new ResourceDescription[]
{
new ResourceDescription(r2Name, resourceFileName, () => new MemoryStream(resourceFileData), false),
new ResourceDescription(r1Name, () => new MemoryStream(arrayOfEmbeddedData), true)
});
Assert.False(result.Success);
Assert.NotEmpty(result.Diagnostics.Where(x => x.Code == (int)ErrorCode.ERR_CantRefResource));
result = emit(c1, output,
new ResourceDescription[]
{
new ResourceDescription(r2Name, resourceFileName, () => new MemoryStream(resourceFileData), false)
});
Assert.False(result.Success);
Assert.NotEmpty(result.Diagnostics.Where(x => x.Code == (int)ErrorCode.ERR_CantRefResource));
var c_mod1 = CSharpCompilation.Create(
Guid.NewGuid().ToString(),
new[] { sourceTree },
new[] { MscorlibRef },
TestOptions.ReleaseModule);
var output_mod1 = new MemoryStream();
result = emit(c_mod1, output_mod1,
new ResourceDescription[]
{
new ResourceDescription(r1Name, () => new MemoryStream(arrayOfEmbeddedData), true)
});
Assert.True(result.Success);
var mod1 = ModuleMetadata.CreateFromImage(output_mod1.ToImmutable());
var ref_mod1 = mod1.GetReference();
Assert.Equal(ManifestResourceAttributes.Public, mod1.Module.GetEmbeddedResourcesOrThrow()[0].Attributes);
{
var c2 = CreateCompilationWithMscorlib(sourceTree, new[] { ref_mod1 }, TestOptions.ReleaseDll);
var output2 = new MemoryStream();
var result2 = c2.Emit(output2);
Assert.True(result2.Success);
var assembly = System.Reflection.Assembly.ReflectionOnlyLoad(output2.ToArray());
assembly.ModuleResolve += (object sender, ResolveEventArgs e) =>
{
if (e.Name.Equals(c_mod1.SourceModule.Name))
{
return assembly.LoadModule(e.Name, output_mod1.ToArray());
}
return null;
};
string[] resourceNames = assembly.GetManifestResourceNames();
Assert.Equal(1, resourceNames.Length);
var rInfo = assembly.GetManifestResourceInfo(r1Name);
Assert.Equal(System.Reflection.ResourceLocation.Embedded, rInfo.ResourceLocation);
Assert.Equal(c_mod1.SourceModule.Name, rInfo.FileName);
var rData = assembly.GetManifestResourceStream(r1Name);
var rBytes = new byte[rData.Length];
//.........这里部分代码省略.........
示例7: ImportScopeEquality
public void ImportScopeEquality()
{
var sources = new[] { @"
extern alias A;
using System;
using C = System;
namespace N.M
{
using System.Collections;
class C1 { void F() {} }
}
namespace N.M
{
using System.Collections;
class C2 { void F() {} }
}
", @"
extern alias A;
using System;
using C = System;
namespace N.M
{
using System.Collections;
class C3 { void F() {} }
}
namespace N.M
{
using System.Collections.Generic;
class C4 { void F() {} }
}
", @"
extern alias A;
using System;
using D = System;
namespace N.M
{
using System.Collections;
class C5 { void F() {} }
}
", @"
extern alias A;
using System;
class C6 { void F() {} }
" };
var c = CreateCompilationWithMscorlib(sources, new[] { SystemCoreRef.WithAliases(ImmutableArray.Create("A")) });
var pdbStream = new MemoryStream();
c.EmitToArray(EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.PortablePdb), pdbStream: pdbStream);
var pdbImage = pdbStream.ToImmutable();
using (var metadata = new PinnedMetadata(pdbImage))
{
var mdReader = metadata.Reader;
var writer = new StringWriter();
var mdVisualizer = new MetadataVisualizer(mdReader, writer);
mdVisualizer.WriteImportScope();
AssertEx.AssertEqualToleratingWhitespaceDifferences(@"
ImportScope (index: 0x35, size: 36):
=============================================================================================
Parent Imports
=============================================================================================
1: nil (ImportScope) 'A' (#1) = 0x23000002 (AssemblyRef)
2: 0x35000001 (ImportScope) Extern Alias 'A' (#1), 'System' (#7)
3: 0x35000001 (ImportScope) Extern Alias 'A' (#1), 'System' (#7), 'C' (#1d) = 'System' (#7)
4: 0x35000003 (ImportScope) nil
5: 0x35000004 (ImportScope) 'System.Collections' (#27)
6: 0x35000004 (ImportScope) 'System.Collections.Generic' (#4b)
7: 0x35000001 (ImportScope) Extern Alias 'A' (#1), 'System' (#7), 'D' (#69) = 'System' (#7)
8: 0x35000007 (ImportScope) nil
9: 0x35000008 (ImportScope) 'System.Collections' (#27)
", writer.ToString());
}
}
示例8: OverloadResolutionWithStaticType
public void OverloadResolutionWithStaticType()
{
var vbSource = @"
Imports System
Namespace Microsoft.VisualBasic.CompilerServices
<System.AttributeUsage(System.AttributeTargets.Class, Inherited:=False, AllowMultiple:=False)>
Public NotInheritable Class StandardModuleAttribute
Inherits System.Attribute
Public Sub New()
MyBase.New()
End Sub
End Class
End Namespace
Public Module M
Sub Foo(x as Action(Of String))
End Sub
Sub Foo(x as Action(Of GC))
End Sub
End Module
";
var vbProject = VisualBasic.VisualBasicCompilation.Create(
"VBProject",
references: new[] { MscorlibRef },
syntaxTrees: new[] { VisualBasic.VisualBasicSyntaxTree.ParseText(vbSource) });
var csSource = @"
class Program
{
static void Main()
{
M.Foo(x => { });
}
}
";
var metadataStream = new MemoryStream();
var emitResult = vbProject.EmitMetadataOnly(metadataStream);
Assert.True(emitResult.Success);
var csProject = CreateCompilationWithMscorlib(
Parse(csSource),
new[] { new MetadataImageReference(metadataStream.ToImmutable()) });
Assert.Equal(0, csProject.GetDiagnostics().Count());
}
示例9: GetSymAttributeByVersion
public void GetSymAttributeByVersion()
{
var source1 = @"
public class C
{
public static void M()
{
int x = 1;
}
}";
var source2 = @"
public class C
{
public static void M()
{
int x = 1;
string y = ""a"";
}
}";
var comp1 = CreateCompilationWithMscorlib(source1, options: TestOptions.DebugDll);
var comp2 = CreateCompilationWithMscorlib(source2, options: TestOptions.DebugDll);
using (MemoryStream
peStream1Unused = new MemoryStream(),
peStream2 = new MemoryStream(),
pdbStream1 = new MemoryStream(),
pdbStream2 = new MemoryStream())
{
Assert.True(comp1.Emit(peStream1Unused, pdbStream1).Success);
Assert.True(comp2.Emit(peStream2, pdbStream2).Success);
pdbStream1.Position = 0;
pdbStream2.Position = 0;
peStream2.Position = 0;
var symReader = SymReaderFactory.CreateReader(pdbStream1);
symReader.UpdateSymbolStore(pdbStream2);
var module = ModuleInstance.Create(peStream2.ToImmutable(), symReader);
var runtime = CreateRuntimeInstance(module, new[] { MscorlibRef, ExpressionCompilerTestHelpers.IntrinsicAssemblyReference });
ImmutableArray<MetadataBlock> blocks;
Guid moduleVersionId;
ISymUnmanagedReader symReader2;
int methodToken;
int localSignatureToken;
GetContextState(runtime, "C.M", out blocks, out moduleVersionId, out symReader2, out methodToken, out localSignatureToken);
Assert.Same(symReader, symReader2);
AssertEx.SetEqual(symReader.GetLocalNames(methodToken, methodVersion: 1), "x");
AssertEx.SetEqual(symReader.GetLocalNames(methodToken, methodVersion: 2), "x", "y");
var context1 = EvaluationContext.CreateMethodContext(
default(CSharpMetadataContext),
blocks,
symReader,
moduleVersionId,
methodToken: methodToken,
methodVersion: 1,
ilOffset: 0,
localSignatureToken: localSignatureToken);
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
string typeName;
context1.CompileGetLocals(
locals,
argumentsOnly: false,
typeName: out typeName,
testData: null);
AssertEx.SetEqual(locals.Select(l => l.LocalName), "x");
var context2 = EvaluationContext.CreateMethodContext(
default(CSharpMetadataContext),
blocks,
symReader,
moduleVersionId,
methodToken: methodToken,
methodVersion: 2,
ilOffset: 0,
localSignatureToken: localSignatureToken);
locals.Clear();
context2.CompileGetLocals(
locals,
argumentsOnly: false,
typeName: out typeName,
testData: null);
AssertEx.SetEqual(locals.Select(l => l.LocalName), "x", "y");
}
}
示例10: EmitCompilation
internal static bool EmitCompilation(
Compilation c,
IEnumerable<ResourceDescription> manifestResources,
List<ModuleData> dependencies,
DiagnosticBag diagnostics,
bool emitPdb,
CompilationTestData testData,
out ImmutableArray<byte> assembly,
out ImmutableArray<byte> pdb
)
{
assembly = default(ImmutableArray<byte>);
pdb = default(ImmutableArray<byte>);
EmitReferences(c, dependencies, diagnostics, emitPdb);
using (var executableStream = new MemoryStream())
{
EmitResult result;
MemoryStream pdbStream;
string pdbFilePath;
if (emitPdb)
{
pdbStream = new MemoryStream();
pdbFilePath = c.AssemblyName + ".pdb";
}
else
{
pdbStream = null;
pdbFilePath = null;
}
try
{
result = c.Emit(
executableStream,
outputName: null,
pdbFilePath: pdbFilePath,
pdbStream: pdbStream,
xmlDocStream: null,
cancellationToken: default(CancellationToken),
win32Resources: null,
manifestResources: manifestResources,
metadataOnly: false,
testData: testData);
}
finally
{
if (pdbStream != null)
{
pdb = pdbStream.ToImmutable();
pdbStream.Dispose();
}
}
diagnostics.AddRange(result.Diagnostics);
assembly = executableStream.ToImmutable();
return result.Success;
}
}
示例11: VerifyEmitWithNoResources
private static void VerifyEmitWithNoResources(CSharpCompilation comp, Platform platform)
{
var options = TestOptions.ReleaseExe.WithPlatform(platform);
using (var outputStream = new MemoryStream())
{
var success = comp.WithOptions(options).Emit(outputStream).Success;
Assert.True(success);
var peVerifyOutput = CLRHelpers.PeVerify(outputStream.ToImmutable()).Join(Environment.NewLine);
Assert.Equal(string.Empty, peVerifyOutput);
}
}
示例12: AssemblyRefs_DuplicateRows
public void AssemblyRefs_DuplicateRows()
{
var compilation = CreateCompilationWithMscorlib(
"class C : C1 { }; class D { }",
new[] { TestReferences.SymbolsTests.Methods.CSMethods });
PEAssemblyBuilder assembly = new PEAssemblyBuilder(
(SourceAssemblySymbol)compilation.Assembly,
EmitOptions.Default,
compilation.Options.OutputKind,
GetDefaultModulePropertiesForSerialization(),
Enumerable.Empty<ResourceDescription>(),
// map all references to a single name:
assembylSymbol => new AssemblyIdentity("foo")
);
// Don't attempt to emit if there were any syntax, declaration, semantic, or emitted errors previously.
DiagnosticBag diagnostics = new DiagnosticBag();
MethodCompiler.CompileMethodBodies(
compilation: compilation,
moduleBeingBuiltOpt: assembly,
generateDebugInfo: false,
hasDeclarationErrors: false,
diagnostics: diagnostics,
filterOpt: null,
cancellationToken: default(CancellationToken));
diagnostics.Verify();
var context = new EmitContext(assembly, null, new DiagnosticBag());
ImmutableArray<byte> image;
using (var stream = new MemoryStream())
{
Cci.PeWriter.WritePeToStream(
context,
compilation.MessageProvider,
stream,
nativePdbWriterOpt: null,
allowMissingMethodBodies: false,
deterministic: false,
cancellationToken: CancellationToken.None);
image = stream.ToImmutable();
}
context.Diagnostics.Verify();
// check that there are no duplicate rows in AssemblyRef table:
PEAssembly emittedAssembly = AssemblyMetadata.CreateFromImage(image).GetAssembly();
var emittedReferences = emittedAssembly.Modules[0].ReferencedAssemblies;
Assert.Equal(1, emittedReferences.Length);
Assert.Equal("foo", emittedReferences[0].Name);
}
示例13: VerifyEmitWithNoResources
private static void VerifyEmitWithNoResources(CSharpCompilation comp, Platform platform)
{
var options = new CSharpCompilationOptions(
OutputKind.ConsoleApplication,
optimize: true,
platform: platform,
debugInformationKind: DebugInformationKind.None);
using (var outputStream = new MemoryStream())
{
var success = comp.WithOptions(options).Emit(outputStream).Success;
Assert.True(success);
var peVerifyOutput = CLRHelpers.PeVerify(outputStream.ToImmutable()).Join(Environment.NewLine);
Assert.Equal(string.Empty, peVerifyOutput);
}
}
示例14: EmitModuleWithDifferentName
public void EmitModuleWithDifferentName()
{
var name = "a";
var extension = ".netmodule";
var outputName = "b";
var compilation = CreateCompilationWithMscorlib("class A { }", compOptions: TestOptions.NetModule.WithModuleName(name + extension), assemblyName: null);
compilation.VerifyDiagnostics();
var assembly = compilation.Assembly;
Assert.Equal("?", assembly.Name);
var module = assembly.Modules.Single();
Assert.Equal(name + extension, module.Name);
var stream = new MemoryStream();
Assert.True(compilation.Emit(stream, outputName + extension).Success);
using (ModuleMetadata metadata = ModuleMetadata.CreateFromImage(stream.ToImmutable()))
{
var peReader = metadata.Module.GetMetadataReader();
Assert.False(peReader.IsAssembly);
Assert.Equal(outputName + extension, peReader.GetString(peReader.GetModuleDefinition().Name));
}
}
示例15: EmitDifference
internal static CompilationDifference EmitDifference(
this Compilation compilation,
EmitBaseline baseline,
ImmutableArray<SemanticEdit> edits,
IEnumerable<ISymbol> allAddedSymbols = null,
CompilationTestData testData = null)
{
testData = testData ?? new CompilationTestData();
var isAddedSymbol = new Func<ISymbol, bool>(s => allAddedSymbols?.Contains(s) ?? false);
var pdbName = Path.ChangeExtension(compilation.SourceModule.Name, "pdb");
// keep the stream open, it's passed to CompilationDifference
var pdbStream = new MemoryStream();
using (MemoryStream mdStream = new MemoryStream(), ilStream = new MemoryStream())
{
var updatedMethods = new List<MethodDefinitionHandle>();
var result = compilation.EmitDifference(
baseline,
edits,
isAddedSymbol,
mdStream,
ilStream,
pdbStream,
updatedMethods,
testData,
default(CancellationToken));
pdbStream.Seek(0, SeekOrigin.Begin);
return new CompilationDifference(
mdStream.ToImmutable(),
ilStream.ToImmutable(),
pdbStream,
testData,
result,
updatedMethods.ToImmutableArray());
}
}