GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve PSMethod-to-Delegate conversion by daxian-dbw · Pull Request #6570 · PowerShell/PowerShell

Improve PSMethod-to-Delegate conversion #6570

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.

Sign up for GitHub

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
Dongbo Wang (daxian-dbw) merged 1 commit into PowerShell:master from daxian-dbw:psmethod
Apr 7, 2018

Uh oh!

There was an error while loading. Please reload this page.

Merged

Improve PSMethod-to-Delegate conversion #6570

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
71907cf
[Feature] Improve PSMethod-to-Delegate conversion
daxian-dbw Apr 5, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 1 addition & 1 deletion 2 src/System.Management.Automation/engine/CoreAdapter.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ internal class MethodCacheEntry
/// <summary>
/// Cache delegate to the ctor of PSMethod&lt;&gt; with a template parameter derived from the methodInformationStructures.
/// </summary>
internal Func<string, DotNetAdapter, object, DotNetAdapter.MethodCacheEntry, bool, bool, PSMethod> psmethodCtor;
internal Func<string, DotNetAdapter, object, DotNetAdapter.MethodCacheEntry, bool, bool, PSMethod> PSMethodCtor;

internal MethodCacheEntry(MethodBase[] methods)
{
Expand Down
37 changes: 15 additions & 22 deletions 37 src/System.Management.Automation/engine/LanguagePrimitives.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq.Expressions;
using System.Management.Automation.Language;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Xml;
Expand All @@ -20,7 +21,7 @@
using System.Management.Automation.Runspaces;
using System.Diagnostics.CodeAnalysis; // for fxcop
using Dbg = System.Management.Automation.Diagnostics;
using System.Reflection.Emit;
using MethodCacheEntry = System.Management.Automation.DotNetAdapter.MethodCacheEntry;

#if !CORECLR
// System.DirectoryServices are not in CoreCLR
Expand Down Expand Up @@ -3210,31 +3211,24 @@ private static Delegate ConvertPSMethodInfoToDelegate(object valueToConvert,
IFormatProvider formatProvider,
TypeTable backupTable)
{
PSMethod psMethod;
// We can only possibly convert PSMethod instance of the type PSMethod<T>.
// Such a PSMethod essentially represents a set of .NET method overloads.
var psMethod = (PSMethod)valueToConvert;

try
{
psMethod = (PSMethod) valueToConvert;

var maybeType = psMethod.instance as Type;
var methodInfoCandiates = maybeType != null
? maybeType.GetMethods(BindingFlags.Static | BindingFlags.Public)
: psMethod.instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);

var methods = (MethodCacheEntry)psMethod.adapterData;
var isStatic = psMethod.instance is Type;
var targetMethodInfo = resultType.GetMethod("Invoke");

var comparator = new DelegateArgsComparator(targetMethodInfo);

foreach (var candidate in methodInfoCandiates)
foreach (var methodInformation in methods.methodInformationStructures)
{
if (candidate.Name != psMethod.Name)
{
continue;
}
var candidate = (MethodInfo)methodInformation.method;
if (comparator.SignatureMatches(candidate.ReturnType, candidate.GetParameters()))
{
return maybeType != null
? candidate.CreateDelegate(resultType)
: candidate.CreateDelegate(resultType, psMethod.instance);
return isStatic ? candidate.CreateDelegate(resultType)
: candidate.CreateDelegate(resultType, psMethod.instance);
}
}
}
Expand All @@ -3246,12 +3240,12 @@ private static Delegate ConvertPSMethodInfoToDelegate(object valueToConvert,
valueToConvert.ToString(), resultType.ToString(), e.Message);
}

var msg = String.Format(ExtendedTypeSystem.PSMethodToDelegateNoMatchingOverLoad, psMethod, resultType);
var msg = String.Format(ExtendedTypeSystem.PSMethodToDelegateNoMatchingOverLoad, psMethod, resultType);
typeConversion.WriteLine($"PSMethod to Delegate exception: \"{msg}\".");
throw new PSInvalidCastException("InvalidCastExceptionPSMethodToDelegate", null,
ExtendedTypeSystem.InvalidCastExceptionWithInnerException,
valueToConvert.ToString(), resultType.ToString(), msg);
}
}

