本文整理汇总了C#中CSharpCompilation.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCompilation.Clone方法的具体用法?C# CSharpCompilation.Clone怎么用?C# CSharpCompilation.Clone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSharpCompilation
的用法示例。
在下文中一共展示了CSharpCompilation.Clone方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCompilationWithExternAliases
private static CSharpCompilation GetCompilationWithExternAliases(CSharpCompilation compilation, ImmutableArray<ExternAliasRecord> externAliasRecords)
{
if (externAliasRecords.IsDefaultOrEmpty)
{
return compilation.Clone();
}
var updatedReferences = ArrayBuilder<MetadataReference>.GetInstance();
var assembliesAndModulesBuilder = ArrayBuilder<Symbol>.GetInstance();
foreach (var reference in compilation.References)
{
updatedReferences.Add(reference);
assembliesAndModulesBuilder.Add(compilation.GetAssemblyOrModuleSymbol(reference));
}
Debug.Assert(assembliesAndModulesBuilder.Count == updatedReferences.Count);
var assembliesAndModules = assembliesAndModulesBuilder.ToImmutableAndFree();
foreach (var externAliasRecord in externAliasRecords)
{
var targetAssembly = externAliasRecord.TargetAssembly as AssemblySymbol;
int index;
if (targetAssembly != null)
{
index = assembliesAndModules.IndexOf(targetAssembly);
}
else
{
index = IndexOfMatchingAssembly((AssemblyIdentity)externAliasRecord.TargetAssembly, assembliesAndModules, compilation.Options.AssemblyIdentityComparer);
}
if (index < 0)
{
Debug.WriteLine($"Unable to find corresponding assembly reference for extern alias '{externAliasRecord}'");
continue;
}
var externAlias = externAliasRecord.Alias;
var assemblyReference = updatedReferences[index];
var oldAliases = assemblyReference.Properties.Aliases;
var newAliases = oldAliases.IsEmpty
? ImmutableArray.Create(MetadataReferenceProperties.GlobalAlias, externAlias)
: oldAliases.Concat(ImmutableArray.Create(externAlias));
// NOTE: Dev12 didn't emit custom debug info about "global", so we don't have
// a good way to distinguish between a module aliased with both (e.g.) "X" and
// "global" and a module aliased with only "X". As in Dev12, we assume that
// "global" is a valid alias to remain at least as permissive as source.
// NOTE: In the event that this introduces ambiguities between two assemblies
// (e.g. because one was "global" in source and the other was "X"), it should be
// possible to disambiguate as long as each assembly has a distinct extern alias,
// not necessarily used in source.
Debug.Assert(newAliases.Contains(MetadataReferenceProperties.GlobalAlias));
// Replace the value in the map with the updated reference.
updatedReferences[index] = assemblyReference.WithAliases(newAliases);
}
compilation = compilation.WithReferences(updatedReferences);
updatedReferences.Free();
return compilation;
}
示例2: GetCompilationWithExternAliases
private static CSharpCompilation GetCompilationWithExternAliases(CSharpCompilation compilation, ImmutableArray<string> externAliasStrings)
{
if (externAliasStrings.IsDefaultOrEmpty)
{
return compilation.Clone();
}
var updatedReferences = ArrayBuilder<MetadataReference>.GetInstance();
var assemblyIdentities = ArrayBuilder<AssemblyIdentity>.GetInstance();
foreach (var reference in compilation.References)
{
var identity = reference.Properties.Kind == MetadataImageKind.Assembly
? ((AssemblySymbol)compilation.GetAssemblyOrModuleSymbol(reference)).Identity
: null;
assemblyIdentities.Add(identity);
updatedReferences.Add(reference);
}
Debug.Assert(assemblyIdentities.Count == updatedReferences.Count);
var comparer = compilation.Options.AssemblyIdentityComparer;
var numAssemblies = assemblyIdentities.Count;
foreach (var externAliasString in externAliasStrings)
{
string alias;
string externAlias;
string target;
ImportTargetKind kind;
if (!CustomDebugInfoReader.TryParseCSharpImportString(externAliasString, out alias, out externAlias, out target, out kind))
{
Debug.WriteLine("Unable to parse extern alias '{0}'", (object)externAliasString);
continue;
}
Debug.Assert(kind == ImportTargetKind.Assembly, "Programmer error: How did a non-assembly get in the extern alias list?");
Debug.Assert(alias == null); // Not used.
Debug.Assert(externAlias != null); // Name of the extern alias.
Debug.Assert(target != null); // Name of the target assembly.
AssemblyIdentity targetIdentity;
if (!AssemblyIdentity.TryParseDisplayName(target, out targetIdentity))
{
Debug.WriteLine("Unable to parse target of extern alias '{0}'", (object)externAliasString);
continue;
}
int index = -1;
for (int i = 0; i < numAssemblies; i++)
{
var candidateIdentity = assemblyIdentities[i];
if (candidateIdentity != null && comparer.ReferenceMatchesDefinition(targetIdentity, candidateIdentity))
{
index = i;
break;
}
}
if (index < 0)
{
Debug.WriteLine("Unable to find corresponding assembly reference for extern alias '{0}'", (object)externAliasString);
continue;
}
var assemblyReference = updatedReferences[index];
var oldAliases = assemblyReference.Properties.Aliases;
var newAliases = oldAliases.IsEmpty
? ImmutableArray.Create(MetadataReferenceProperties.GlobalAlias, externAlias)
: oldAliases.Concat(ImmutableArray.Create(externAlias));
// NOTE: Dev12 didn't emit custom debug info about "global", so we don't have
// a good way to distinguish between a module aliased with both (e.g.) "X" and
// "global" and a module aliased with only "X". As in Dev12, we assume that
// "global" is a valid alias to remain at least as permissive as source.
// NOTE: In the event that this introduces ambiguities between two assemblies
// (e.g. because one was "global" in source and the other was "X"), it should be
// possible to disambiguate as long as each assembly has a distinct extern alias,
// not necessarily used in source.
Debug.Assert(newAliases.Contains(MetadataReferenceProperties.GlobalAlias));
// Replace the value in the map with the updated reference.
updatedReferences[index] = assemblyReference.WithAliases(newAliases);
}
compilation = compilation.WithReferences(updatedReferences);
assemblyIdentities.Free();
updatedReferences.Free();
return compilation;
}