I'm new to creating NuGet packages and I ran the following code snippet in various environments:
/// <summary>
/// Tries to find type by name in the executing assembly and after that
/// in referenced assemblies.
/// </summary>
/// <param name="typeName">Name of the type to find (can be full or assembly qualified name as well).</param>
/// <returns>Type found using the given name (or null if not found).</returns>
public static Type FindType(string typeName)
{
if (typeName == null) throw new ArgumentNullException(nameof(typeName));
// Helper method for finding the type in an assembly
Type Finder(Assembly ass) => ass?.GetTypes().FirstOrDefault(type =>
typeName.In(type.Name, type.FullName, type.AssemblyQualifiedName)
);
// Get the current assembly
var executingAssembly = Assembly.GetExecutingAssembly();
// Check if the type is inside the current assembly
var targetType = Finder(executingAssembly);
// Go through all of the referenced assemblies
foreach (var assName in executingAssembly.GetReferencedAssemblies())
{
// If the type was found, return it
if (targetType != null)
return targetType;
// Check if the type is inside the assembly
targetType = Finder(Assembly.Load(assName));
}
// Type wasn't found, return null
return null;
}
If I run it as local function or through a referenced project, it works fine, but when I create a NuGet package and call the method using the implementation of the method inside the NuGet package, it returns null.
The method Assembly.GetExecutingAssembly
claims it returns The assembly that contains the code that is currently executing
and yet I get different results when running it from NuGet package.
What can I do to get the correct output from the method if I pack it inside NuGet package?
Aucun commentaire:
Enregistrer un commentaire