本文整理汇总了C#中CSharpCompilationOptions.WithDelaySign方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCompilationOptions.WithDelaySign方法的具体用法?C# CSharpCompilationOptions.WithDelaySign怎么用?C# CSharpCompilationOptions.WithDelaySign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSharpCompilationOptions
的用法示例。
在下文中一共展示了CSharpCompilationOptions.WithDelaySign方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddSigningOptions
private static CSharpCompilationOptions AddSigningOptions(CSharpCompilationOptions options, CommonCompilerOptions compilerOptions, string projectDirectory)
{
var useOssSigning = compilerOptions.UseOssSigning == true;
var keyFile = compilerOptions.KeyFile;
if (!string.IsNullOrEmpty(keyFile))
{
keyFile = Path.GetFullPath(Path.Combine(projectDirectory, compilerOptions.KeyFile));
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || useOssSigning)
{
return options.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile)));
}
options = options.WithCryptoKeyFile(keyFile);
return options.WithDelaySign(compilerOptions.DelaySign);
}
return options;
}
示例2: Create
public HostBuildData Create(HostBuildOptions options)
{
var parseOptions = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp6, documentationMode: DocumentationMode.Parse);
var compilationOptions = new CSharpCompilationOptions(
OutputKind.ConsoleApplication,
xmlReferenceResolver: new XmlFileResolver(options.ProjectDirectory),
sourceReferenceResolver: new SourceFileResolver(ImmutableArray<string>.Empty, options.ProjectDirectory),
metadataReferenceResolver: new AssemblyReferenceResolver(
new MetadataFileReferenceResolver(ImmutableArray<string>.Empty, options.ProjectDirectory),
MetadataFileReferenceProvider.Default),
strongNameProvider: new DesktopStrongNameProvider(ImmutableArray.Create(options.ProjectDirectory, options.OutputDirectory)),
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default);
var warnings = new List<KeyValuePair<string, ReportDiagnostic>>(options.Warnings);
if (options.OutputKind.HasValue)
{
var kind = options.OutputKind.Value;
compilationOptions = compilationOptions.WithOutputKind(kind);
if (compilationOptions.Platform == Platform.AnyCpu32BitPreferred &&
(kind == OutputKind.DynamicallyLinkedLibrary || kind == OutputKind.NetModule || kind == OutputKind.WindowsRuntimeMetadata))
{
compilationOptions = compilationOptions.WithPlatform(Platform.AnyCpu);
}
}
if (!string.IsNullOrEmpty(options.DefineConstants))
{
IEnumerable<Diagnostic> diagnostics;
parseOptions = parseOptions.WithPreprocessorSymbols(CSharpCommandLineParser.ParseConditionalCompilationSymbols(options.DefineConstants, out diagnostics));
}
if (options.DocumentationFile != null)
{
parseOptions = parseOptions.WithDocumentationMode(!string.IsNullOrEmpty(options.DocumentationFile) ? DocumentationMode.Diagnose : DocumentationMode.Parse);
}
if (options.LanguageVersion != null)
{
var languageVersion = CompilationOptionsConversion.GetLanguageVersion(options.LanguageVersion);
if (languageVersion.HasValue)
{
parseOptions = parseOptions.WithLanguageVersion(languageVersion.Value);
}
}
if (!string.IsNullOrEmpty(options.PlatformWith32BitPreference))
{
Platform platform;
if (Enum.TryParse<Platform>(options.PlatformWith32BitPreference, true, out platform))
{
if (platform == Platform.AnyCpu &&
compilationOptions.OutputKind != OutputKind.DynamicallyLinkedLibrary &&
compilationOptions.OutputKind != OutputKind.NetModule &&
compilationOptions.OutputKind != OutputKind.WindowsRuntimeMetadata)
{
platform = Platform.AnyCpu32BitPreferred;
}
compilationOptions = compilationOptions.WithPlatform(platform);
}
}
if (options.AllowUnsafeBlocks.HasValue)
{
compilationOptions = compilationOptions.WithAllowUnsafe(options.AllowUnsafeBlocks.Value);
}
if (options.CheckForOverflowUnderflow.HasValue)
{
compilationOptions = compilationOptions.WithOverflowChecks(options.CheckForOverflowUnderflow.Value);
}
if (options.DelaySign != null)
{
bool delaySignExplicitlySet = options.DelaySign.Item1;
bool delaySign = options.DelaySign.Item2;
compilationOptions = compilationOptions.WithDelaySign(delaySignExplicitlySet ? delaySign : (bool?)null);
}
if (!string.IsNullOrEmpty(options.ApplicationConfiguration))
{
var appConfigPath = FileUtilities.ResolveRelativePath(options.ApplicationConfiguration, options.ProjectDirectory);
try
{
using (var appConfigStream = PortableShim.FileStream.Create(appConfigPath, PortableShim.FileMode.Open, PortableShim.FileAccess.Read))
{
compilationOptions = compilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.LoadFromXml(appConfigStream));
}
}
catch (Exception)
{
}
}
if (!string.IsNullOrEmpty(options.KeyContainer))
{
compilationOptions = compilationOptions.WithCryptoKeyContainer(options.KeyContainer);
}
if (!string.IsNullOrEmpty(options.KeyFile))
//.........这里部分代码省略.........
示例3: GenerateSerializationAssembly
public static AssemblyDefinition GenerateSerializationAssembly(PlatformType platformType, BaseAssemblyResolver assemblyResolver, AssemblyDefinition assembly, string serializationAssemblyLocation, string signKeyFile, List<string> serializatonProjectReferencePaths)
{
// Make sure all assemblies in serializatonProjectReferencePaths are referenced (sometimes they might be optimized out if no direct references)
foreach (var serializatonProjectReferencePath in serializatonProjectReferencePaths)
{
var shortAssemblyName = Path.GetFileNameWithoutExtension(serializatonProjectReferencePath);
// Still in references (not optimized)
if (assembly.MainModule.AssemblyReferences.Any(x => x.Name == shortAssemblyName))
continue;
// For now, use AssemblyDefinition.ReadAssembly to compute full name -- maybe not very efficient but it shouldn't happen often anyway)
var referencedAssembly = AssemblyDefinition.ReadAssembly(serializatonProjectReferencePath);
assembly.MainModule.AssemblyReferences.Add(AssemblyNameReference.Parse(referencedAssembly.FullName));
}
// Create the serializer code generator
var serializerGenerator = new ComplexSerializerCodeGenerator(assemblyResolver, assembly);
// Register default serialization profile (to help AOT generic instantiation of serializers)
RegisterDefaultSerializationProfile(assemblyResolver, assembly, serializerGenerator);
// Generate serializer code
var serializerGeneratedCode = serializerGenerator.TransformText();
var syntaxTree = CSharpSyntaxTree.ParseText(serializerGeneratedCode);
// Add reference from source assembly
// Use a hash set because it seems including twice mscorlib (2.0 and 4.0) seems to be a problem.
var skipWindows = "Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null";
var compilerOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default);
// Sign the serialization assembly the same way the source was signed
// TODO: Transmit over command line
if (assembly.Name.HasPublicKey)
{
// TODO: If delay signed, we could actually extract the public key and apply it ourself maybe?
if (signKeyFile == null)
throw new InvalidOperationException("Generating serialization code for signed assembly, but no key was specified.");
compilerOptions = compilerOptions.WithCryptoKeyFile(signKeyFile).WithStrongNameProvider(new DesktopStrongNameProvider());
if ((assembly.MainModule.Attributes & ModuleAttributes.StrongNameSigned) != ModuleAttributes.StrongNameSigned)
{
// Delay signed
compilerOptions = compilerOptions.WithDelaySign(true);
}
}
var metadataReferences = new List<MetadataReference>();
var assemblyLocations = new HashSet<string>();
foreach (var referencedAssemblyName in assembly.MainModule.AssemblyReferences)
{
// We skip both Windows, and current assembly (AssemblyProcessor.Common, which might be added with an alias)
if (referencedAssemblyName.FullName != skipWindows && referencedAssemblyName.FullName != typeof(ComplexSerializerGenerator).Assembly.FullName && referencedAssemblyName.FullName != "SiliconStudio.AssemblyProcessor")
{
if (assemblyLocations.Add(referencedAssemblyName.Name))
{
//Console.WriteLine("Resolve Assembly for serialization [{0}]", referencedAssemblyName.FullName);
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assemblyResolver.Resolve(referencedAssemblyName)));
}
}
}
// typeof(Dictionary<,>)
// Special case for 4.5: Because Dictionary<,> is forwarded, we need to add a reference to the actual assembly
var mscorlibAssembly = CecilExtensions.FindCorlibAssembly(assembly);
metadataReferences.Add(CreateMetadataReference(assemblyResolver, mscorlibAssembly));
var collectionType = mscorlibAssembly.MainModule.GetTypeResolved(typeof(Dictionary<,>).FullName);
metadataReferences.Add(CreateMetadataReference(assemblyResolver, collectionType.Module.Assembly));
// Make sure System and System.Reflection are added
// TODO: Maybe we should do that for .NETCore and PCL too? (instead of WinRT only)
if (platformType == PlatformType.WindowsStore || platformType == PlatformType.WindowsPhone)
{
if (assemblyLocations.Add("System"))
{
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assemblyResolver.Resolve("System")));
}
if (assemblyLocations.Add("System.Reflection"))
{
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assemblyResolver.Resolve("System.Reflection")));
}
}
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assembly));
assemblyLocations.Add(assembly.Name.Name);
// In case Paradox.Framework.Serialization was not referenced, let's add it.
if (!assemblyLocations.Contains("SiliconStudio.Core"))
{
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assemblyResolver.Resolve("SiliconStudio.Core")));
assemblyLocations.Add("SiliconStudio.Core");
}
// Create roslyn compilation object
var assemblyName = Path.GetFileNameWithoutExtension(serializationAssemblyLocation);
var compilation = CSharpCompilation.Create(assemblyName, new[] { syntaxTree }, metadataReferences, compilerOptions);
//.........这里部分代码省略.........
示例4: AddSigningOptions
private static CSharpCompilationOptions AddSigningOptions(CSharpCompilationOptions options, CommonCompilerOptions compilerOptions, string projectDirectory)
{
var useOssSigning = compilerOptions.PublicSign == true;
var keyFile = compilerOptions.KeyFile;
if (!string.IsNullOrEmpty(keyFile))
{
keyFile = Path.GetFullPath(Path.Combine(projectDirectory, compilerOptions.KeyFile));
if (RuntimeEnvironment.OperatingSystemPlatform != InternalAbstractions.Platform.Windows || useOssSigning)
{
return options.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile)));
}
options = options.WithCryptoKeyFile(keyFile);
return options.WithDelaySign(compilerOptions.DelaySign);
}
return options;
}
示例5: GenerateRoslynAssembly
public static AssemblyDefinition GenerateRoslynAssembly(BaseAssemblyResolver assemblyResolver, AssemblyDefinition assembly, string serializationAssemblyLocation, string signKeyFile, List<string> references, List<AssemblyDefinition> memoryReferences, ILogger log, IEnumerable<string> sourceCodes)
{
var syntaxTrees = sourceCodes.Select(x => CSharpSyntaxTree.ParseText(x));
var compilerOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default);
// Sign the serialization assembly the same way the source was signed
// TODO: Transmit over command line
if (assembly.Name.HasPublicKey)
{
// TODO: If delay signed, we could actually extract the public key and apply it ourself maybe?
if (signKeyFile == null)
throw new InvalidOperationException("Generating serialization code for signed assembly, but no key was specified.");
compilerOptions = compilerOptions.WithCryptoKeyFile(signKeyFile).WithStrongNameProvider(new DesktopStrongNameProvider());
if ((assembly.MainModule.Attributes & ModuleAttributes.StrongNameSigned) != ModuleAttributes.StrongNameSigned)
{
// Delay signed
compilerOptions = compilerOptions.WithDelaySign(true);
}
}
// Add references (files and in-memory PE data)
var metadataReferences = new List<MetadataReference>();
foreach (var reference in references)
{
metadataReferences.Add(MetadataReference.CreateFromFile(reference));
}
foreach (var reference in memoryReferences)
{
metadataReferences.Add(CreateMetadataReference(assemblyResolver, reference));
}
// typeof(Dictionary<,>)
// Special case for 4.5: Because Dictionary<,> is forwarded, we need to add a reference to the actual assembly
var mscorlibAssembly = CecilExtensions.FindCorlibAssembly(assembly);
metadataReferences.Add(CreateMetadataReference(assemblyResolver, mscorlibAssembly));
var collectionType = mscorlibAssembly.MainModule.GetTypeResolved(typeof(Dictionary<,>).FullName);
metadataReferences.Add(CreateMetadataReference(assemblyResolver, collectionType.Module.Assembly));
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assembly));
// In case SiliconStudio.Core was not referenced, let's add it.
if (assembly.Name.Name != "SiliconStudio.Core" && !references.Any(x => string.Compare(Path.GetFileNameWithoutExtension(x), "SiliconStudio.Core", StringComparison.OrdinalIgnoreCase) == 0))
{
metadataReferences.Add(CreateMetadataReference(assemblyResolver, assemblyResolver.Resolve("SiliconStudio.Core")));
}
// Create roslyn compilation object
var assemblyName = assembly.Name.Name + ".Serializers";
var compilation = CSharpCompilation.Create(assemblyName, syntaxTrees, metadataReferences, compilerOptions);
// Do the actual compilation, and check errors
using (var peStream = new FileStream(serializationAssemblyLocation, FileMode.Create, FileAccess.Write))
{
var compilationResult = compilation.Emit(peStream);
if (!compilationResult.Success)
{
var errors = new StringBuilder();
errors.AppendLine(string.Format("Serialization assembly compilation: {0} error(s)", compilationResult.Diagnostics.Count(x => x.Severity >= DiagnosticSeverity.Error)));
foreach (var error in compilationResult.Diagnostics)
{
if (error.Severity >= DiagnosticSeverity.Warning)
errors.AppendLine(error.ToString());
}
throw new InvalidOperationException(errors.ToString());
}
}
var repackOptions = new ILRepacking.RepackOptions(new string[0])
{
OutputFile = assembly.MainModule.FullyQualifiedName,
DebugInfo = true,
CopyAttributes = true,
AllowMultipleAssemblyLevelAttributes = true,
XmlDocumentation = false,
NoRepackRes = true,
InputAssemblies = new[] { serializationAssemblyLocation },
SearchDirectories = assemblyResolver.GetSearchDirectories(),
SearchAssemblies = references,
};
// Run ILMerge
var merge = new ILRepacking.ILRepack(repackOptions)
{
PrimaryAssemblyDefinition = assembly,
MemoryOnly = true,
//KeepFirstOfMultipleAssemblyLevelAttributes = true,
//Log = true,
//LogFile = "ilmerge.log",
};
try
{
var consoleWriter = Console.Out;
Console.SetOut(TextWriter.Null);
try
{
//.........这里部分代码省略.........
示例6: AddSigningOptions
private static CSharpCompilationOptions AddSigningOptions(CSharpCompilationOptions options, ICompilerOptions compilerOptions, string projectDirectory)
{
var useOssSigning = compilerOptions.UseOssSigning == true;
var keyFile =
Environment.GetEnvironmentVariable(EnvironmentNames.BuildKeyFile) ??
GetKeyFileFullPath(projectDirectory, compilerOptions.KeyFile);
if (!string.IsNullOrWhiteSpace(keyFile))
{
#if DOTNET5_4
return options.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile)));
#else
if (RuntimeEnvironmentHelper.IsMono || useOssSigning)
{
return options.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile)));
}
options = options.WithCryptoKeyFile(keyFile);
var delaySignString = Environment.GetEnvironmentVariable(EnvironmentNames.BuildDelaySign);
var delaySign =
delaySignString == null
? compilerOptions.DelaySign
: string.Equals(delaySignString, "true", StringComparison.OrdinalIgnoreCase) ||
string.Equals(delaySignString, "1", StringComparison.Ordinal);
return options.WithDelaySign(delaySign);
#endif
}
return useOssSigning ? options.WithCryptoPublicKey(StrongNameKey) : options;
}