Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null Assembly.GetEntryAssembly in ReflectionHelper.GetExecutingOrEntryAssembly #434

Closed
ohadschn opened this issue Apr 22, 2019 · 2 comments

Comments

@ohadschn
Copy link
Contributor

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)
@ohadschn ohadschn changed the title Handle null Assembly.GetEntryAssembly in GetExecutingOrEntryAssembly Handle null Assembly.GetEntryAssembly in ReflectionHelper.GetExecutingOrEntryAssembly Apr 22, 2019
@ohadschn ohadschn changed the title Handle null Assembly.GetEntryAssembly in ReflectionHelper.GetExecutingOrEntryAssembly Handle null Assembly.GetEntryAssembly in ReflectionHelper.GetExecutingOrEntryAssembly Apr 22, 2019
@moh-hassan
Copy link
Collaborator

The PR #430 which rebased from #387 have a correction to this problem with other approach using Assembly.GetCallingAssembly
The source code is here.

		private static Assembly GetExecutingOrEntryAssembly()
				{
					//resolve issues of null EntryAssembly in Xunit Test #392,424,389
					//return Assembly.GetEntryAssembly();
					return Assembly.GetEntryAssembly()??Assembly.GetCallingAssembly();
		}

The problem has two sides:

  • The null of GetEntryAssembly in net4x in test framework(it's resolved in netcore2.x)
  • The assembly attributes of Copyright, company,... may be null

This Proposal discuss the problem in detail

Currently, the Assembly.GetCallingAssembly is used in a separate class that inject it for net4x if it's null and help to resolve this problem.

The Assembly.GetCallingAssembly Method returns the Assembly of the method that invoked the currently executing method.

The GetExecutingAssembly is still used by test suit and so the assembly attributes may be null and cause the same issue.

The test framework don't modify the GetCallingAssembly .

The PR applied this solution of GetCallingAssembly and pass all unit test.

@ohadschn
Copy link
Contributor Author

Thank you, I guess this means this issue is a duplicate - closing in favor of #403.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants