Skip to content

Conversation

@lzybkr
Copy link
Contributor

@lzybkr lzybkr commented Nov 1, 2017

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 }

@lzybkr lzybkr requested a review from BrucePay as a code owner November 1, 2017 06:12
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 }
@lzybkr lzybkr force-pushed the fix_dyn_class_assem branch from 6a668b3 to 7ca8ac8 Compare November 1, 2017 20:08
typeof(DynamicClassImplementationAssemblyAttribute).GetProperty(nameof(DynamicClassImplementationAssemblyAttribute.ScriptFile)) };
var propertyArgs = new object[] { scriptFile };
var fieldInfoList = Utils.EmptyArray<FieldInfo>();
var fieldArgs = Utils.EmptyArray<object>();
Copy link
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
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
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
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
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
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. :)

@daxian-dbw daxian-dbw self-assigned this Nov 1, 2017
@SteveL-MSFT
Copy link
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
Contributor Author

lzybkr commented Nov 2, 2017

@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
Member

@lzybkr we can wait for customer request before adding more. Thanks

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Leave a comment

@lzybkr lzybkr merged commit 71d5439 into PowerShell:master Nov 2, 2017
@lzybkr lzybkr deleted the fix_dyn_class_assem branch November 2, 2017 17:29
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