Skip to content

Handle null Assembly.GetEntryAssembly in ReflectionHelper.GetExecutingOrEntryAssembly #434

Closed
@ohadschn

Description

@ohadschn

ReflectionHelper.GetExecutingOrEntryAssembly is implemented like this:

return Assembly.GetEntryAssembly();

However, going by its name it should actually be implemented like this:

private static Assembly GetExecutingOrEntryAssembly()
{
    return Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
}

Specifically, it would solve the failure of the current implementation when called from MSTest (for background see here):

Assert.Fail failed. Exception of type System.ArgumentException thrown as expected, but the provided predicate rejected it: System.ArgumentNullException: Value cannot be null.
Parameter name: element
   at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
   at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](Assembly element)
   at CommandLine.Infrastructure.ReflectionHelper.GetAttribute[TAttribute]()
   at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Func`2 onError, Func`2 onExample, Boolean verbsIndex, Int32 maxDisplayWidth)
   at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Int32 maxDisplayWidth)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions