forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Some more fixes and refactor #2
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3115,7 +3115,7 @@ private static Collection<CollectionEntry<PSMemberInfo>> GetTypeMemberCollection | |
| returnValue.Add(new CollectionEntry<PSMemberInfo>( | ||
| PSObject.TypeTableGetMembersDelegate<PSMemberInfo>, | ||
| PSObject.TypeTableGetMemberDelegate<PSMemberInfo>, | ||
| PSObject.TypeTableGetFirstOrDefaultMemberDelegate<PSMemberInfo>, | ||
| PSObject.TypeTableGetFirstMemberOrDefaultDelegate<PSMemberInfo>, | ||
| true, true, "type table members")); | ||
| return returnValue; | ||
| } | ||
|
|
@@ -3126,7 +3126,7 @@ private static Collection<CollectionEntry<PSMethodInfo>> GetTypeMethodCollection | |
| returnValue.Add(new CollectionEntry<PSMethodInfo>( | ||
| PSObject.TypeTableGetMembersDelegate<PSMethodInfo>, | ||
| PSObject.TypeTableGetMemberDelegate<PSMethodInfo>, | ||
| PSObject.TypeTableGetFirstOrDefaultMemberDelegate<PSMethodInfo>, | ||
| PSObject.TypeTableGetFirstMemberOrDefaultDelegate<PSMethodInfo>, | ||
| true, true, "type table members")); | ||
| return returnValue; | ||
| } | ||
|
|
@@ -3137,7 +3137,7 @@ private static Collection<CollectionEntry<PSPropertyInfo>> GetTypePropertyCollec | |
| returnValue.Add(new CollectionEntry<PSPropertyInfo>( | ||
| PSObject.TypeTableGetMembersDelegate<PSPropertyInfo>, | ||
| PSObject.TypeTableGetMemberDelegate<PSPropertyInfo>, | ||
| PSObject.TypeTableGetFirstOrDefaultMemberDelegate<PSPropertyInfo>, | ||
| PSObject.TypeTableGetFirstMemberOrDefaultDelegate<PSPropertyInfo>, | ||
| true, true, "type table members")); | ||
| return returnValue; | ||
| } | ||
|
|
@@ -3823,7 +3823,7 @@ IEnumerator IEnumerable.GetEnumerator() | |
|
|
||
| #endregion IEnumerable | ||
|
|
||
| internal abstract T GetFirstOrDefault(MemberNamePredicate predicate); | ||
| internal abstract T FirstOrDefault(MemberNamePredicate predicate); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -4274,15 +4274,19 @@ public override IEnumerator<T> GetEnumerator() | |
| } | ||
| } | ||
|
|
||
| internal override T GetFirstOrDefault(MemberNamePredicate predicate) | ||
| /// <summary> | ||
| /// Returns the first member that matches the specified <see cref="MemberNamePredicate"/>. | ||
| /// </summary> | ||
| internal override T FirstOrDefault(MemberNamePredicate predicate) | ||
| { | ||
| var enumerator = _members.GetEnumerator(); | ||
| while (enumerator.MoveNext()) | ||
| lock (_members) | ||
| { | ||
| var key = (string)enumerator.Key; | ||
| if (predicate(key)) | ||
| foreach (DictionaryEntry entry in _members) | ||
| { | ||
| return (T)enumerator.Value; | ||
| if (predicate((string)entry.Key)) | ||
| { | ||
| return entry.Value as T; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -4311,8 +4315,8 @@ internal CollectionEntry( | |
| GetMembers = getMembers; | ||
| GetMember = getMember; | ||
| GetFirstOrDefault = getFirstOrDefault; | ||
| ShouldReplicateWhenReturning = shouldReplicateWhenReturning; | ||
| ShouldCloneWhenReturning = shouldCloneWhenReturning; | ||
| _shouldReplicateWhenReturning = shouldReplicateWhenReturning; | ||
| _shouldCloneWhenReturning = shouldCloneWhenReturning; | ||
| CollectionNameForTracing = collectionNameForTracing; | ||
| } | ||
|
|
||
|
|
@@ -4324,20 +4328,19 @@ internal CollectionEntry( | |
|
|
||
| internal string CollectionNameForTracing { get; } | ||
|
|
||
| private bool ShouldReplicateWhenReturning { get; } | ||
| private readonly bool _shouldReplicateWhenReturning; | ||
|
|
||
| private bool ShouldCloneWhenReturning { get; } | ||
| private readonly bool _shouldCloneWhenReturning; | ||
|
|
||
| internal T CloneOrReplicateObject(object owner, T member) | ||
| { | ||
| if (ShouldCloneWhenReturning) | ||
| if (_shouldCloneWhenReturning) | ||
| { | ||
| member = (T)member.Copy(); | ||
| } | ||
|
|
||
| if (ShouldReplicateWhenReturning) | ||
| if (_shouldReplicateWhenReturning) | ||
| { | ||
| owner = owner is PSMemberSet memberSet ? memberSet.instance : owner; | ||
| member.ReplicateInstance(owner); | ||
| } | ||
|
|
||
|
|
@@ -4931,15 +4934,46 @@ public override IEnumerator<T> GetEnumerator() | |
| return new Enumerator<T>(this); | ||
| } | ||
|
|
||
| internal override T GetFirstOrDefault(MemberNamePredicate predicate) | ||
| internal override T FirstOrDefault(MemberNamePredicate predicate) | ||
| { | ||
| object delegateOwner; | ||
| if (_mshOwner != null) | ||
| { | ||
| delegateOwner = _mshOwner; | ||
| foreach (PSMemberInfo member in _mshOwner.InstanceMembers) | ||
| { | ||
| if (member is T memberAsT && predicate(memberAsT.Name)) | ||
| { | ||
| return memberAsT; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| delegateOwner = _memberSetOwner.instance; | ||
| foreach (PSMemberInfo member in _memberSetOwner.InternalMembers) | ||
| { | ||
| if (member is T memberAsT && predicate(memberAsT.Name)) | ||
| { | ||
| memberAsT.ReplicateInstance(delegateOwner); | ||
| return memberAsT; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (delegateOwner == null) | ||
| { | ||
| return null; | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle |
||
|
|
||
| var ownerAsPSObj = PSObject.AsPSObject(delegateOwner); | ||
| for (int i = 0; i < Collections.Count; i++) | ||
| { | ||
| var collectionEntry = Collections[i]; | ||
| var res = collectionEntry.GetFirstOrDefault(_mshOwner, predicate); | ||
| if (res != null) | ||
| var member = collectionEntry.GetFirstOrDefault(ownerAsPSObj, predicate); | ||
| if (member != null) | ||
| { | ||
| return collectionEntry.CloneOrReplicateObject(_mshOwner, res); | ||
| return collectionEntry.CloneOrReplicateObject(ownerAsPSObj, member); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, simply the code using
foreachloop.