private static object ConvertToNullable(object valueToConvert,
Type resultType,
Expand Down Expand Up @@ -4789,7 +4783,6 @@ private static ConversionData FigureLanguageConversion(Type fromType, Type toTyp
while (signatureEnumerator.MoveNext())
{
var candidate = signatureEnumerator.Current.GetMethod("Invoke");

if (comparator.SignatureMatches(candidate.ReturnType, candidate.GetParameters()))
{
return CacheConversion<Delegate>(fromType, toType, LanguagePrimitives.ConvertPSMethodInfoToDelegate, ConversionRank.Language);
Expand Down Expand Up @@ -4824,7 +4817,7 @@ private bool ReturnTypeMatches(Type returnType)
private bool ParameterTypesMatches(ParameterInfo[] arguments)
{
var argsCount = _targetParametersInfos.Length;
// void is encoded as typeof(Unit) in the PSMethod<MethodGroup<>> as the last parameter
// void is encoded as typeof(VOID) in the PSMethod<MethodGroup<>> as the last parameter
if (arguments.Length != argsCount)
{
return false;
Expand Down
35 changes: 17 additions & 18 deletions 35 src/System.Management.Automation/engine/MshMemberInfo.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2642,11 +2642,17 @@ internal static PSMethod Create(string name, DotNetAdapter dotNetInstanceAdapter

internal static PSMethod Create(string name, DotNetAdapter dotNetInstanceAdapter, object baseObject, DotNetAdapter.MethodCacheEntry method, bool isSpecial, bool isHidden)
{
if (method.psmethodCtor == null)
if (method[0].method is ConstructorInfo)
{
method.psmethodCtor = CreatePSMethodConstructor(method.methodInformationStructures);
// Constructor cannot be converted to a delegate, so just return a simple PSMethod instance
return new PSMethod(name, dotNetInstanceAdapter, baseObject, method, isSpecial, isHidden);
}
return method.psmethodCtor.Invoke(name, dotNetInstanceAdapter, baseObject, method, isSpecial, isHidden);

if (method.PSMethodCtor == null)
{
method.PSMethodCtor = CreatePSMethodConstructor(method.methodInformationStructures);
}
return method.PSMethodCtor.Invoke(name, dotNetInstanceAdapter, baseObject, method, isSpecial, isHidden);
}

static Type GetMethodGroupType(MethodInfo methodInfo)
Expand Down Expand Up @@ -2697,7 +2703,7 @@ private static Type GetPSMethodTypeProjection(Type type, bool isOut = false)
{
if (type == typeof(void))
{
return typeof(Unit);
return typeof(VOID);
}
if (type == typeof(TypedReference))
{
Expand Down Expand Up @@ -2742,7 +2748,7 @@ internal static bool MatchesPSMethodProjectedType(Type targetType, Type projecte
sourceType = sourceType.GenericTypeArguments[0];
}

if (targetType == typeof(void) && sourceType == typeof(Unit))
if (targetType == typeof(void) && sourceType == typeof(VOID))
{
return true;
}
Expand Down Expand Up @@ -2783,21 +2789,14 @@ private static MethodInfo ReplaceGenericTypeArgumentsWithMarkerTypes(MethodInfo

private static Func<string, DotNetAdapter, object, object, bool, bool, PSMethod> CreatePSMethodConstructor(MethodInformation[] methods)
{
// Produce the PSMethod creator for MethodInfo objects
var types = new Type[methods.Length];
for (int i = 0; i < methods.Length; i++)
{
var mb = methods[i].method;

if (mb is MethodInfo mi)
{
types[i] = GetMethodGroupType(mi);
}
else
{
types[i] = typeof(Unit);
}
types[i] = GetMethodGroupType((MethodInfo)methods[i].method);
}
var methodGroupType = CreateMethodGroup(types, 0, types.Length);

var methodGroupType = CreateMethodGroup(types, 0, types.Length);
Type psMethodType = typeof(PSMethod<>).MakeGenericType(methodGroupType);
var delegateType = typeof(Func<string, DotNetAdapter, object, object, bool, bool, PSMethod>);
return (Func<string, DotNetAdapter, object, object, bool, bool, PSMethod>)Delegate.CreateDelegate(delegateType, psMethodType.GetMethod("Create", BindingFlags.NonPublic|BindingFlags.Static));
Expand Down Expand Up @@ -2946,9 +2945,9 @@ internal class MethodGroup<T1, T2, T3, T4, T5, T6, T7, T8> : MethodGroup { }
internal class MethodGroup<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> : MethodGroup { }
internal class MethodGroup<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31, T32> : MethodGroup { }

class Unit
class VOID
{
private Unit() { }
private VOID() { }
}

internal struct PSMethodSignatureEnumerator : IEnumerator<Type>
Expand Down
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.