Skip to content

Fix dynamic class assembly name - #5292

Merged
Jason Shirk (lzybkr) merged 2 commits into
PowerShell:masterfrom
lzybkr:fix_dyn_class_assem
Nov 2, 2017
Merged

Fix dynamic class assembly name#5292
Jason Shirk (lzybkr) merged 2 commits into
PowerShell:masterfrom
lzybkr:fix_dyn_class_assem

Conversation

@lzybkr

Copy link
Copy Markdown
Contributor

Using the assembly name to hint at the source of the classes was
problematic in multiple ways.

This change stores the actual filename in an attribute on the assembly.

So for a given type, one can get the assembly this way:

[SomeType].Assembly.GetCustomAttributes() |
? { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute] } |
% { $_.ScriptFile }

Using the assembly name to hint at the source of the classes was
problematic in multiple ways.

This change stores the actual filename in an attribute on the assembly.

So for a given type, one can get the assembly this way:

[SomeType].Assembly.GetCustomAttributes() |
    ? { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute] } |
    % { $_.ScriptFile }
typeof(DynamicClassImplementationAssemblyAttribute).GetProperty(nameof(DynamicClassImplementationAssemblyAttribute.ScriptFile)) };
var propertyArgs = new object[] { scriptFile };
var fieldInfoList = Utils.EmptyArray<FieldInfo>();
var fieldArgs = Utils.EmptyArray<object>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fieldInfoList and fieldArgs are not used anywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, cut and paste I guess.

{
yield return new CustomAttributeBuilder(typeof(DynamicClassImplementationAssemblyAttribute).GetConstructor(Type.EmptyTypes), s_emptyArgArray);
var ctor = typeof(DynamicClassImplementationAssemblyAttribute).GetConstructor(Type.EmptyTypes);
var emptyArgs = Utils.EmptyArray<object>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You call Utils.EmptyArray<object>() multiple times, why not just use s_emptyArgArray?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove s_emptyArgArray - it's probably not better to store 2 references to the same array, Utils.EmptyArray already has a static reference.


$a = [C].Assembly.GetCustomAttributes($false).Where{
$_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute]}
$a.ScriptFile | Should BeExactly (& { $MyInvocation.ScriptName })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe replace (& { $MyInvocation.ScriptName }) with $PSCommandPath?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, sure. My excuse - I've never used it before and was too lazy to look for it. :)

@SteveL-MSFT

Copy link
Copy Markdown
Member

This is great, but the usage seems too complex to remember. Can we use ETS to just add a ScriptFile property making it more discoverable?

@lzybkr

Copy link
Copy Markdown
Contributor Author

Steve Lee (@SteveL-MSFT) - I've only seen 1 request for this ability - from the author of ISESteroids - so ease of use doesn't seem too critical. I suppose we could use a NoteProperty on the assembly instance, but that is harder to access from C#.

@SteveL-MSFT

Copy link
Copy Markdown
Member

Jason Shirk (@lzybkr) we can wait for customer request before adding more. Thanks

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment

@lzybkr
Jason Shirk (lzybkr) merged commit 71d5439 into PowerShell:master Nov 2, 2017
@lzybkr
Jason Shirk (lzybkr) deleted the fix_dyn_class_assem branch November 2, 2017 17:29
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
Using the assembly name to hint at the source of the classes was
problematic in multiple ways.

This change stores the actual filename in an attribute on the assembly.

So for a given type, one can get the assembly this way:

[SomeType].Assembly.GetCustomAttributes() |
    ? { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute] } |
    % { $_.ScriptFile }
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

Successfully merging this pull request may close these issues.

4 participants