Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/CoreAdapter.cs
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 src/System.Management.Automation/engine/LanguagePrimitives.cs
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 src/System.Management.Automation/engine/MshMemberInfo.cs
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