本文整理汇总了C#中MetadataReference类的典型用法代码示例。如果您正苦于以下问题:C# MetadataReference类的具体用法?C# MetadataReference怎么用?C# MetadataReference使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MetadataReference类属于命名空间,在下文中一共展示了MetadataReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompileAndVerifyIL
private CompilationVerifier CompileAndVerifyIL(string source, string methodName, string expectedOptimizedIL = null, string expectedUnoptimizedIL = null,
MetadataReference[] references = null)
{
references = references ?? new[] { SystemCoreRef, CSharpRef };
// verify that we emit correct optimized and unoptimized IL:
var unoptimizedCompilation = CreateCompilationWithMscorlib45(source, references, compOptions: TestOptions.UnsafeDll.WithMetadataImportOptions(MetadataImportOptions.All).WithOptimizations(false));
var optimizedCompilation = CreateCompilationWithMscorlib45(source, references, compOptions: TestOptions.UnsafeDll.WithMetadataImportOptions(MetadataImportOptions.All).WithOptimizations(true));
var unoptimizedVerifier = CompileAndVerify(unoptimizedCompilation);
var optimizedVerifier = CompileAndVerify(optimizedCompilation);
// check what IL we emit exactly:
if (expectedUnoptimizedIL != null)
{
unoptimizedVerifier.VerifyIL(methodName, expectedUnoptimizedIL, realIL: true);
}
if (expectedOptimizedIL != null)
{
optimizedVerifier.VerifyIL(methodName, expectedOptimizedIL, realIL: true);
}
// return null if ambiguous
return (expectedUnoptimizedIL != null) ^ (expectedOptimizedIL != null) ? (unoptimizedVerifier ?? optimizedVerifier) : null;
}
示例2: GetSymbolsForReferences
internal static AssemblySymbol[] GetSymbolsForReferences(
CSharpCompilation[] compilations = null,
byte[][] bytes = null,
MetadataReference[] mrefs = null,
CSharpCompilationOptions options = null)
{
var refs = new List<MetadataReference>();
if (compilations != null)
{
foreach (var c in compilations)
{
refs.Add(new CSharpCompilationReference(c));
}
}
if (bytes != null)
{
foreach (var b in bytes)
{
refs.Add(new MetadataImageReference(b.AsImmutableOrNull()));
}
}
if (mrefs != null)
{
refs.AddRange(mrefs);
}
var tc1 = CSharpCompilation.Create(assemblyName: "Dummy", options: options ?? TestOptions.ReleaseDll, syntaxTrees: new SyntaxTree[0], references: refs);
return (from @ref in refs select tc1.GetReferencedAssemblySymbol(@ref)).ToArray();
}
示例3: TestOverloadResolutionWithDiff
internal void TestOverloadResolutionWithDiff(string source, MetadataReference[] additionalRefs = null)
{
// The mechanism of this test is: we build the bound tree for the code passed in and then extract
// from it the nodes that describe the method symbols. We then compare the description of
// the symbols given to the comment that follows the call.
var mscorlibRef = new MetadataImageReference(ProprietaryTestResources.NetFX.v4_0_30316_17626.mscorlib.AsImmutableOrNull(), display: "mscorlib");
var references = new[] { mscorlibRef }.Concat(additionalRefs ?? SpecializedCollections.EmptyArray<MetadataReference>());
var compilation = CreateCompilation(source, references, TestOptions.ReleaseDll);
var method = (SourceMethodSymbol)compilation.GlobalNamespace.GetTypeMembers("C").Single().GetMembers("M").Single();
var diagnostics = new DiagnosticBag();
var block = MethodCompiler.BindMethodBody(method, new TypeCompilationState(method.ContainingType, compilation, null), diagnostics);
var tree = BoundTreeDumperNodeProducer.MakeTree(block);
var results = string.Join("\n", tree.PreorderTraversal().Select(edge => edge.Value)
.Where(x => x.Text == "method" && x.Value != null)
.Select(x => x.Value)
.ToArray());
// var r = string.Join("\n", tree.PreorderTraversal().Select(edge => edge.Value).ToArray();
var expected = string.Join("\n", source
.Split(new[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.Contains("//-"))
.Select(x => x.Substring(x.IndexOf("//-") + 3))
.ToArray());
AssertEx.Equal(expected, results);
}
示例4: CompileAndVerifyIL
private CompilationVerifier CompileAndVerifyIL(
string source,
string methodName,
string expectedOptimizedIL = null,
string expectedUnoptimizedIL = null,
MetadataReference[] references = null,
bool allowUnsafe = false,
[CallerFilePath]string callerPath = null,
[CallerLineNumber]int callerLine = 0)
{
references = references ?? new[] { SystemCoreRef, CSharpRef };
// verify that we emit correct optimized and unoptimized IL:
var unoptimizedCompilation = CreateCompilationWithMscorlib45(source, references, options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All).WithAllowUnsafe(allowUnsafe));
var optimizedCompilation = CreateCompilationWithMscorlib45(source, references, options: TestOptions.ReleaseDll.WithMetadataImportOptions(MetadataImportOptions.All).WithAllowUnsafe(allowUnsafe));
var unoptimizedVerifier = CompileAndVerify(unoptimizedCompilation);
var optimizedVerifier = CompileAndVerify(optimizedCompilation);
// check what IL we emit exactly:
if (expectedUnoptimizedIL != null)
{
unoptimizedVerifier.VerifyIL(methodName, expectedUnoptimizedIL, realIL: true, sequencePoints: methodName, callerPath: callerPath, callerLine: callerLine);
}
if (expectedOptimizedIL != null)
{
optimizedVerifier.VerifyIL(methodName, expectedOptimizedIL, realIL: true, callerPath: callerPath, callerLine: callerLine);
}
// return null if ambiguous
return (expectedUnoptimizedIL != null) ^ (expectedOptimizedIL != null) ? (unoptimizedVerifier ?? optimizedVerifier) : null;
}
示例5: CreateCompilation
internal static Compilation CreateCompilation(string source, MetadataReference[] references, string assemblyName, CSharpCompilationOptions options = null)
{
return CSharpCompilation.Create(
assemblyName,
new[] { SyntaxFactory.ParseSyntaxTree(source) },
references,
options ?? new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}
示例6: CreateChecksum
public Checksum CreateChecksum(MetadataReference reference, CancellationToken cancellationToken)
{
var portable = reference as PortableExecutableReference;
if (portable != null)
{
return CreatePortableExecutableReferenceChecksum(portable, cancellationToken);
}
throw ExceptionUtilities.UnexpectedValue(reference.GetType());
}
示例7: GetMetadataReferenceFilePath
private static string GetMetadataReferenceFilePath(MetadataReference metadataReference)
{
var executabeReference = metadataReference as PortableExecutableReference;
if (executabeReference == null)
{
return null;
}
return executabeReference.FilePath;
}
示例8: AddProjectWithMetadataReferences
public static Solution AddProjectWithMetadataReferences(Solution solution, string projectName, string languageName, string code, MetadataReference metadataReference, params ProjectId[] projectReferences)
{
var suffix = languageName == LanguageNames.CSharp ? "cs" : "vb";
var pid = ProjectId.CreateNewId();
var did = DocumentId.CreateNewId(pid);
var pi = ProjectInfo.Create(
pid,
VersionStamp.Default,
projectName,
projectName,
languageName,
metadataReferences: new[] { metadataReference },
projectReferences: projectReferences.Select(p => new ProjectReference(p)));
return solution.AddProject(pi).AddDocument(did, $"{projectName}.{suffix}", SourceText.From(code));
}
示例9: ModuleInstance
internal ModuleInstance(
MetadataReference metadataReference,
ModuleMetadata moduleMetadata,
Guid moduleVersionId,
byte[] fullImage,
byte[] metadataOnly,
object symReader,
bool includeLocalSignatures)
{
Debug.Assert((fullImage == null) || (fullImage.Length > metadataOnly.Length));
this.MetadataReference = metadataReference;
this.ModuleMetadata = moduleMetadata;
this.ModuleVersionId = moduleVersionId;
this.FullImage = fullImage;
this.MetadataOnly = metadataOnly;
this.MetadataHandle = GCHandle.Alloc(metadataOnly, GCHandleType.Pinned);
this.SymReader = symReader; // should be non-null if and only if there are symbols
_includeLocalSignatures = includeLocalSignatures;
}
示例10: WriteTo
public void WriteTo(MetadataReference reference, ObjectWriter writer, CancellationToken cancellationToken)
{
var portable = reference as PortableExecutableReference;
if (portable != null)
{
var supportTemporaryStorage = portable as ISupportTemporaryStorage;
if (supportTemporaryStorage != null)
{
if (TryWritePortableExecutableReferenceBackedByTemporaryStorageTo(supportTemporaryStorage, writer, cancellationToken))
{
return;
}
}
WritePortableExecutableReferenceTo(portable, writer, cancellationToken);
return;
}
throw ExceptionUtilities.UnexpectedValue(reference.GetType());
}
示例11: AppConfig1
public void AppConfig1()
{
var references = new MetadataReference[]
{
TestReferences.NetFx.v4_0_30319.mscorlib,
TestReferences.NetFx.v4_0_30319.System,
TestReferences.NetFx.silverlight_v5_0_5_0.System
};
var compilation = CreateCompilation(
new[] { Parse("") },
references,
options: TestOptions.ReleaseDll.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default));
compilation.VerifyDiagnostics(
// error CS1703: Multiple assemblies with equivalent identity have been imported: 'System.dll' and 'System.v5.0.5.0_silverlight.dll'. Remove one of the duplicate references.
Diagnostic(ErrorCode.ERR_DuplicateImport).WithArguments("System.dll", "System.v5.0.5.0_silverlight.dll"));
var appConfig = new MemoryStream(Encoding.UTF8.GetBytes(
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<runtime>
<assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
<supportPortability PKT=""7cec85d7bea7798e"" enable=""false""/>
</assemblyBinding>
</runtime>
</configuration>"));
var comparer = DesktopAssemblyIdentityComparer.LoadFromXml(appConfig);
compilation = CreateCompilation(
new[] { Parse("") },
references,
options: TestOptions.ReleaseDll.WithAssemblyIdentityComparer(comparer));
compilation.VerifyDiagnostics();
}
示例12: TestMixedEventAccessorModifiers_EmptyConcrete
public void TestMixedEventAccessorModifiers_EmptyConcrete()
{
var text1 = @"
class Derived : AccessorModifierMismatch
{
// Not overriding anything.
}
";
var refs = new MetadataReference[] { TestReferences.SymbolsTests.Events };
CreateCompilationWithMscorlib(text1, references: refs, compOptions: TestOptions.Dll).VerifyDiagnostics(
// (2,7): error CS0534: 'Derived' does not implement inherited abstract member 'AccessorModifierMismatch.NoneAbstract.remove'
// class Derived : AccessorModifierMismatch
Diagnostic(ErrorCode.ERR_UnimplementedAbstractMethod, "Derived").WithArguments("Derived", "AccessorModifierMismatch.NoneAbstract.remove"),
// (2,7): error CS0534: 'Derived' does not implement inherited abstract member 'AccessorModifierMismatch.AbstractNone.add'
// class Derived : AccessorModifierMismatch
Diagnostic(ErrorCode.ERR_UnimplementedAbstractMethod, "Derived").WithArguments("Derived", "AccessorModifierMismatch.AbstractNone.add"));
}
示例13: TestMixedPropertyAccessorModifiers_OverrideSetters
public void TestMixedPropertyAccessorModifiers_OverrideSetters()
{
var text1 = @"
class Derived : AccessorModifierMismatch // CS0534 (didn't implement AbstractAbstract.get)
{
public override int NoneNone { set { } } // CS0506 (not virtual)
public override int NoneAbstract { set { } }
public override int NoneVirtual { set { } }
public override int NoneOverride { set { } } // CS1545 (bogus)
public override int NoneSealed { set { } } // CS1545 (bogus)
public override int AbstractNone { set { } } // CS0506 (not virtual)
public override int AbstractAbstract { set { } }
public override int AbstractVirtual { set { } }
public override int AbstractOverride { set { } } // CS1545 (bogus)
public override int AbstractSealed { set { } } // CS1545 (bogus)
public override int VirtualNone { set { } }
public override int VirtualAbstract { set { } }
public override int VirtualVirtual { set { } }
public override int VirtualOverride { set { } } // CS1545 (bogus)
public override int VirtualSealed { set { } } // CS1545 (bogus)
public override int OverrideNone { set { } } // CS1545 (bogus)
public override int OverrideAbstract { set { } } // CS1545 (bogus)
public override int OverrideVirtual { set { } } // CS1545 (bogus)
public override int OverrideOverride { set { } }
public override int OverrideSealed { set { } } // CS1545 (bogus)
public override int SealedNone { set { } } // CS1545 (bogus)
public override int SealedAbstract { set { } } // CS1545 (bogus)
public override int SealedVirtual { set { } } // CS1545 (bogus)
public override int SealedOverride { set { } } // CS1545 (bogus)
public override int SealedSealed { set { } } // CS0239 (sealed)
}
";
var refs = new MetadataReference[] { TestReferences.SymbolsTests.Properties };
CreateCompilationWithMscorlib(text1, references: refs, compOptions: TestOptions.Dll).VerifyDiagnostics(
// (4,25): error CS0506: 'Derived.NoneNone': cannot override inherited member 'AccessorModifierMismatch.NoneNone' because it is not marked virtual, abstract, or override
// public override int NoneNone { set { } } // CS0506 (not virtual)
Diagnostic(ErrorCode.ERR_CantOverrideNonVirtual, "NoneNone").WithArguments("Derived.NoneNone", "AccessorModifierMismatch.NoneNone"),
// (7,25): error CS0569: 'Derived.NoneOverride': cannot override 'AccessorModifierMismatch.NoneOverride' because it is not supported by the language
// public override int NoneOverride { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "NoneOverride").WithArguments("Derived.NoneOverride", "AccessorModifierMismatch.NoneOverride"),
// (8,25): error CS0569: 'Derived.NoneSealed': cannot override 'AccessorModifierMismatch.NoneSealed' because it is not supported by the language
// public override int NoneSealed { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "NoneSealed").WithArguments("Derived.NoneSealed", "AccessorModifierMismatch.NoneSealed"),
// (10,40): error CS0506: 'Derived.AbstractNone.set': cannot override inherited member 'AccessorModifierMismatch.NoneNone.set' because it is not marked virtual, abstract, or override
// public override int AbstractNone { set { } } // CS0506 (not virtual)
Diagnostic(ErrorCode.ERR_CantOverrideNonVirtual, "set").WithArguments("Derived.AbstractNone.set", "AccessorModifierMismatch.NoneNone.set"),
// (13,25): error CS0569: 'Derived.AbstractOverride': cannot override 'AccessorModifierMismatch.AbstractOverride' because it is not supported by the language
// public override int AbstractOverride { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "AbstractOverride").WithArguments("Derived.AbstractOverride", "AccessorModifierMismatch.AbstractOverride"),
// (14,25): error CS0569: 'Derived.AbstractSealed': cannot override 'AccessorModifierMismatch.AbstractSealed' because it is not supported by the language
// public override int AbstractSealed { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "AbstractSealed").WithArguments("Derived.AbstractSealed", "AccessorModifierMismatch.AbstractSealed"),
// (16,39): error CS0506: 'Derived.VirtualNone.set': cannot override inherited member 'AccessorModifierMismatch.NoneNone.set' because it is not marked virtual, abstract, or override
// public override int VirtualNone { set { } }
Diagnostic(ErrorCode.ERR_CantOverrideNonVirtual, "set").WithArguments("Derived.VirtualNone.set", "AccessorModifierMismatch.NoneNone.set"),
// (19,25): error CS0569: 'Derived.VirtualOverride': cannot override 'AccessorModifierMismatch.VirtualOverride' because it is not supported by the language
// public override int VirtualOverride { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "VirtualOverride").WithArguments("Derived.VirtualOverride", "AccessorModifierMismatch.VirtualOverride"),
// (20,25): error CS0569: 'Derived.VirtualSealed': cannot override 'AccessorModifierMismatch.VirtualSealed' because it is not supported by the language
// public override int VirtualSealed { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "VirtualSealed").WithArguments("Derived.VirtualSealed", "AccessorModifierMismatch.VirtualSealed"),
// (22,25): error CS0569: 'Derived.OverrideNone': cannot override 'AccessorModifierMismatch.OverrideNone' because it is not supported by the language
// public override int OverrideNone { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "OverrideNone").WithArguments("Derived.OverrideNone", "AccessorModifierMismatch.OverrideNone"),
// (23,25): error CS0569: 'Derived.OverrideAbstract': cannot override 'AccessorModifierMismatch.OverrideAbstract' because it is not supported by the language
// public override int OverrideAbstract { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "OverrideAbstract").WithArguments("Derived.OverrideAbstract", "AccessorModifierMismatch.OverrideAbstract"),
// (24,25): error CS0569: 'Derived.OverrideVirtual': cannot override 'AccessorModifierMismatch.OverrideVirtual' because it is not supported by the language
// public override int OverrideVirtual { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "OverrideVirtual").WithArguments("Derived.OverrideVirtual", "AccessorModifierMismatch.OverrideVirtual"),
// (26,25): error CS0569: 'Derived.OverrideSealed': cannot override 'AccessorModifierMismatch.OverrideSealed' because it is not supported by the language
// public override int OverrideSealed { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "OverrideSealed").WithArguments("Derived.OverrideSealed", "AccessorModifierMismatch.OverrideSealed"),
// (28,25): error CS0569: 'Derived.SealedNone': cannot override 'AccessorModifierMismatch.SealedNone' because it is not supported by the language
// public override int SealedNone { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "SealedNone").WithArguments("Derived.SealedNone", "AccessorModifierMismatch.SealedNone"),
// (29,25): error CS0569: 'Derived.SealedAbstract': cannot override 'AccessorModifierMismatch.SealedAbstract' because it is not supported by the language
// public override int SealedAbstract { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "SealedAbstract").WithArguments("Derived.SealedAbstract", "AccessorModifierMismatch.SealedAbstract"),
// (30,25): error CS0569: 'Derived.SealedVirtual': cannot override 'AccessorModifierMismatch.SealedVirtual' because it is not supported by the language
// public override int SealedVirtual { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "SealedVirtual").WithArguments("Derived.SealedVirtual", "AccessorModifierMismatch.SealedVirtual"),
// (31,25): error CS0569: 'Derived.SealedOverride': cannot override 'AccessorModifierMismatch.SealedOverride' because it is not supported by the language
// public override int SealedOverride { set { } } // CS1545 (bogus)
Diagnostic(ErrorCode.ERR_CantOverrideBogusMethod, "SealedOverride").WithArguments("Derived.SealedOverride", "AccessorModifierMismatch.SealedOverride"),
// (32,25): error CS0239: 'Derived.SealedSealed': cannot override inherited member 'AccessorModifierMismatch.SealedSealed' because it is sealed
// public override int SealedSealed { set { } } // CS0239 (sealed)
Diagnostic(ErrorCode.ERR_CantOverrideSealed, "SealedSealed").WithArguments("Derived.SealedSealed", "AccessorModifierMismatch.SealedSealed"),
// (2,7): error CS0534: 'Derived' does not implement inherited abstract member 'AccessorModifierMismatch.AbstractNone.get'
// class Derived : AccessorModifierMismatch // CS0534 (didn't implement AbstractAbstract.get)
Diagnostic(ErrorCode.ERR_UnimplementedAbstractMethod, "Derived").WithArguments("Derived", "AccessorModifierMismatch.AbstractNone.get"));
}
示例14: TestMixedPropertyAccessorModifiers_EmptyAbstract
public void TestMixedPropertyAccessorModifiers_EmptyAbstract()
{
var text1 = @"
abstract class Derived : AccessorModifierMismatch
{
// Not overriding anything.
}
";
var refs = new MetadataReference[] { TestReferences.SymbolsTests.Properties };
CreateCompilationWithMscorlib(text1, references: refs, compOptions: TestOptions.Dll).VerifyDiagnostics();
}
示例15: SerializeMetadataReference
public void SerializeMetadataReference(MetadataReference reference, ObjectWriter writer, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
_hostSerializationService.WriteTo(reference, writer, cancellationToken);
}