/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see .
*/
using System;
using System.ComponentModel.Composition;
using dnlib.DotNet;
namespace dnSpy.Contracts.DnSpy.Metadata {
///
/// Resolves assemblies. Use
/// to export an instance.
///
public interface IRuntimeAssemblyResolver {
///
/// Tries to resolve an assembly or returns 'default' if it couldn't resolve it
///
/// Assembly that should be resolved
/// Module that needs to resolve an assembly
///
RuntimeAssemblyResolverResult Resolve(IAssembly assembly, ModuleDef? sourceModule);
}
/// Metadata
public interface IRuntimeAssemblyResolverMetadata {
/// See
double Order { get; }
}
///
/// Exports a instance
///
[MetadataAttribute, AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class ExportRuntimeAssemblyResolverAttribute : ExportAttribute, IRuntimeAssemblyResolverMetadata {
///
/// Constructor
///
/// Order of this instance
public ExportRuntimeAssemblyResolverAttribute(double order = double.MaxValue)
: base(typeof(IRuntimeAssemblyResolver)) => Order = order;
///
/// Order of this instance
///
public double Order { get; }
}
///
/// Resolved assembly result, see
///
public readonly struct RuntimeAssemblyResolverResult {
///
/// Checks if this is the 'default' instance
///
public bool IsDefault => Filename is null && GetFileData is null;
///
/// Filename of module or null/empty string if it's unknown
///
public string? Filename { get; }
///
/// A delegate that creates the assembly data or null
///
public Func<(byte[]? filedata, bool isFileLayout)>? GetFileData { get; }
RuntimeAssemblyResolverResult(string? filename, Func<(byte[]? filedata, bool isFileLayout)>? getFileData) {
Filename = filename;
GetFileData = getFileData;
}
///
/// Creates an instance
///
/// Filename
///
public static RuntimeAssemblyResolverResult Create(string filename) {
if (filename is null)
throw new ArgumentNullException(nameof(filename));
return new RuntimeAssemblyResolverResult(filename, getFileData: null);
}
///
/// Creates an instance
///
/// A delegate that creates the assembly data
/// Optional filename
///
public static RuntimeAssemblyResolverResult Create(Func<(byte[]? filedata, bool isFileLayout)> getFileData, string? filename) {
if (getFileData is null)
throw new ArgumentNullException(nameof(getFileData));
return new RuntimeAssemblyResolverResult(filename, getFileData);
}
}
}