| |
| |
|
|
| using System.Collections.Concurrent; |
| using System.Collections.Generic; |
| using System.Collections.ObjectModel; |
| using System.ComponentModel; |
| using System.Data; |
| using System.Diagnostics; |
| using System.Diagnostics.CodeAnalysis; |
| using System.Dynamic; |
| using System.Globalization; |
| using System.Linq; |
| using System.Linq.Expressions; |
| using System.Management.Automation.Language; |
| using System.Management.Automation.Runspaces; |
| using System.Reflection; |
| using System.Reflection.Emit; |
| using System.Text; |
| using System.Threading; |
| using System.Xml; |
|
|
| using System.Management.Automation.Internal; |
| using Microsoft.PowerShell; |
| using Dbg = System.Management.Automation.Diagnostics; |
|
|
| #pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings |
|
|
| namespace System.Management.Automation |
| { |
| |
| |
| |
| |
| |
| |
| |
| internal abstract class Adapter |
| { |
| |
| |
| |
| [TraceSource("ETS", "Extended Type System")] |
| protected static PSTraceSource tracer = PSTraceSource.GetTracer("ETS", "Extended Type System"); |
| #region virtual |
|
|
| #region member |
|
|
| internal virtual bool CanSiteBinderOptimize(MemberTypes typeToOperateOn) { return false; } |
|
|
| protected static IEnumerable<string> GetDotNetTypeNameHierarchy(Type type) |
| { |
| for (; type != null; type = type.BaseType) |
| { |
| yield return type.FullName; |
| } |
| } |
|
|
| protected static IEnumerable<string> GetDotNetTypeNameHierarchy(object obj) |
| { |
| return GetDotNetTypeNameHierarchy(obj.GetType()); |
| } |
|
|
| |
| |
| |
| |
| protected virtual IEnumerable<string> GetTypeNameHierarchy(object obj) |
| { |
| return GetDotNetTypeNameHierarchy(obj); |
| } |
|
|
| |
| |
| |
| |
| |
| protected virtual ConsolidatedString GetInternedTypeNameHierarchy(object obj) |
| { |
| return new ConsolidatedString(GetTypeNameHierarchy(obj)); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected abstract T GetMember<T>(object obj, string memberName) where T : PSMemberInfo; |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected abstract T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected abstract PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) where T : PSMemberInfo; |
|
|
| #endregion member |
|
|
| #region property |
|
|
| |
| |
| |
| |
| |
| protected abstract object PropertyGet(PSProperty property); |
|
|
| |
| |
| |
| |
| |
| |
| protected abstract void PropertySet(PSProperty property, object setValue, bool convertIfPossible); |
|
|
| |
| |
| |
| |
| |
| protected abstract bool PropertyIsSettable(PSProperty property); |
|
|
| |
| |
| |
| |
| |
| protected abstract bool PropertyIsGettable(PSProperty property); |
|
|
| |
| |
| |
| |
| |
| |
| protected abstract string PropertyType(PSProperty property, bool forDisplay); |
|
|
| |
| |
| |
| |
| |
| protected abstract string PropertyToString(PSProperty property); |
|
|
| |
| |
| |
| |
| |
| protected abstract AttributeCollection PropertyAttributes(PSProperty property); |
|
|
| #endregion property |
|
|
| #region method |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual object MethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, object[] arguments) |
| { |
| return this.MethodInvoke(method, arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected abstract object MethodInvoke(PSMethod method, object[] arguments); |
|
|
| |
| |
| |
| |
| |
| protected abstract Collection<string> MethodDefinitions(PSMethod method); |
|
|
| |
| |
| |
| |
| protected virtual string MethodToString(PSMethod method) |
| { |
| StringBuilder returnValue = new StringBuilder(); |
| Collection<string> definitions = MethodDefinitions(method); |
| for (int i = 0; i < definitions.Count; i++) |
| { |
| returnValue.Append(definitions[i]); |
| returnValue.Append(", "); |
| } |
|
|
| returnValue.Remove(returnValue.Length - 2, 2); |
| return returnValue.ToString(); |
| } |
|
|
| #endregion method |
|
|
| #region parameterized property |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual string ParameterizedPropertyType(PSParameterizedProperty property) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual bool ParameterizedPropertyIsSettable(PSParameterizedProperty property) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual bool ParameterizedPropertyIsGettable(PSParameterizedProperty property) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual Collection<string> ParameterizedPropertyDefinitions(PSParameterizedProperty property) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual object ParameterizedPropertyGet(PSParameterizedProperty property, object[] arguments) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual void ParameterizedPropertySet(PSParameterizedProperty property, object setValue, object[] arguments) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected virtual string ParameterizedPropertyToString(PSParameterizedProperty property) |
| { |
| Diagnostics.Assert(false, "adapter is not called for parameterized properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| #endregion parameterized property |
|
|
| #endregion virtual |
|
|
| #region base |
|
|
| #region private |
|
|
| private static Exception NewException( |
| Exception e, |
| string errorId, |
| string targetErrorId, |
| string resourceString, |
| params object[] parameters) |
| { |
| object[] newParameters = new object[parameters.Length + 1]; |
| for (int i = 0; i < parameters.Length; i++) |
| { |
| newParameters[i + 1] = parameters[i]; |
| } |
|
|
| Exception ex = e as TargetInvocationException; |
| if (ex != null) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| newParameters[0] = inner.Message; |
| return new ExtendedTypeSystemException( |
| targetErrorId, |
| inner, |
| resourceString, |
| newParameters); |
| } |
|
|
| newParameters[0] = e.Message; |
| return new ExtendedTypeSystemException( |
| errorId, |
| e, |
| resourceString, |
| newParameters); |
| } |
|
|
| #endregion private |
|
|
| #region member |
|
|
| internal ConsolidatedString BaseGetTypeNameHierarchy(object obj) |
| { |
| try |
| { |
| return GetInternedTypeNameHierarchy(obj); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseGetTypeNameHierarchy", |
| "CatchFromBaseGetTypeNameHierarchyTI", |
| ExtendedTypeSystem.ExceptionRetrievingTypeNameHierarchy); |
| } |
| } |
|
|
| internal T BaseGetMember<T>(object obj, string memberName) where T : PSMemberInfo |
| { |
| try |
| { |
| return this.GetMember<T>(obj, memberName); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseGetMember", |
| "CatchFromBaseGetMemberTI", |
| ExtendedTypeSystem.ExceptionGettingMember, |
| memberName); |
| } |
| } |
|
|
| internal T BaseGetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| try |
| { |
| return this.GetFirstMemberOrDefault<T>(obj, predicate); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseGetMember", |
| "CatchFromBaseGetMemberTI", |
| ExtendedTypeSystem.ExceptionGettingMember, |
| nameof(predicate)); |
| } |
| } |
|
|
| internal PSMemberInfoInternalCollection<T> BaseGetMembers<T>(object obj) where T : PSMemberInfo |
| { |
| try |
| { |
| return this.GetMembers<T>(obj); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseGetMembers", |
| "CatchFromBaseGetMembersTI", |
| ExtendedTypeSystem.ExceptionGettingMembers); |
| } |
| } |
|
|
| #endregion member |
|
|
| #region property |
|
|
| internal object BasePropertyGet(PSProperty property) |
| { |
| try |
| { |
| return PropertyGet(property); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new GetValueInvocationException( |
| "CatchFromBaseAdapterGetValueTI", |
| inner, |
| ExtendedTypeSystem.ExceptionWhenGetting, |
| property.Name, inner.Message); |
| } |
| catch (GetValueException) { throw; } |
| catch (Exception e) |
| { |
| throw new GetValueInvocationException( |
| "CatchFromBaseAdapterGetValue", |
| e, |
| ExtendedTypeSystem.ExceptionWhenGetting, |
| property.Name, e.Message); |
| } |
| } |
|
|
| internal void BasePropertySet(PSProperty property, object setValue, bool convert) |
| { |
| try |
| { |
| PropertySet(property, setValue, convert); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new SetValueInvocationException( |
| "CatchFromBaseAdapterSetValueTI", |
| inner, |
| ExtendedTypeSystem.ExceptionWhenSetting, |
| property.Name, inner.Message); |
| } |
| catch (SetValueException) { throw; } |
| catch (Exception e) |
| { |
| throw new SetValueInvocationException( |
| "CatchFromBaseAdapterSetValue", |
| e, |
| ExtendedTypeSystem.ExceptionWhenSetting, |
| property.Name, e.Message); |
| } |
| } |
|
|
| internal bool BasePropertyIsSettable(PSProperty property) |
| { |
| try |
| { |
| return this.PropertyIsSettable(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBasePropertyIsSettable", |
| "CatchFromBasePropertyIsSettableTI", |
| ExtendedTypeSystem.ExceptionRetrievingPropertyWriteState, |
| property.Name); |
| } |
| } |
|
|
| internal bool BasePropertyIsGettable(PSProperty property) |
| { |
| try |
| { |
| return this.PropertyIsGettable(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBasePropertyIsGettable", |
| "CatchFromBasePropertyIsGettableTI", |
| ExtendedTypeSystem.ExceptionRetrievingPropertyReadState, |
| property.Name); |
| } |
| } |
|
|
| internal string BasePropertyType(PSProperty property) |
| { |
| try |
| { |
| return this.PropertyType(property, forDisplay: false); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBasePropertyType", |
| "CatchFromBasePropertyTypeTI", |
| ExtendedTypeSystem.ExceptionRetrievingPropertyType, |
| property.Name); |
| } |
| } |
|
|
| internal string BasePropertyToString(PSProperty property) |
| { |
| try |
| { |
| return this.PropertyToString(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBasePropertyToString", |
| "CatchFromBasePropertyToStringTI", |
| ExtendedTypeSystem.ExceptionRetrievingPropertyString, |
| property.Name); |
| } |
| } |
|
|
| internal AttributeCollection BasePropertyAttributes(PSProperty property) |
| { |
| try |
| { |
| return this.PropertyAttributes(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBasePropertyAttributes", |
| "CatchFromBasePropertyAttributesTI", |
| ExtendedTypeSystem.ExceptionRetrievingPropertyAttributes, |
| property.Name); |
| } |
| } |
|
|
| #endregion property |
|
|
| #region method |
| internal object BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, params object[] arguments) |
| { |
| try |
| { |
| return this.MethodInvoke(method, invocationConstraints, arguments); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new MethodInvocationException( |
| "CatchFromBaseAdapterMethodInvokeTI", |
| inner, |
| ExtendedTypeSystem.MethodInvocationException, |
| method.Name, |
| arguments.Length, |
| inner.Message); |
| } |
| catch (FlowControlException) { throw; } |
| catch (ScriptCallDepthException) { throw; } |
| catch (PipelineStoppedException) { throw; } |
| catch (MethodException) { throw; } |
| catch (Exception e) |
| { |
| if (method.baseObject is SteppablePipeline |
| && (method.Name.Equals("Begin", StringComparison.OrdinalIgnoreCase) || |
| method.Name.Equals("Process", StringComparison.OrdinalIgnoreCase) || |
| method.Name.Equals("End", StringComparison.OrdinalIgnoreCase))) |
| { |
| throw; |
| } |
|
|
| throw new MethodInvocationException( |
| "CatchFromBaseAdapterMethodInvoke", |
| e, |
| ExtendedTypeSystem.MethodInvocationException, |
| method.Name, |
| arguments.Length, |
| e.Message); |
| } |
| } |
|
|
| internal Collection<string> BaseMethodDefinitions(PSMethod method) |
| { |
| try |
| { |
| return this.MethodDefinitions(method); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseMethodDefinitions", |
| "CatchFromBaseMethodDefinitionsTI", |
| ExtendedTypeSystem.ExceptionRetrievingMethodDefinitions, |
| method.Name); |
| } |
| } |
|
|
| internal string BaseMethodToString(PSMethod method) |
| { |
| try |
| { |
| return this.MethodToString(method); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseMethodToString", |
| "CatchFromBaseMethodToStringTI", |
| ExtendedTypeSystem.ExceptionRetrievingMethodString, |
| method.Name); |
| } |
| } |
| #endregion method |
|
|
| #region parameterized property |
| internal string BaseParameterizedPropertyType(PSParameterizedProperty property) |
| { |
| try |
| { |
| return this.ParameterizedPropertyType(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseParameterizedPropertyType", |
| "CatchFromBaseParameterizedPropertyTypeTI", |
| ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertytype, |
| property.Name); |
| } |
| } |
|
|
| internal bool BaseParameterizedPropertyIsSettable(PSParameterizedProperty property) |
| { |
| try |
| { |
| return this.ParameterizedPropertyIsSettable(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseParameterizedPropertyIsSettable", |
| "CatchFromBaseParameterizedPropertyIsSettableTI", |
| ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyWriteState, |
| property.Name); |
| } |
| } |
|
|
| internal bool BaseParameterizedPropertyIsGettable(PSParameterizedProperty property) |
| { |
| try |
| { |
| return this.ParameterizedPropertyIsGettable(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseParameterizedPropertyIsGettable", |
| "CatchFromBaseParameterizedPropertyIsGettableTI", |
| ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyReadState, |
| property.Name); |
| } |
| } |
|
|
| internal Collection<string> BaseParameterizedPropertyDefinitions(PSParameterizedProperty property) |
| { |
| try |
| { |
| return this.ParameterizedPropertyDefinitions(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseParameterizedPropertyDefinitions", |
| "CatchFromBaseParameterizedPropertyDefinitionsTI", |
| ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyDefinitions, |
| property.Name); |
| } |
| } |
|
|
| internal object BaseParameterizedPropertyGet(PSParameterizedProperty property, params object[] arguments) |
| { |
| try |
| { |
| return this.ParameterizedPropertyGet(property, arguments); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new GetValueInvocationException( |
| "CatchFromBaseAdapterParameterizedPropertyGetValueTI", |
| inner, |
| ExtendedTypeSystem.ExceptionWhenGetting, |
| property.Name, |
| inner.Message); |
| } |
| catch (GetValueException) { throw; } |
| catch (Exception e) |
| { |
| throw new GetValueInvocationException( |
| "CatchFromBaseParameterizedPropertyAdapterGetValue", |
| e, |
| ExtendedTypeSystem.ExceptionWhenGetting, |
| property.Name, |
| e.Message); |
| } |
| } |
|
|
| internal void BaseParameterizedPropertySet(PSParameterizedProperty property, object setValue, params object[] arguments) |
| { |
| try |
| { |
| this.ParameterizedPropertySet(property, setValue, arguments); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new SetValueInvocationException( |
| "CatchFromBaseAdapterParameterizedPropertySetValueTI", |
| inner, |
| ExtendedTypeSystem.ExceptionWhenSetting, |
| property.Name, |
| inner.Message); |
| } |
| catch (SetValueException) { throw; } |
| catch (Exception e) |
| { |
| throw new SetValueInvocationException( |
| "CatchFromBaseAdapterParameterizedPropertySetValue", |
| e, |
| ExtendedTypeSystem.ExceptionWhenSetting, |
| property.Name, |
| e.Message); |
| } |
| } |
|
|
| internal string BaseParameterizedPropertyToString(PSParameterizedProperty property) |
| { |
| try |
| { |
| return this.ParameterizedPropertyToString(property); |
| } |
| catch (ExtendedTypeSystemException) { throw; } |
| catch (Exception e) |
| { |
| throw NewException( |
| e, |
| "CatchFromBaseParameterizedPropertyToString", |
| "CatchFromBaseParameterizedPropertyToStringTI", |
| ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyString, |
| property.Name); |
| } |
| } |
|
|
| #endregion parameterized property |
|
|
| #region Internal Helper Methods |
|
|
| private static Type GetArgumentType(object argument, bool isByRefParameter) |
| { |
| if (argument == null) |
| { |
| return typeof(LanguagePrimitives.Null); |
| } |
|
|
| if (isByRefParameter && argument is PSReference psref) |
| { |
| return GetArgumentType(PSObject.Base(psref.Value), isByRefParameter: false); |
| } |
|
|
| return GetObjectType(argument, debase: false); |
| } |
|
|
| internal static ConversionRank GetArgumentConversionRank(object argument, Type parameterType, bool isByRef, bool allowCastingToByRefLikeType) |
| { |
| Type fromType = null; |
| ConversionRank rank = ConversionRank.None; |
|
|
| if (allowCastingToByRefLikeType && parameterType.IsByRefLike) |
| { |
| |
| |
| |
| |
| fromType = GetArgumentType(PSObject.Base(argument), isByRefParameter: false); |
| if (fromType != typeof(LanguagePrimitives.Null)) |
| { |
| LanguagePrimitives.FigureCastConversion(fromType, parameterType, ref rank); |
| } |
|
|
| return rank; |
| } |
|
|
| fromType = GetArgumentType(argument, isByRef); |
| rank = LanguagePrimitives.GetConversionRank(fromType, parameterType); |
|
|
| if (rank == ConversionRank.None) |
| { |
| fromType = GetArgumentType(PSObject.Base(argument), isByRef); |
| rank = LanguagePrimitives.GetConversionRank(fromType, parameterType); |
| } |
|
|
| return rank; |
| } |
|
|
| private static ParameterInformation[] ExpandParameters(int argCount, ParameterInformation[] parameters, Type elementType) |
| { |
| Diagnostics.Assert(parameters[parameters.Length - 1].isParamArray, "ExpandParameters shouldn't be called on non-param method"); |
|
|
| ParameterInformation[] newParameters = new ParameterInformation[argCount]; |
| Array.Copy(parameters, newParameters, parameters.Length - 1); |
| for (int i = parameters.Length - 1; i < argCount; ++i) |
| { |
| newParameters[i] = new ParameterInformation(elementType, false, null, false); |
| } |
|
|
| return newParameters; |
| } |
|
|
| |
| |
| |
| |
| private static int CompareOverloadCandidates(OverloadCandidate candidate1, OverloadCandidate candidate2, object[] arguments) |
| { |
| Diagnostics.Assert(candidate1.conversionRanks.Length == candidate2.conversionRanks.Length, |
| "should have same number of conversions regardless of the number of parameters - default arguments are not included here"); |
|
|
| ParameterInformation[] params1 = candidate1.expandedParameters ?? candidate1.parameters; |
| ParameterInformation[] params2 = candidate2.expandedParameters ?? candidate2.parameters; |
|
|
| int betterCount = 0; |
| int multiplier = candidate1.conversionRanks.Length; |
| for (int i = 0; i < candidate1.conversionRanks.Length; ++i, --multiplier) |
| { |
| if (candidate1.conversionRanks[i] < candidate2.conversionRanks[i]) |
| { |
| betterCount -= multiplier; |
| } |
| else if (candidate1.conversionRanks[i] > candidate2.conversionRanks[i]) |
| { |
| betterCount += multiplier; |
| } |
| else if (candidate1.conversionRanks[i] == ConversionRank.UnrelatedArrays) |
| { |
| |
| Type argElemType = EffectiveArgumentType(arguments[i]).GetElementType(); |
| ConversionRank rank1 = LanguagePrimitives.GetConversionRank(argElemType, params1[i].parameterType.GetElementType()); |
| ConversionRank rank2 = LanguagePrimitives.GetConversionRank(argElemType, params2[i].parameterType.GetElementType()); |
| if (rank1 < rank2) |
| { |
| betterCount -= multiplier; |
| } |
| else if (rank1 > rank2) |
| { |
| betterCount += multiplier; |
| } |
| } |
| } |
|
|
| if (betterCount == 0) |
| { |
| multiplier = candidate1.conversionRanks.Length; |
| for (int i = 0; i < candidate1.conversionRanks.Length; ++i, multiplier = Math.Abs(multiplier) - 1) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ConversionRank rank1 = candidate1.conversionRanks[i]; |
| ConversionRank rank2 = candidate2.conversionRanks[i]; |
| if (rank1 < ConversionRank.NullToValue || rank2 < ConversionRank.NullToValue) |
| { |
| |
| |
| continue; |
| } |
|
|
| if ((rank1 >= ConversionRank.NumericImplicit) != (rank2 >= ConversionRank.NumericImplicit)) |
| { |
| |
| |
| |
| continue; |
| } |
|
|
| |
| |
| |
| if (rank1 >= ConversionRank.NumericImplicit) |
| { |
| multiplier = -multiplier; |
| } |
|
|
| |
| |
| rank1 = LanguagePrimitives.GetConversionRank(params1[i].parameterType, params2[i].parameterType); |
| rank2 = LanguagePrimitives.GetConversionRank(params2[i].parameterType, params1[i].parameterType); |
| if (rank1 < rank2) |
| { |
| betterCount += multiplier; |
| } |
| else if (rank1 > rank2) |
| { |
| betterCount -= multiplier; |
| } |
| } |
| } |
|
|
| if (betterCount == 0) |
| { |
| |
| for (int i = 0; i < candidate1.conversionRanks.Length; ++i) |
| { |
| if (params1[i].parameterType != params2[i].parameterType) |
| { |
| return 0; |
| } |
| } |
|
|
| |
| if (candidate1.expandedParameters != null && candidate2.expandedParameters != null) |
| { |
| |
| return (candidate1.parameters.Length > candidate2.parameters.Length) ? 1 : -1; |
| } |
| else if (candidate1.expandedParameters != null) |
| { |
| return -1; |
| } |
| else if (candidate2.expandedParameters != null) |
| { |
| return 1; |
| } |
|
|
| |
| betterCount = CompareTypeSpecificity(candidate1, candidate2); |
| } |
|
|
| |
| |
| if (betterCount == 0) |
| { |
| if (candidate1.parameters.Length < candidate2.parameters.Length) |
| { |
| return 1; |
| } |
| else if (candidate1.parameters.Length > candidate2.parameters.Length) |
| { |
| return -1; |
| } |
| } |
|
|
| return betterCount; |
| } |
|
|
| private static OverloadCandidate FindBestCandidate(List<OverloadCandidate> candidates, object[] arguments) |
| { |
| Dbg.Assert(candidates != null, "Caller should verify candidates != null"); |
|
|
| OverloadCandidate bestCandidateSoFar = null; |
| bool multipleBestCandidates = false; |
|
|
| for (int i = 0; i < candidates.Count; i++) |
| { |
| OverloadCandidate currentCandidate = candidates[i]; |
| if (bestCandidateSoFar == null) |
| { |
| bestCandidateSoFar = currentCandidate; |
| continue; |
| } |
|
|
| int comparisonResult = CompareOverloadCandidates(bestCandidateSoFar, currentCandidate, arguments); |
| if (comparisonResult == 0) |
| { |
| multipleBestCandidates = true; |
| } |
| else if (comparisonResult < 0) |
| { |
| bestCandidateSoFar = currentCandidate; |
| multipleBestCandidates = false; |
| } |
| } |
|
|
| Dbg.Assert( |
| !candidates.Any(otherCandidate => otherCandidate != bestCandidateSoFar && CompareOverloadCandidates(otherCandidate, bestCandidateSoFar, arguments) > 0), |
| "No other candidates are better than bestCandidateSoFar"); |
|
|
| return multipleBestCandidates ? null : bestCandidateSoFar; |
| } |
|
|
| private static OverloadCandidate FindBestCandidate(List<OverloadCandidate> candidates, object[] arguments, PSMethodInvocationConstraints invocationConstraints) |
| { |
| List<OverloadCandidate> filteredCandidates = candidates.Where(candidate => IsInvocationConstraintSatisfied(candidate, invocationConstraints)).ToList(); |
| if (filteredCandidates.Count > 0) |
| { |
| candidates = filteredCandidates; |
| } |
|
|
| OverloadCandidate bestCandidate = FindBestCandidate(candidates, arguments); |
| return bestCandidate; |
| } |
|
|
| private static int CompareTypeSpecificity(Type type1, Type type2) |
| { |
| if (type1.IsGenericParameter || type2.IsGenericParameter) |
| { |
| int result = 0; |
| if (type1.IsGenericParameter) |
| { |
| result -= 1; |
| } |
|
|
| if (type2.IsGenericParameter) |
| { |
| result += 1; |
| } |
|
|
| return result; |
| } |
|
|
| if (type1.IsArray) |
| { |
| Dbg.Assert(type2.IsArray, "Caller should verify that both overload candidates have the same parameter types"); |
| Dbg.Assert(type1.GetArrayRank() == type2.GetArrayRank(), "Caller should verify that both overload candidates have the same parameter types"); |
| return CompareTypeSpecificity(type1.GetElementType(), type2.GetElementType()); |
| } |
|
|
| if (type1.IsGenericType) |
| { |
| Dbg.Assert(type2.IsGenericType, "Caller should verify that both overload candidates have the same parameter types"); |
| Dbg.Assert(type1.GetGenericTypeDefinition() == type2.GetGenericTypeDefinition(), "Caller should verify that both overload candidates have the same parameter types"); |
| return CompareTypeSpecificity(type1.GetGenericArguments(), type2.GetGenericArguments()); |
| } |
|
|
| return 0; |
| } |
|
|
| private static int CompareTypeSpecificity(Type[] params1, Type[] params2) |
| { |
| Dbg.Assert(params1.Length == params2.Length, "Caller should verify that both overload candidates have the same number of parameters"); |
|
|
| bool candidate1hasAtLeastOneMoreSpecificParameter = false; |
| bool candidate2hasAtLeastOneMoreSpecificParameter = false; |
| for (int i = 0; i < params1.Length; ++i) |
| { |
| int specificityComparison = CompareTypeSpecificity(params1[i], params2[i]); |
| if (specificityComparison > 0) |
| { |
| candidate1hasAtLeastOneMoreSpecificParameter = true; |
| } |
| else if (specificityComparison < 0) |
| { |
| candidate2hasAtLeastOneMoreSpecificParameter = true; |
| } |
|
|
| if (candidate1hasAtLeastOneMoreSpecificParameter && candidate2hasAtLeastOneMoreSpecificParameter) |
| { |
| break; |
| } |
| } |
|
|
| if (candidate1hasAtLeastOneMoreSpecificParameter && !candidate2hasAtLeastOneMoreSpecificParameter) |
| { |
| return 1; |
| } |
| else if (candidate2hasAtLeastOneMoreSpecificParameter && !candidate1hasAtLeastOneMoreSpecificParameter) |
| { |
| return -1; |
| } |
| else |
| { |
| return 0; |
| } |
| } |
|
|
| |
| |
| |
| |
| private static int CompareTypeSpecificity(OverloadCandidate candidate1, OverloadCandidate candidate2) |
| { |
| if (!(candidate1.method.isGeneric || candidate2.method.isGeneric)) |
| { |
| return 0; |
| } |
|
|
| Type[] params1 = GetGenericMethodDefinitionIfPossible(candidate1.method.method).GetParameters().Select(static p => p.ParameterType).ToArray(); |
| Type[] params2 = GetGenericMethodDefinitionIfPossible(candidate2.method.method).GetParameters().Select(static p => p.ParameterType).ToArray(); |
| return CompareTypeSpecificity(params1, params2); |
| } |
|
|
| private static MethodBase GetGenericMethodDefinitionIfPossible(MethodBase method) |
| { |
| if (method.IsGenericMethod && !method.IsGenericMethodDefinition) |
| { |
| MethodInfo methodInfo = method as MethodInfo; |
| if (methodInfo != null) |
| { |
| return methodInfo.GetGenericMethodDefinition(); |
| } |
| } |
|
|
| return method; |
| } |
|
|
| [DebuggerDisplay("OverloadCandidate: {method.methodDefinition}")] |
| private sealed class OverloadCandidate |
| { |
| internal MethodInformation method; |
| internal ParameterInformation[] parameters; |
| internal ParameterInformation[] expandedParameters; |
| internal ConversionRank[] conversionRanks; |
|
|
| internal OverloadCandidate(MethodInformation method, int argCount) |
| { |
| this.method = method; |
| this.parameters = method.parameters; |
| conversionRanks = new ConversionRank[argCount]; |
| } |
| } |
|
|
| private static bool IsInvocationTargetConstraintSatisfied(MethodInformation method, PSMethodInvocationConstraints invocationConstraints) |
| { |
| Dbg.Assert(method != null, "Caller should verify method != null"); |
|
|
| if (method.method == null) |
| { |
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| var methodDeclaringType = method.method.DeclaringType; |
| if (invocationConstraints == null || invocationConstraints.MethodTargetType == null) |
| { |
| |
| |
|
|
| return !methodDeclaringType.IsInterface; |
| } |
|
|
| var targetType = invocationConstraints.MethodTargetType; |
| if (targetType.IsInterface) |
| { |
| |
| |
| return methodDeclaringType == targetType || (methodDeclaringType.IsInterface && targetType.IsSubclassOf(methodDeclaringType)); |
| } |
|
|
| if (methodDeclaringType.IsInterface) |
| { |
| |
| |
| |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| return targetType.IsAssignableFrom(methodDeclaringType) |
| || methodDeclaringType.IsAssignableFrom(targetType) |
| || (targetType.IsArray && methodDeclaringType == typeof(Array)); |
| } |
|
|
| private static bool IsInvocationConstraintSatisfied(OverloadCandidate overloadCandidate, PSMethodInvocationConstraints invocationConstraints) |
| { |
| Dbg.Assert(overloadCandidate != null, "Caller should verify overloadCandidate != null"); |
|
|
| if (invocationConstraints == null) |
| { |
| return true; |
| } |
|
|
| if (invocationConstraints.ParameterTypes != null) |
| { |
| int parameterIndex = 0; |
| foreach (Type parameterTypeConstraint in invocationConstraints.ParameterTypes) |
| { |
| if (parameterTypeConstraint != null) |
| { |
| if (parameterIndex >= overloadCandidate.parameters.Length) |
| { |
| return false; |
| } |
|
|
| Type parameterType = overloadCandidate.parameters[parameterIndex].parameterType; |
| if (parameterType != parameterTypeConstraint) |
| { |
| return false; |
| } |
| } |
|
|
| parameterIndex++; |
| } |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static MethodInformation FindBestMethod( |
| MethodInformation[] methods, |
| PSMethodInvocationConstraints invocationConstraints, |
| bool allowCastingToByRefLikeType, |
| object[] arguments, |
| ref string errorId, |
| ref string errorMsg, |
| out bool expandParamsOnBest, |
| out bool callNonVirtually) |
| { |
| callNonVirtually = false; |
| var methodInfo = FindBestMethodImpl(methods, invocationConstraints, allowCastingToByRefLikeType, arguments, ref errorId, ref errorMsg, out expandParamsOnBest); |
| if (methodInfo == null) |
| { |
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (invocationConstraints != null && |
| invocationConstraints.MethodTargetType != null && |
| methodInfo.method != null && |
| methodInfo.method.DeclaringType != null) |
| { |
| Type targetType = methodInfo.method.DeclaringType; |
| if (targetType != invocationConstraints.MethodTargetType && targetType.IsSubclassOf(invocationConstraints.MethodTargetType)) |
| { |
| var parameterTypes = methodInfo.method.GetParameters().Select(static parameter => parameter.ParameterType).ToArray(); |
| var targetTypeMethod = invocationConstraints.MethodTargetType.GetMethod(methodInfo.method.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null); |
|
|
| if (targetTypeMethod != null && (targetTypeMethod.IsPublic || targetTypeMethod.IsFamily || targetTypeMethod.IsFamilyOrAssembly)) |
| { |
| methodInfo = new MethodInformation(targetTypeMethod, 0); |
| callNonVirtually = true; |
| } |
| } |
| } |
|
|
| return methodInfo; |
| } |
|
|
| private static Type[] ResolveGenericTypeParameters(object[] genericTypeParameters) |
| { |
| if (genericTypeParameters is null || genericTypeParameters.Length == 0) |
| { |
| return null; |
| } |
|
|
| Type[] genericParamTypes = new Type[genericTypeParameters.Length]; |
| for (int i = 0; i < genericTypeParameters.Length; i++) |
| { |
| genericParamTypes[i] = genericTypeParameters[i] switch |
| { |
| Type paramType => paramType, |
| ITypeName paramTypeName => TypeOps.ResolveTypeName(paramTypeName, paramTypeName.Extent), |
| _ => throw new ArgumentException("Unexpected value"), |
| }; |
| } |
|
|
| return genericParamTypes; |
| } |
|
|
| private static MethodInformation FindBestMethodImpl( |
| MethodInformation[] methods, |
| PSMethodInvocationConstraints invocationConstraints, |
| bool allowCastingToByRefLikeType, |
| object[] arguments, |
| ref string errorId, |
| ref string errorMsg, |
| out bool expandParamsOnBest) |
| { |
| expandParamsOnBest = false; |
|
|
| |
| |
| |
| |
| |
| |
| if (methods.Length == 1 |
| && !methods[0].hasVarArgs |
| |
| && !methods[0].isGeneric |
| && (methods[0].method is null || !methods[0].method.DeclaringType.IsGenericTypeDefinition) |
| && methods[0].parameters.Length == arguments.Length) |
| { |
| return methods[0]; |
| } |
|
|
| Type[] genericParamTypes = ResolveGenericTypeParameters(invocationConstraints?.GenericTypeParameters); |
| var candidates = new List<OverloadCandidate>(); |
|
|
| for (int i = 0; i < methods.Length; i++) |
| { |
| MethodInformation methodInfo = methods[i]; |
|
|
| if (methodInfo.method?.DeclaringType.IsGenericTypeDefinition == true |
| || (!methodInfo.isGeneric && genericParamTypes is not null)) |
| { |
| |
| |
| continue; |
| } |
|
|
| if (methodInfo.isGeneric) |
| { |
| if (genericParamTypes is not null) |
| { |
| try |
| { |
| |
| |
| |
| var originalMethod = (MethodInfo)methodInfo.method; |
| methodInfo = new MethodInformation( |
| originalMethod.MakeGenericMethod(genericParamTypes), |
| parametersToIgnore: 0); |
| } |
| catch (ArgumentException) |
| { |
| |
| |
| continue; |
| } |
| } |
| else |
| { |
| |
| Type[] argumentTypes = arguments.Select(EffectiveArgumentType).ToArray(); |
| Type[] paramConstraintTypes = invocationConstraints?.ParameterTypes; |
|
|
| if (paramConstraintTypes is not null) |
| { |
| for (int k = 0; k < paramConstraintTypes.Length; k++) |
| { |
| if (paramConstraintTypes[k] is not null) |
| { |
| argumentTypes[k] = paramConstraintTypes[k]; |
| } |
| } |
| } |
|
|
| methodInfo = TypeInference.Infer(methodInfo, argumentTypes); |
| if (methodInfo is null) |
| { |
| |
| continue; |
| } |
| } |
| } |
|
|
| if (!IsInvocationTargetConstraintSatisfied(methodInfo, invocationConstraints)) |
| { |
| continue; |
| } |
|
|
| ParameterInformation[] parameters = methodInfo.parameters; |
| if (arguments.Length != parameters.Length) |
| { |
| |
|
|
| if (arguments.Length > parameters.Length) |
| { |
| |
| if (!methodInfo.hasVarArgs) |
| { |
| continue; |
| } |
| } |
| else |
| { |
| |
| if (!methodInfo.hasOptional && (!methodInfo.hasVarArgs || (arguments.Length + 1) != parameters.Length)) |
| { |
| continue; |
| } |
|
|
| if (methodInfo.hasOptional) |
| { |
| |
| |
| |
| int optionals = 0; |
| for (int j = 0; j < parameters.Length; j++) |
| { |
| if (parameters[j].isOptional) |
| { |
| optionals += 1; |
| } |
| } |
|
|
| if (arguments.Length + optionals < parameters.Length) |
| { |
| |
| continue; |
| } |
| } |
| } |
| } |
|
|
| OverloadCandidate candidate = new OverloadCandidate(methodInfo, arguments.Length); |
| for (int j = 0; candidate != null && j < parameters.Length; j++) |
| { |
| ParameterInformation parameter = parameters[j]; |
| if (parameter.isOptional && arguments.Length <= j) |
| { |
| break; |
| } |
| else if (parameter.isParamArray) |
| { |
| Type elementType = parameter.parameterType.GetElementType(); |
| if (parameters.Length == arguments.Length) |
| { |
| ConversionRank arrayConv = GetArgumentConversionRank( |
| arguments[j], |
| parameter.parameterType, |
| isByRef: false, |
| allowCastingToByRefLikeType: false); |
|
|
| ConversionRank elemConv = GetArgumentConversionRank( |
| arguments[j], |
| elementType, |
| isByRef: false, |
| allowCastingToByRefLikeType: false); |
|
|
| if (elemConv > arrayConv) |
| { |
| candidate.expandedParameters = ExpandParameters(arguments.Length, parameters, elementType); |
| candidate.conversionRanks[j] = elemConv; |
| } |
| else |
| { |
| candidate.conversionRanks[j] = arrayConv; |
| } |
|
|
| if (candidate.conversionRanks[j] == ConversionRank.None) |
| { |
| candidate = null; |
| } |
| } |
| else |
| { |
| |
| |
| |
| for (int k = j; k < arguments.Length; k++) |
| { |
| candidate.conversionRanks[k] = GetArgumentConversionRank( |
| arguments[k], |
| elementType, |
| isByRef: false, |
| allowCastingToByRefLikeType: false); |
|
|
| if (candidate.conversionRanks[k] == ConversionRank.None) |
| { |
| |
| candidate = null; |
| break; |
| } |
| } |
|
|
| if (candidate != null) |
| { |
| candidate.expandedParameters = ExpandParameters(arguments.Length, parameters, elementType); |
| } |
| } |
| } |
| else |
| { |
| candidate.conversionRanks[j] = GetArgumentConversionRank( |
| arguments[j], |
| parameter.parameterType, |
| parameter.isByRef, |
| allowCastingToByRefLikeType); |
|
|
| if (candidate.conversionRanks[j] == ConversionRank.None) |
| { |
| |
| candidate = null; |
| } |
| } |
| } |
|
|
| if (candidate != null) |
| { |
| candidates.Add(candidate); |
| } |
| } |
|
|
| if (candidates.Count == 0) |
| { |
| if (methods.Length > 0 && methods.All(static m => m.method != null && m.method.DeclaringType.IsGenericTypeDefinition && m.method.IsStatic)) |
| { |
| errorId = "CannotInvokeStaticMethodOnUninstantiatedGenericType"; |
| errorMsg = string.Format( |
| CultureInfo.InvariantCulture, |
| ExtendedTypeSystem.CannotInvokeStaticMethodOnUninstantiatedGenericType, |
| methods[0].method.DeclaringType.FullName); |
| return null; |
| } |
| else if (genericParamTypes is not null) |
| { |
| errorId = "MethodCountCouldNotFindBestGeneric"; |
| errorMsg = string.Format( |
| ExtendedTypeSystem.MethodGenericArgumentCountException, |
| methods[0].method.Name, |
| genericParamTypes.Length, |
| arguments.Length); |
| return null; |
| } |
| else |
| { |
| errorId = "MethodCountCouldNotFindBest"; |
| errorMsg = ExtendedTypeSystem.MethodArgumentCountException; |
| return null; |
| } |
| } |
|
|
| OverloadCandidate bestCandidate = candidates.Count == 1 |
| ? candidates[0] |
| : FindBestCandidate(candidates, arguments, invocationConstraints); |
| if (bestCandidate != null) |
| { |
| expandParamsOnBest = bestCandidate.expandedParameters != null; |
| return bestCandidate.method; |
| } |
|
|
| errorId = "MethodCountCouldNotFindBest"; |
| errorMsg = ExtendedTypeSystem.MethodAmbiguousException; |
| return null; |
| } |
|
|
| internal static Type EffectiveArgumentType(object arg) |
| { |
| arg = PSObject.Base(arg); |
| if (arg is null) |
| { |
| return typeof(LanguagePrimitives.Null); |
| } |
|
|
| if (arg is object[] array && array.Length > 0) |
| { |
| Type firstType = GetObjectType(array[0], debase: true); |
| if (firstType is not null) |
| { |
| bool allSameType = true; |
| for (int j = 1; j < array.Length; ++j) |
| { |
| if (firstType != GetObjectType(array[j], debase: true)) |
| { |
| allSameType = false; |
| break; |
| } |
| } |
|
|
| if (allSameType) |
| { |
| return firstType.MakeArrayType(); |
| } |
| } |
| } |
|
|
| return GetObjectType(arg, debase: false); |
| } |
|
|
| internal static Type GetObjectType(object obj, bool debase) |
| { |
| if (debase) |
| { |
| obj = PSObject.Base(obj); |
| } |
|
|
| return obj == NullString.Value ? typeof(string) : obj?.GetType(); |
| } |
|
|
| internal static void SetReferences(object[] arguments, MethodInformation methodInformation, object[] originalArguments) |
| { |
| using (PSObject.MemberResolution.TraceScope("Checking for possible references.")) |
| { |
| ParameterInformation[] parameters = methodInformation.parameters; |
| for (int i = 0; (i < originalArguments.Length) && (i < parameters.Length) && (i < arguments.Length); i++) |
| { |
| object originalArgument = originalArguments[i]; |
| PSReference originalArgumentReference = originalArgument as PSReference; |
| |
| if (originalArgumentReference == null) |
| { |
| if (originalArgument is not PSObject originalArgumentObj) |
| { |
| continue; |
| } |
|
|
| originalArgumentReference = originalArgumentObj.BaseObject as PSReference; |
| if (originalArgumentReference == null) |
| { |
| continue; |
| } |
| } |
|
|
| ParameterInformation parameter = parameters[i]; |
| if (!parameter.isByRef) |
| { |
| continue; |
| } |
|
|
| object argument = arguments[i]; |
| PSObject.MemberResolution.WriteLine("Argument '{0}' was a reference so it will be set to \"{1}\".", i + 1, argument); |
| originalArgumentReference.Value = argument; |
| } |
| } |
| } |
|
|
| internal static MethodInformation GetBestMethodAndArguments( |
| string methodName, |
| MethodInformation[] methods, |
| object[] arguments, |
| out object[] newArguments) |
| { |
| return GetBestMethodAndArguments(methodName, methods, null, arguments, out newArguments); |
| } |
|
|
| internal static MethodInformation GetBestMethodAndArguments( |
| string methodName, |
| MethodInformation[] methods, |
| PSMethodInvocationConstraints invocationConstraints, |
| object[] arguments, |
| out object[] newArguments) |
| { |
| bool expandParamsOnBest; |
| bool callNonVirtually; |
| string errorId = null; |
| string errorMsg = null; |
|
|
| MethodInformation bestMethod = FindBestMethod( |
| methods, |
| invocationConstraints, |
| allowCastingToByRefLikeType: false, |
| arguments, |
| ref errorId, |
| ref errorMsg, |
| out expandParamsOnBest, |
| out callNonVirtually); |
|
|
| if (bestMethod == null) |
| { |
| throw new MethodException(errorId, null, errorMsg, methodName, arguments.Length); |
| } |
|
|
| newArguments = GetMethodArgumentsBase(methodName, bestMethod.parameters, arguments, expandParamsOnBest); |
| return bestMethod; |
| } |
|
|
| |
| |
| |
| |
| internal static object[] GetMethodArgumentsBase(string methodName, |
| ParameterInformation[] parameters, object[] arguments, |
| bool expandParamsOnBest) |
| { |
| int parametersLength = parameters.Length; |
| if (parametersLength == 0) |
| { |
| return Array.Empty<object>(); |
| } |
|
|
| object[] retValue = new object[parametersLength]; |
| for (int i = 0; i < parametersLength - 1; i++) |
| { |
| ParameterInformation parameter = parameters[i]; |
| SetNewArgument(methodName, arguments, retValue, parameter, i); |
| } |
|
|
| ParameterInformation lastParameter = parameters[parametersLength - 1]; |
| if (!expandParamsOnBest) |
| { |
| SetNewArgument(methodName, arguments, retValue, lastParameter, parametersLength - 1); |
| return retValue; |
| } |
|
|
| |
|
|
| |
| if (arguments.Length < parametersLength) |
| { |
| retValue[parametersLength - 1] = Array.CreateInstance(lastParameter.parameterType.GetElementType(), new int[] { 0 }); |
| return retValue; |
| } |
|
|
| |
| |
| |
| int remainingArgumentCount = arguments.Length - parametersLength + 1; |
| if (remainingArgumentCount == 1 && arguments[arguments.Length - 1] == null) |
| { |
| |
| retValue[parametersLength - 1] = null; |
| } |
| else |
| { |
| object[] remainingArguments = new object[remainingArgumentCount]; |
| Type paramsElementType = lastParameter.parameterType.GetElementType(); |
| for (int j = 0; j < remainingArgumentCount; j++) |
| { |
| int argumentIndex = j + parametersLength - 1; |
| try |
| { |
| remainingArguments[j] = MethodArgumentConvertTo(arguments[argumentIndex], false, argumentIndex, |
| paramsElementType, CultureInfo.InvariantCulture); |
| } |
| catch (InvalidCastException e) |
| { |
| |
| throw new MethodException( |
| "MethodArgumentConversionInvalidCastArgument", |
| e, |
| ExtendedTypeSystem.MethodArgumentConversionException, |
| argumentIndex, arguments[argumentIndex], methodName, paramsElementType, e.Message); |
| } |
| } |
|
|
| try |
| { |
| retValue[parametersLength - 1] = MethodArgumentConvertTo(remainingArguments, |
| lastParameter.isByRef, parametersLength - 1, lastParameter.parameterType, |
| CultureInfo.InvariantCulture); |
| } |
| catch (InvalidCastException e) |
| { |
| |
| throw new MethodException( |
| "MethodArgumentConversionParamsConversion", |
| e, |
| ExtendedTypeSystem.MethodArgumentConversionException, |
| parametersLength - 1, remainingArguments, methodName, lastParameter.parameterType, e.Message); |
| } |
| } |
|
|
| return retValue; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| internal static void SetNewArgument(string methodName, object[] arguments, |
| object[] newArguments, ParameterInformation parameter, int index) |
| { |
| if (arguments.Length > index) |
| { |
| try |
| { |
| newArguments[index] = MethodArgumentConvertTo(arguments[index], parameter.isByRef, index, |
| parameter.parameterType, CultureInfo.InvariantCulture); |
| } |
| catch (InvalidCastException e) |
| { |
| |
| throw new MethodException( |
| "MethodArgumentConversionInvalidCastArgument", |
| e, |
| ExtendedTypeSystem.MethodArgumentConversionException, |
| index, arguments[index], methodName, parameter.parameterType, e.Message); |
| } |
| } |
| else |
| { |
| Diagnostics.Assert(parameter.isOptional, "FindBestMethod would not return this method if there is no corresponding argument for a non optional parameter"); |
| newArguments[index] = parameter.defaultValue; |
| } |
| } |
|
|
| internal static object MethodArgumentConvertTo(object valueToConvert, |
| bool isParameterByRef, int parameterIndex, Type resultType, |
| IFormatProvider formatProvider) |
| { |
| using (PSObject.MemberResolution.TraceScope("Method argument conversion.")) |
| { |
| if (resultType == null) |
| { |
| throw PSTraceSource.NewArgumentNullException(nameof(resultType)); |
| } |
|
|
| bool isArgumentByRef; |
| valueToConvert = UnReference(valueToConvert, out isArgumentByRef); |
| if (isParameterByRef && !isArgumentByRef) |
| { |
| throw new MethodException("NonRefArgumentToRefParameterMsg", null, |
| ExtendedTypeSystem.NonRefArgumentToRefParameter, parameterIndex + 1, typeof(PSReference).FullName, "[ref]"); |
| } |
|
|
| if (isArgumentByRef && !isParameterByRef) |
| { |
| throw new MethodException("RefArgumentToNonRefParameterMsg", null, |
| ExtendedTypeSystem.RefArgumentToNonRefParameter, parameterIndex + 1, typeof(PSReference).FullName, "[ref]"); |
| } |
|
|
| return PropertySetAndMethodArgumentConvertTo(valueToConvert, resultType, formatProvider); |
| } |
| } |
|
|
| internal static object UnReference(object obj, out bool isArgumentByRef) |
| { |
| isArgumentByRef = false; |
| PSReference reference = obj as PSReference; |
| if (reference != null) |
| { |
| PSObject.MemberResolution.WriteLine("Parameter was a reference."); |
| isArgumentByRef = true; |
| return reference.Value; |
| } |
|
|
| PSObject mshObj = obj as PSObject; |
| if (mshObj != null) |
| { |
| reference = mshObj.BaseObject as PSReference; |
| } |
|
|
| if (reference != null) |
| { |
| PSObject.MemberResolution.WriteLine("Parameter was an PSObject containing a reference."); |
| isArgumentByRef = true; |
| return reference.Value; |
| } |
|
|
| return obj; |
| } |
|
|
| internal static object PropertySetAndMethodArgumentConvertTo(object valueToConvert, |
| Type resultType, IFormatProvider formatProvider) |
| { |
| using (PSObject.MemberResolution.TraceScope("Converting parameter \"{0}\" to \"{1}\".", valueToConvert, resultType)) |
| { |
| if (resultType == null) |
| { |
| throw PSTraceSource.NewArgumentNullException(nameof(resultType)); |
| } |
|
|
| PSObject mshObj = valueToConvert as PSObject; |
| if (mshObj != null) |
| { |
| if (resultType == typeof(object)) |
| { |
| PSObject.MemberResolution.WriteLine("Parameter was an PSObject and will be converted to System.Object."); |
| |
| |
| return PSObject.Base(mshObj); |
| } |
| } |
|
|
| return LanguagePrimitives.ConvertTo(valueToConvert, resultType, formatProvider); |
| } |
| } |
|
|
| internal static void DoBoxingIfNecessary(ILGenerator generator, Type type) |
| { |
| if (type.IsByRef) |
| { |
| |
| |
| |
| |
| type = type.GetElementType(); |
| if (type.IsPrimitive) |
| { |
| if (type == typeof(byte)) { generator.Emit(OpCodes.Ldind_U1); } |
| else if (type == typeof(ushort)) { generator.Emit(OpCodes.Ldind_U2); } |
| else if (type == typeof(uint)) { generator.Emit(OpCodes.Ldind_U4); } |
| else if (type == typeof(sbyte)) { generator.Emit(OpCodes.Ldind_I8); } |
| else if (type == typeof(short)) { generator.Emit(OpCodes.Ldind_I2); } |
| else if (type == typeof(int)) { generator.Emit(OpCodes.Ldind_I4); } |
| else if (type == typeof(long)) { generator.Emit(OpCodes.Ldind_I8); } |
| else if (type == typeof(float)) { generator.Emit(OpCodes.Ldind_R4); } |
| else if (type == typeof(double)) { generator.Emit(OpCodes.Ldind_R8); } |
| } |
| else if (type.IsValueType) |
| { |
| generator.Emit(OpCodes.Ldobj, type); |
| } |
| else |
| { |
| generator.Emit(OpCodes.Ldind_Ref); |
| } |
| } |
| else if (type.IsPointer) |
| { |
| |
| |
| |
| |
| MethodInfo boxMethod = typeof(Pointer).GetMethod("Box"); |
| MethodInfo typeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle"); |
| generator.Emit(OpCodes.Ldtoken, type); |
| generator.Emit(OpCodes.Call, typeFromHandle); |
| generator.Emit(OpCodes.Call, boxMethod); |
| } |
|
|
| if (type.IsValueType) |
| { |
| generator.Emit(OpCodes.Box, type); |
| } |
| } |
|
|
| #endregion |
|
|
| #endregion base |
| } |
|
|
| |
| |
| |
| |
| internal abstract class CacheEntry |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal virtual bool IsHidden => false; |
| } |
|
|
| |
| |
| |
| internal class CacheTable |
| { |
| |
| |
| |
| |
| internal Collection<object> memberCollection; |
| private readonly Dictionary<string, int> _indexes; |
|
|
| internal CacheTable() |
| { |
| memberCollection = new Collection<object>(); |
| _indexes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| } |
|
|
| internal void Add(string name, object member) |
| { |
| _indexes[name] = memberCollection.Count; |
| memberCollection.Add(member); |
| } |
|
|
| internal object this[string name] |
| { |
| get |
| { |
| int indexObj; |
| if (!_indexes.TryGetValue(name, out indexObj)) |
| { |
| return null; |
| } |
|
|
| return memberCollection[indexObj]; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal object GetFirstOrDefault(MemberNamePredicate predicate) |
| { |
| foreach (var entry in _indexes) |
| { |
| if (predicate(entry.Key)) |
| { |
| object member = memberCollection[entry.Value]; |
| if (member is CacheEntry cacheEntry && cacheEntry.IsHidden) |
| { |
| continue; |
| } |
|
|
| return member; |
| } |
| } |
|
|
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| [DebuggerDisplay("MethodInformation: {methodDefinition}")] |
| internal class MethodInformation |
| { |
| internal MethodBase method; |
| private string _cachedMethodDefinition; |
|
|
| internal string methodDefinition |
| { |
| get |
| { |
| if (_cachedMethodDefinition == null) |
| { |
| var name = method is ConstructorInfo ? "new" : method.Name; |
| var methodDefn = DotNetAdapter.GetMethodInfoOverloadDefinition(name, method, method.GetParameters().Length - parameters.Length); |
| Interlocked.CompareExchange(ref _cachedMethodDefinition, methodDefn, null); |
| } |
|
|
| return _cachedMethodDefinition; |
| } |
| } |
|
|
| internal ParameterInformation[] parameters; |
| internal bool hasVarArgs; |
| internal bool hasOptional; |
| internal bool isGeneric; |
|
|
| private bool _useReflection; |
|
|
| private delegate object MethodInvoker(object target, object[] arguments); |
|
|
| private MethodInvoker _methodInvoker; |
|
|
| |
| |
| |
| internal MethodInformation(MethodBase method, int parametersToIgnore) |
| { |
| this.method = method; |
| this.isGeneric = method.IsGenericMethod; |
| ParameterInfo[] methodParameters = method.GetParameters(); |
| int parametersLength = methodParameters.Length - parametersToIgnore; |
| this.parameters = new ParameterInformation[parametersLength]; |
|
|
| for (int i = 0; i < parametersLength; i++) |
| { |
| this.parameters[i] = new ParameterInformation(methodParameters[i]); |
| if (methodParameters[i].IsOptional) |
| { |
| hasOptional = true; |
| } |
| } |
|
|
| this.hasVarArgs = false; |
| if (parametersLength > 0) |
| { |
| ParameterInfo lastParameter = methodParameters[parametersLength - 1]; |
|
|
| |
| |
| if (!hasOptional && lastParameter.ParameterType.IsArray) |
| { |
| |
| |
| |
| |
| |
| |
| var paramArrayAttrs = lastParameter.GetCustomAttributes(typeof(ParamArrayAttribute), false); |
| if (paramArrayAttrs != null && paramArrayAttrs.Length > 0) |
| { |
| this.hasVarArgs = true; |
| this.parameters[parametersLength - 1].isParamArray = true; |
| } |
| } |
| } |
| } |
|
|
| internal MethodInformation(bool hasvarargs, bool hasoptional, ParameterInformation[] arguments) |
| { |
| hasVarArgs = hasvarargs; |
| hasOptional = hasoptional; |
| parameters = arguments; |
| } |
|
|
| internal object Invoke(object target, object[] arguments) |
| { |
| |
| |
| |
| |
| |
| |
|
|
| if (method is ConstructorInfo ctor) |
| { |
| if (ctor.DeclaringType.IsByRefLike) |
| { |
| throw new MethodException( |
| nameof(ExtendedTypeSystem.CannotInstantiateBoxedByRefLikeType), |
| innerException: null, |
| ExtendedTypeSystem.CannotInstantiateBoxedByRefLikeType, |
| ctor.DeclaringType); |
| } |
|
|
| return ctor.Invoke(arguments); |
| } |
|
|
| var methodInfo = (MethodInfo)method; |
| if (methodInfo.ReturnType.IsByRefLike) |
| { |
| throw new MethodException( |
| nameof(ExtendedTypeSystem.CannotCallMethodWithByRefLikeReturnType), |
| innerException: null, |
| ExtendedTypeSystem.CannotCallMethodWithByRefLikeReturnType, |
| methodInfo.Name, |
| methodInfo.ReturnType); |
| } |
|
|
| if (target is PSObject) |
| { |
| if (!method.DeclaringType.IsAssignableFrom(target.GetType())) |
| { |
| target = PSObject.Base(target); |
| } |
| } |
|
|
| if (!_useReflection) |
| { |
| _methodInvoker ??= GetMethodInvoker(methodInfo); |
|
|
| if (_methodInvoker != null) |
| { |
| return _methodInvoker(target, arguments); |
| } |
| } |
|
|
| return method.Invoke(target, arguments); |
| } |
|
|
| private static readonly OpCode[] s_ldc = new OpCode[] { |
| OpCodes.Ldc_I4_0, OpCodes.Ldc_I4_1, OpCodes.Ldc_I4_2, OpCodes.Ldc_I4_3, OpCodes.Ldc_I4_4, |
| OpCodes.Ldc_I4_5, OpCodes.Ldc_I4_6, OpCodes.Ldc_I4_7, OpCodes.Ldc_I4_8 |
| }; |
|
|
| private static void EmitLdc(ILGenerator emitter, int c) |
| { |
| if (c < s_ldc.Length) |
| { |
| emitter.Emit(s_ldc[c]); |
| } |
| else |
| { |
| emitter.Emit(OpCodes.Ldc_I4, c); |
| } |
| } |
|
|
| private static bool CompareMethodParameters(MethodBase method1, MethodBase method2) |
| { |
| ParameterInfo[] params1 = method1.GetParameters(); |
| ParameterInfo[] params2 = method2.GetParameters(); |
|
|
| if (params1.Length != params2.Length) |
| { |
| return false; |
| } |
|
|
| for (int i = 0; i < params1.Length; ++i) |
| { |
| if (params1[i].ParameterType != params2[i].ParameterType) |
| { |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
|
|
| private static Type FindInterfaceForMethod(MethodInfo method, out MethodInfo methodToCall) |
| { |
| methodToCall = null; |
|
|
| Type valuetype = method.DeclaringType; |
|
|
| Diagnostics.Assert(valuetype.IsValueType, "This code only works with valuetypes"); |
|
|
| Type[] interfaces = valuetype.GetInterfaces(); |
| for (int i = 0; i < interfaces.Length; i++) |
| { |
| Type type = interfaces[i]; |
| MethodInfo methodInfo = type.GetMethod(method.Name, BindingFlags.Instance); |
| if (methodInfo != null && CompareMethodParameters(methodInfo, method)) |
| { |
| methodToCall = methodInfo; |
| return type; |
| } |
| } |
|
|
| |
|
|
| return null; |
| } |
|
|
| [SuppressMessage("NullPtr", "#pw26500", Justification = "This is a false positive. Original warning was on the deference of 'locals' on line 1863: emitter.Emit(OpCodes.Ldloca, locals[cLocal])")] |
| private MethodInvoker GetMethodInvoker(MethodInfo method) |
| { |
| Type type; |
| bool valueTypeInstanceMethod = false; |
| bool anyOutOrRefParameters = false; |
| bool mustStoreRetVal = false; |
| MethodInfo methodToCall = method; |
| int cLocal = 0; |
| int c; |
|
|
| DynamicMethod dynamicMethod = new DynamicMethod(method.Name, typeof(object), |
| new Type[] { typeof(object), typeof(object[]) }, typeof(Adapter).Module, true); |
|
|
| ILGenerator emitter = dynamicMethod.GetILGenerator(); |
| ParameterInfo[] parameters = method.GetParameters(); |
|
|
| int localCount = 0; |
| if (!method.IsStatic && method.DeclaringType.IsValueType) |
| { |
| if (!method.IsVirtual) |
| { |
| |
| valueTypeInstanceMethod = true; |
| localCount += 1; |
| } |
| } |
|
|
| for (int i = 0; i < parameters.Length; i++) |
| { |
| |
| |
| |
| if (parameters[i].IsOut || parameters[i].ParameterType.IsByRef) |
| { |
| anyOutOrRefParameters = true; |
| localCount += 1; |
| } |
| } |
|
|
| LocalBuilder[] locals = null; |
| Type returnType = method.ReturnType; |
| if (localCount > 0) |
| { |
| if (anyOutOrRefParameters && returnType != typeof(void)) |
| { |
| |
| |
| |
| localCount += 1; |
| mustStoreRetVal = true; |
| } |
|
|
| locals = new LocalBuilder[localCount]; |
|
|
| cLocal = 0; |
| if (valueTypeInstanceMethod) |
| { |
| |
| type = method.DeclaringType; |
| locals[cLocal] = emitter.DeclareLocal(type); |
| emitter.Emit(OpCodes.Ldarg_0); |
| emitter.Emit(OpCodes.Unbox_Any, type); |
| emitter.Emit(OpCodes.Stloc, locals[cLocal]); |
| cLocal += 1; |
| } |
|
|
| |
| |
| for (c = 0; c < parameters.Length; ++c) |
| { |
| type = parameters[c].ParameterType; |
| if (parameters[c].IsOut || type.IsByRef) |
| { |
| if (type.IsByRef) |
| { |
| type = type.GetElementType(); |
| } |
|
|
| locals[cLocal] = emitter.DeclareLocal(type); |
|
|
| emitter.Emit(OpCodes.Ldarg_1); |
| EmitLdc(emitter, c); |
| emitter.Emit(OpCodes.Ldelem_Ref); |
| if (type.IsValueType) |
| { |
| emitter.Emit(OpCodes.Unbox_Any, type); |
| } |
| else if (type != typeof(object)) |
| { |
| emitter.Emit(OpCodes.Castclass, type); |
| } |
|
|
| emitter.Emit(OpCodes.Stloc, locals[cLocal]); |
|
|
| cLocal += 1; |
| } |
| } |
|
|
| if (mustStoreRetVal) |
| { |
| locals[cLocal] = emitter.DeclareLocal(returnType); |
| } |
| } |
|
|
| cLocal = 0; |
| if (!method.IsStatic) |
| { |
| |
| if (method.DeclaringType.IsValueType) |
| { |
| if (method.IsVirtual) |
| { |
| type = FindInterfaceForMethod(method, out methodToCall); |
| if (type == null) |
| { |
| _useReflection = true; |
| return null; |
| } |
|
|
| emitter.Emit(OpCodes.Ldarg_0); |
| emitter.Emit(OpCodes.Castclass, type); |
| } |
| else |
| { |
| emitter.Emit(OpCodes.Ldloca, locals[cLocal]); |
| cLocal += 1; |
| } |
| } |
| else |
| { |
| emitter.Emit(OpCodes.Ldarg_0); |
| } |
| } |
|
|
| for (c = 0; c < parameters.Length; c++) |
| { |
| type = parameters[c].ParameterType; |
| if (type.IsByRef) |
| { |
| emitter.Emit(OpCodes.Ldloca, locals[cLocal]); |
| cLocal += 1; |
| } |
| else if (parameters[c].IsOut) |
| { |
| emitter.Emit(OpCodes.Ldloc, locals[cLocal]); |
| cLocal += 1; |
| } |
| else |
| { |
| emitter.Emit(OpCodes.Ldarg_1); |
| EmitLdc(emitter, c); |
| emitter.Emit(OpCodes.Ldelem_Ref); |
|
|
| |
| if (type.IsValueType) |
| { |
| emitter.Emit(OpCodes.Unbox_Any, type); |
| } |
| |
| else if (type != typeof(object)) |
| { |
| emitter.Emit(OpCodes.Castclass, type); |
| } |
| } |
| } |
|
|
| emitter.Emit(method.IsStatic ? OpCodes.Call : OpCodes.Callvirt, methodToCall); |
|
|
| if (mustStoreRetVal) |
| { |
| emitter.Emit(OpCodes.Stloc, locals[locals.Length - 1]); |
| } |
|
|
| |
| |
| if (anyOutOrRefParameters) |
| { |
| cLocal = valueTypeInstanceMethod ? 1 : 0; |
| for (c = 0; c < parameters.Length; c++) |
| { |
| type = parameters[c].ParameterType; |
| if (!parameters[c].IsOut && !type.IsByRef) |
| { |
| continue; |
| } |
|
|
| if (type.IsByRef) |
| { |
| type = type.GetElementType(); |
| } |
|
|
| emitter.Emit(OpCodes.Ldarg_1); |
| EmitLdc(emitter, c); |
| emitter.Emit(OpCodes.Ldloc, locals[cLocal]); |
|
|
| |
| if (type.IsValueType) |
| { |
| emitter.Emit(OpCodes.Box, type); |
| } |
|
|
| emitter.Emit(OpCodes.Stelem_Ref); |
| cLocal += 1; |
| } |
| } |
|
|
| |
| if (returnType == typeof(void)) |
| { |
| emitter.Emit(OpCodes.Ldnull); |
| } |
| else |
| { |
| if (mustStoreRetVal) |
| { |
| |
| emitter.Emit(OpCodes.Ldloc, locals[locals.Length - 1]); |
| } |
|
|
| Adapter.DoBoxingIfNecessary(emitter, returnType); |
| } |
|
|
| emitter.Emit(OpCodes.Ret); |
|
|
| return (MethodInvoker)dynamicMethod.CreateDelegate(typeof(MethodInvoker)); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal class ParameterInformation |
| { |
| internal Type parameterType; |
| internal object defaultValue; |
| internal bool isOptional; |
| internal bool isByRef; |
| internal bool isParamArray; |
|
|
| internal ParameterInformation(System.Reflection.ParameterInfo parameter) |
| { |
| this.isOptional = parameter.IsOptional; |
| this.defaultValue = parameter.DefaultValue; |
| this.parameterType = parameter.ParameterType; |
| if (this.parameterType.IsByRef) |
| { |
| this.isByRef = true; |
| this.parameterType = this.parameterType.GetElementType(); |
| } |
| else |
| { |
| this.isByRef = false; |
| } |
| } |
|
|
| internal ParameterInformation(Type parameterType, bool isOptional, object defaultValue, bool isByRef) |
| { |
| this.parameterType = parameterType; |
| this.isOptional = isOptional; |
| this.defaultValue = defaultValue; |
| this.isByRef = isByRef; |
| } |
| } |
|
|
| |
| |
| |
| |
| internal class DotNetAdapter : Adapter |
| { |
| #region auxiliary methods and classes |
|
|
| private const BindingFlags instanceBindingFlags = (BindingFlags.FlattenHierarchy | BindingFlags.Public | |
| BindingFlags.IgnoreCase | BindingFlags.Instance); |
|
|
| private const BindingFlags staticBindingFlags = (BindingFlags.FlattenHierarchy | BindingFlags.Public | |
| BindingFlags.IgnoreCase | BindingFlags.Static); |
|
|
| private readonly bool _isStatic; |
|
|
| internal DotNetAdapter() { } |
|
|
| internal DotNetAdapter(bool isStatic) |
| { |
| _isStatic = isStatic; |
| } |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, CacheTable> s_instancePropertyCacheTable = new Dictionary<Type, CacheTable>(); |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, CacheTable> s_staticPropertyCacheTable = new Dictionary<Type, CacheTable>(); |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, CacheTable> s_instanceMethodCacheTable = new Dictionary<Type, CacheTable>(); |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, CacheTable> s_staticMethodCacheTable = new Dictionary<Type, CacheTable>(); |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, Dictionary<string, EventCacheEntry>> s_instanceEventCacheTable |
| = new Dictionary<Type, Dictionary<string, EventCacheEntry>>(); |
|
|
| |
| |
| |
| |
| private static readonly Dictionary<Type, Dictionary<string, EventCacheEntry>> s_staticEventCacheTable |
| = new Dictionary<Type, Dictionary<string, EventCacheEntry>>(); |
|
|
| internal class MethodCacheEntry : CacheEntry |
| { |
| internal readonly MethodInformation[] methodInformationStructures; |
| |
| |
| |
| internal Func<string, DotNetAdapter, object, DotNetAdapter.MethodCacheEntry, bool, bool, PSMethod> PSMethodCtor; |
|
|
| internal MethodCacheEntry(IList<MethodBase> methods) |
| { |
| methodInformationStructures = DotNetAdapter.GetMethodInformationArray(methods); |
| } |
|
|
| internal MethodInformation this[int i] |
| { |
| get |
| { |
| return methodInformationStructures[i]; |
| } |
| } |
|
|
| private bool? _isHidden; |
|
|
| internal override bool IsHidden |
| { |
| get |
| { |
| if (_isHidden == null) |
| { |
| bool hasHiddenAttribute = false; |
| foreach (var method in methodInformationStructures) |
| { |
| if (method.method.GetCustomAttributes(typeof(HiddenAttribute), inherit: false).Length != 0) |
| { |
| hasHiddenAttribute = true; |
| break; |
| } |
| } |
|
|
| _isHidden = hasHiddenAttribute; |
| } |
|
|
| return _isHidden.Value; |
| } |
| } |
| } |
|
|
| internal class EventCacheEntry : CacheEntry |
| { |
| internal EventInfo[] events; |
|
|
| internal EventCacheEntry(EventInfo[] events) |
| { |
| this.events = events; |
| } |
| } |
|
|
| internal class ParameterizedPropertyCacheEntry : CacheEntry |
| { |
| internal MethodInformation[] getterInformation; |
| internal MethodInformation[] setterInformation; |
| internal string propertyName; |
| internal bool readOnly; |
| internal bool writeOnly; |
| internal Type propertyType; |
| |
| internal string[] propertyDefinition; |
|
|
| internal ParameterizedPropertyCacheEntry(List<PropertyInfo> properties) |
| { |
| PropertyInfo firstProperty = properties[0]; |
| this.propertyName = firstProperty.Name; |
| this.propertyType = firstProperty.PropertyType; |
| var getterList = new List<MethodInfo>(); |
| var setterList = new List<MethodInfo>(); |
| var definitionArray = new List<string>(); |
|
|
| for (int i = 0; i < properties.Count; i++) |
| { |
| PropertyInfo property = properties[i]; |
| |
| |
| if (property.PropertyType != this.propertyType) |
| { |
| this.propertyType = typeof(object); |
| } |
|
|
| |
| MethodInfo propertyGetter = property.GetGetMethod(); |
| StringBuilder definition = new StringBuilder(); |
| StringBuilder extraDefinition = new StringBuilder(); |
| if (propertyGetter != null) |
| { |
| extraDefinition.Append("get;"); |
| definition.Append(DotNetAdapter.GetMethodInfoOverloadDefinition(this.propertyName, propertyGetter, 0)); |
| getterList.Add(propertyGetter); |
| } |
|
|
| |
| MethodInfo propertySetter = property.GetSetMethod(); |
| if (propertySetter != null) |
| { |
| extraDefinition.Append("set;"); |
| if (definition.Length == 0) |
| { |
| definition.Append(DotNetAdapter.GetMethodInfoOverloadDefinition(this.propertyName, propertySetter, 1)); |
| } |
|
|
| setterList.Add(propertySetter); |
| } |
|
|
| definition.Append(" {"); |
| definition.Append(extraDefinition); |
| definition.Append('}'); |
| definitionArray.Add(definition.ToString()); |
| } |
|
|
| propertyDefinition = definitionArray.ToArray(); |
|
|
| this.writeOnly = getterList.Count == 0; |
| this.readOnly = setterList.Count == 0; |
|
|
| this.getterInformation = new MethodInformation[getterList.Count]; |
| for (int i = 0; i < getterList.Count; i++) |
| { |
| this.getterInformation[i] = new MethodInformation(getterList[i], 0); |
| } |
|
|
| this.setterInformation = new MethodInformation[setterList.Count]; |
| for (int i = 0; i < setterList.Count; i++) |
| { |
| this.setterInformation[i] = new MethodInformation(setterList[i], 1); |
| } |
| } |
| } |
|
|
| internal class PropertyCacheEntry : CacheEntry |
| { |
| internal delegate object GetterDelegate(object instance); |
|
|
| internal delegate void SetterDelegate(object instance, object setValue); |
|
|
| internal PropertyCacheEntry(PropertyInfo property) |
| { |
| this.member = property; |
| this.propertyType = property.PropertyType; |
| |
| |
| |
| Type declaringType = property.DeclaringType; |
|
|
| if (declaringType.IsValueType || |
| propertyType.IsGenericType || |
| declaringType.IsGenericType || |
| declaringType.IsCOMObject || |
| propertyType.IsCOMObject) |
| { |
| this.readOnly = property.GetSetMethod() == null; |
| this.writeOnly = property.GetGetMethod() == null; |
| this.useReflection = true; |
| return; |
| } |
|
|
| |
| MethodInfo propertyGetter = property.GetGetMethod(true); |
| if (propertyGetter != null && (propertyGetter.IsPublic || propertyGetter.IsFamily || propertyGetter.IsFamilyOrAssembly)) |
| { |
| this.isStatic = propertyGetter.IsStatic; |
| |
| } |
| else |
| { |
| this.writeOnly = true; |
| } |
|
|
| |
| MethodInfo propertySetter = property.GetSetMethod(true); |
| if (propertySetter != null && (propertySetter.IsPublic || propertySetter.IsFamily || propertySetter.IsFamilyOrAssembly)) |
| { |
| this.isStatic = propertySetter.IsStatic; |
| } |
| else |
| { |
| this.readOnly = true; |
| } |
| } |
|
|
| internal PropertyCacheEntry(FieldInfo field) |
| { |
| this.member = field; |
| this.isStatic = field.IsStatic; |
| this.propertyType = field.FieldType; |
|
|
| |
| |
| |
| if (field.IsLiteral || field.IsInitOnly) |
| { |
| this.readOnly = true; |
| } |
| } |
|
|
| private void InitGetter() |
| { |
| if (writeOnly || useReflection) |
| { |
| return; |
| } |
|
|
| var parameter = Expression.Parameter(typeof(object)); |
| Expression instance = null; |
|
|
| var field = member as FieldInfo; |
| if (field != null) |
| { |
| var declaringType = field.DeclaringType; |
| if (!field.IsStatic) |
| { |
| if (declaringType.IsValueType) |
| { |
| |
| |
| instance = Nullable.GetUnderlyingType(declaringType) != null |
| ? (Expression)Expression.Property(parameter, "Value") |
| : Expression.Unbox(parameter, declaringType); |
| } |
| else |
| { |
| instance = parameter.Cast(declaringType); |
| } |
| } |
|
|
| Expression getterExpr; |
|
|
| if (declaringType.IsGenericTypeDefinition) |
| { |
| Expression innerException = Expression.New(CachedReflectionInfo.GetValueException_ctor, |
| Expression.Constant("PropertyGetException"), |
| Expression.Constant(null, typeof(Exception)), |
| Expression.Constant(ParserStrings.PropertyInGenericType), |
| Expression.NewArrayInit(typeof(object), Expression.Constant(field.Name))); |
| getterExpr = Compiler.ThrowRuntimeErrorWithInnerException("PropertyGetException", |
| Expression.Constant(ParserStrings.PropertyInGenericType), |
| innerException, typeof(object), Expression.Constant(field.Name)); |
| } |
| else |
| { |
| getterExpr = Expression.Field(instance, field).Cast(typeof(object)); |
| } |
|
|
| _getterDelegate = Expression.Lambda<GetterDelegate>(getterExpr, parameter).Compile(); |
| return; |
| } |
|
|
| var property = (PropertyInfo)member; |
| var propertyGetter = property.GetGetMethod(true); |
|
|
| instance = this.isStatic ? null : parameter.Cast(propertyGetter.DeclaringType); |
| _getterDelegate = Expression.Lambda<GetterDelegate>( |
| Expression.Property(instance, property).Cast(typeof(object)), parameter).Compile(); |
| } |
|
|
| private void InitSetter() |
| { |
| if (readOnly || useReflection) |
| { |
| return; |
| } |
|
|
| var parameter = Expression.Parameter(typeof(object)); |
| var value = Expression.Parameter(typeof(object)); |
| Expression instance = null; |
|
|
| var field = member as FieldInfo; |
| if (field != null) |
| { |
| var declaringType = field.DeclaringType; |
| if (!field.IsStatic) |
| { |
| if (declaringType.IsValueType) |
| { |
| |
| |
| instance = Nullable.GetUnderlyingType(declaringType) != null |
| ? (Expression)Expression.Property(parameter, "Value") |
| : Expression.Unbox(parameter, declaringType); |
| } |
| else |
| { |
| instance = parameter.Cast(declaringType); |
| } |
| } |
|
|
| Expression setterExpr; |
| string errMessage = null; |
| Type errType = field.FieldType; |
| if (declaringType.IsGenericTypeDefinition) |
| { |
| errMessage = ParserStrings.PropertyInGenericType; |
| if (errType.ContainsGenericParameters) |
| { |
| errType = typeof(object); |
| } |
| } |
| else if (readOnly) |
| { |
| errMessage = ParserStrings.PropertyIsReadOnly; |
| } |
|
|
| if (errMessage != null) |
| { |
| Expression innerException = Expression.New(CachedReflectionInfo.SetValueException_ctor, |
| Expression.Constant("PropertyAssignmentException"), |
| Expression.Constant(null, typeof(Exception)), |
| Expression.Constant(errMessage), |
| Expression.NewArrayInit(typeof(object), Expression.Constant(field.Name))); |
| setterExpr = Compiler.ThrowRuntimeErrorWithInnerException("PropertyAssignmentException", |
| Expression.Constant(errMessage), |
| innerException, errType, Expression.Constant(field.Name)); |
| } |
| else |
| { |
| setterExpr = Expression.Assign(Expression.Field(instance, field), Expression.Convert(value, field.FieldType)); |
| } |
|
|
| _setterDelegate = Expression.Lambda<SetterDelegate>(setterExpr, parameter, value).Compile(); |
| return; |
| } |
|
|
| var property = (PropertyInfo)member; |
| MethodInfo propertySetter = property.GetSetMethod(true); |
|
|
| instance = this.isStatic ? null : parameter.Cast(propertySetter.DeclaringType); |
| _setterDelegate = |
| Expression.Lambda<SetterDelegate>( |
| Expression.Assign(Expression.Property(instance, property), |
| Expression.Convert(value, property.PropertyType)), parameter, value).Compile(); |
| } |
|
|
| internal MemberInfo member; |
|
|
| internal GetterDelegate getterDelegate |
| { |
| get |
| { |
| if (_getterDelegate == null) |
| { |
| InitGetter(); |
| } |
|
|
| return _getterDelegate; |
| } |
| } |
|
|
| private GetterDelegate _getterDelegate; |
|
|
| internal SetterDelegate setterDelegate |
| { |
| get |
| { |
| if (_setterDelegate == null) |
| { |
| InitSetter(); |
| } |
|
|
| return _setterDelegate; |
| } |
| } |
|
|
| private SetterDelegate _setterDelegate; |
|
|
| internal bool useReflection; |
| internal bool readOnly; |
| internal bool writeOnly; |
| internal bool isStatic; |
| internal Type propertyType; |
|
|
| private bool? _isHidden; |
|
|
| internal override bool IsHidden |
| { |
| get |
| { |
| _isHidden ??= member.GetCustomAttributes(typeof(HiddenAttribute), inherit: false).Length != 0; |
|
|
| return _isHidden.Value; |
| } |
| } |
|
|
| private AttributeCollection _attributes; |
|
|
| internal AttributeCollection Attributes |
| { |
| get |
| { |
| if (_attributes == null) |
| { |
| |
| var objAttributes = this.member.GetCustomAttributes(true); |
| _attributes = new AttributeCollection(objAttributes.Cast<Attribute>().ToArray()); |
| } |
|
|
| return _attributes; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| private static bool SameSignature(MethodBase method1, MethodBase method2) |
| { |
| if (method1.GetGenericArguments().Length != method2.GetGenericArguments().Length) |
| { |
| return false; |
| } |
|
|
| ParameterInfo[] parameters1 = method1.GetParameters(); |
| ParameterInfo[] parameters2 = method2.GetParameters(); |
| if (parameters1.Length != parameters2.Length) |
| { |
| return false; |
| } |
|
|
| for (int i = 0; i < parameters1.Length; ++i) |
| { |
| if (parameters1[i].ParameterType != parameters2[i].ParameterType |
| || parameters1[i].IsOut != parameters2[i].IsOut |
| || parameters1[i].IsOptional != parameters2[i].IsOptional) |
| { |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| private static void AddOverload(List<MethodBase> previousMethodEntry, MethodInfo method) |
| { |
| bool add = true; |
|
|
| for (int i = 0; i < previousMethodEntry.Count; i++) |
| { |
| if (SameSignature(previousMethodEntry[i], method)) |
| { |
| add = false; |
| break; |
| } |
| } |
|
|
| if (add) |
| { |
| previousMethodEntry.Add(method); |
| } |
| } |
|
|
| private static void PopulateMethodReflectionTable(Type type, MethodInfo[] methods, CacheTable typeMethods) |
| { |
| foreach (MethodInfo method in methods) |
| { |
| if (method.DeclaringType == type) |
| { |
| string methodName = method.Name; |
| var previousMethodEntry = (List<MethodBase>)typeMethods[methodName]; |
| if (previousMethodEntry == null) |
| { |
| var methodEntry = new List<MethodBase> { method }; |
| typeMethods.Add(methodName, methodEntry); |
| } |
| else |
| { |
| AddOverload(previousMethodEntry, method); |
| } |
| } |
| } |
|
|
| if (type.BaseType != null) |
| { |
| PopulateMethodReflectionTable(type.BaseType, methods, typeMethods); |
| } |
| } |
|
|
| private static void PopulateMethodReflectionTable(ConstructorInfo[] ctors, CacheTable typeMethods) |
| { |
| foreach (ConstructorInfo ctor in ctors) |
| { |
| var previousMethodEntry = (List<MethodBase>)typeMethods["new"]; |
| if (previousMethodEntry == null) |
| { |
| var methodEntry = new List<MethodBase>(); |
| methodEntry.Add(ctor); |
| typeMethods.Add("new", methodEntry); |
| } |
| else |
| { |
| previousMethodEntry.Add(ctor); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private static void PopulateMethodReflectionTable(Type type, CacheTable typeMethods, BindingFlags bindingFlags) |
| { |
| bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); |
|
|
| MethodInfo[] methods = type.GetMethods(bindingFlags); |
| PopulateMethodReflectionTable(type, methods, typeMethods); |
|
|
| Type[] interfaces = type.GetInterfaces(); |
| foreach (Type interfaceType in interfaces) |
| { |
| if (!TypeResolver.IsPublic(interfaceType)) |
| { |
| continue; |
| } |
|
|
| if (interfaceType.IsGenericType && type.IsArray) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| continue; |
| } |
|
|
| methods = interfaceType.GetMethods(bindingFlags); |
|
|
| foreach (MethodInfo interfaceMethod in methods) |
| { |
| if (isStatic && interfaceMethod.IsVirtual) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| continue; |
| } |
|
|
| var previousMethodEntry = (List<MethodBase>)typeMethods[interfaceMethod.Name]; |
| if (previousMethodEntry == null) |
| { |
| var methodEntry = new List<MethodBase> { interfaceMethod }; |
| typeMethods.Add(interfaceMethod.Name, methodEntry); |
| } |
| else |
| { |
| if (!previousMethodEntry.Contains(interfaceMethod)) |
| { |
| previousMethodEntry.Add(interfaceMethod); |
| } |
| } |
| } |
| } |
|
|
| if ((bindingFlags & BindingFlags.Static) != 0 && TypeResolver.IsPublic(type)) |
| { |
| |
| |
| var previousMethodEntry = (List<MethodBase>)typeMethods["new"]; |
| if (previousMethodEntry == null) |
| { |
| var ctorBindingFlags = bindingFlags & ~(BindingFlags.FlattenHierarchy | BindingFlags.Static); |
| ctorBindingFlags |= BindingFlags.Instance; |
| var ctorInfos = type.GetConstructors(ctorBindingFlags); |
| PopulateMethodReflectionTable(ctorInfos, typeMethods); |
| } |
| } |
|
|
| for (int i = 0; i < typeMethods.memberCollection.Count; i++) |
| { |
| typeMethods.memberCollection[i] = |
| new MethodCacheEntry((List<MethodBase>)typeMethods.memberCollection[i]); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private static void PopulateEventReflectionTable(Type type, Dictionary<string, EventCacheEntry> typeEvents, BindingFlags bindingFlags) |
| { |
| EventInfo[] events = type.GetEvents(bindingFlags); |
| var tempTable = new Dictionary<string, List<EventInfo>>(StringComparer.OrdinalIgnoreCase); |
|
|
| foreach (EventInfo typeEvent in events) |
| { |
| string eventName = typeEvent.Name; |
| if (!tempTable.TryGetValue(eventName, out List<EventInfo> entryList)) |
| { |
| entryList = new List<EventInfo>(); |
| tempTable.Add(eventName, entryList); |
| } |
|
|
| entryList.Add(typeEvent); |
| } |
|
|
| foreach (KeyValuePair<string, List<EventInfo>> entry in tempTable) |
| { |
| typeEvents.Add(entry.Key, new EventCacheEntry(entry.Value.ToArray())); |
| } |
| } |
|
|
| |
| |
| |
| |
| private static bool PropertyAlreadyPresent(List<PropertyInfo> previousProperties, PropertyInfo property) |
| { |
| |
| bool returnValue = false; |
| ParameterInfo[] propertyParameters = property.GetIndexParameters(); |
| int propertyIndexLength = propertyParameters.Length; |
|
|
| foreach (PropertyInfo previousProperty in previousProperties) |
| { |
| ParameterInfo[] previousParameters = previousProperty.GetIndexParameters(); |
| if (previousParameters.Length == propertyIndexLength) |
| { |
| bool parametersAreSame = true; |
| for (int parameterIndex = 0; parameterIndex < previousParameters.Length; parameterIndex++) |
| { |
| ParameterInfo previousParameter = previousParameters[parameterIndex]; |
| ParameterInfo propertyParameter = propertyParameters[parameterIndex]; |
| if (previousParameter.ParameterType != propertyParameter.ParameterType) |
| { |
| parametersAreSame = false; |
| break; |
| } |
| } |
|
|
| if (parametersAreSame) |
| { |
| returnValue = true; |
| break; |
| } |
| } |
| } |
|
|
| return returnValue; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private static void PopulatePropertyReflectionTable(Type type, CacheTable typeProperties, BindingFlags bindingFlags) |
| { |
| bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); |
| var tempTable = new Dictionary<string, List<PropertyInfo>>(StringComparer.OrdinalIgnoreCase); |
|
|
| PropertyInfo[] properties = type.GetProperties(bindingFlags); |
| foreach (PropertyInfo property in properties) |
| { |
| PopulateSingleProperty(type, property, tempTable, property.Name); |
| } |
|
|
| Type[] interfaces = type.GetInterfaces(); |
| foreach (Type interfaceType in interfaces) |
| { |
| if (!TypeResolver.IsPublic(interfaceType)) |
| { |
| continue; |
| } |
|
|
| properties = interfaceType.GetProperties(bindingFlags); |
| foreach (PropertyInfo property in properties) |
| { |
| if (isStatic && |
| (property.GetMethod?.IsVirtual == true || property.SetMethod?.IsVirtual == true)) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| continue; |
| } |
|
|
| PopulateSingleProperty(type, property, tempTable, property.Name); |
| } |
| } |
|
|
| foreach (KeyValuePair<string, List<PropertyInfo>> entry in tempTable) |
| { |
| List<PropertyInfo> propertiesList = entry.Value; |
| PropertyInfo firstProperty = propertiesList[0]; |
| if ((propertiesList.Count > 1) || (firstProperty.GetIndexParameters().Length != 0)) |
| { |
| typeProperties.Add(entry.Key, new ParameterizedPropertyCacheEntry(propertiesList)); |
| } |
| else |
| { |
| typeProperties.Add(entry.Key, new PropertyCacheEntry(firstProperty)); |
| } |
| } |
|
|
| FieldInfo[] fields = type.GetFields(bindingFlags); |
| foreach (FieldInfo field in fields) |
| { |
| string fieldName = field.Name; |
| var previousMember = (PropertyCacheEntry)typeProperties[fieldName]; |
| if (previousMember == null) |
| { |
| typeProperties.Add(fieldName, new PropertyCacheEntry(field)); |
| } |
| else if (!string.Equals(previousMember.member.Name, fieldName)) |
| { |
| |
| |
| |
| |
| throw new ExtendedTypeSystemException( |
| "NotACLSComplaintField", |
| innerException: null, |
| ExtendedTypeSystem.NotAClsCompliantFieldProperty, |
| fieldName, |
| type.FullName, |
| previousMember.member.Name); |
| } |
| } |
| } |
|
|
| private static void PopulateSingleProperty(Type type, PropertyInfo property, Dictionary<string, List<PropertyInfo>> tempTable, string propertyName) |
| { |
| List<PropertyInfo> previousPropertyEntry; |
| if (!tempTable.TryGetValue(propertyName, out previousPropertyEntry)) |
| { |
| previousPropertyEntry = new List<PropertyInfo> { property }; |
| tempTable.Add(propertyName, previousPropertyEntry); |
| } |
| else |
| { |
| var firstProperty = previousPropertyEntry[0]; |
| if (!string.Equals(property.Name, firstProperty.Name, StringComparison.Ordinal)) |
| { |
| throw new ExtendedTypeSystemException("NotACLSComplaintProperty", null, |
| ExtendedTypeSystem.NotAClsCompliantFieldProperty, property.Name, type.FullName, firstProperty.Name); |
| } |
|
|
| if (PropertyAlreadyPresent(previousPropertyEntry, property)) |
| { |
| return; |
| } |
|
|
| previousPropertyEntry.Add(property); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private static CacheTable GetStaticPropertyReflectionTable(Type type) |
| { |
| lock (s_staticPropertyCacheTable) |
| { |
| CacheTable typeTable = null; |
| if (s_staticPropertyCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new CacheTable(); |
| PopulatePropertyReflectionTable(type, typeTable, staticBindingFlags); |
| s_staticPropertyCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| private static CacheTable GetStaticMethodReflectionTable(Type type) |
| { |
| lock (s_staticMethodCacheTable) |
| { |
| CacheTable typeTable = null; |
| if (s_staticMethodCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new CacheTable(); |
| PopulateMethodReflectionTable(type, typeTable, staticBindingFlags); |
| s_staticMethodCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| private static Dictionary<string, EventCacheEntry> GetStaticEventReflectionTable(Type type) |
| { |
| lock (s_staticEventCacheTable) |
| { |
| Dictionary<string, EventCacheEntry> typeTable; |
| if (s_staticEventCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new Dictionary<string, EventCacheEntry>(); |
| PopulateEventReflectionTable(type, typeTable, staticBindingFlags); |
| s_staticEventCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private static CacheTable GetInstancePropertyReflectionTable(Type type) |
| { |
| lock (s_instancePropertyCacheTable) |
| { |
| CacheTable typeTable = null; |
| if (s_instancePropertyCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new CacheTable(); |
| PopulatePropertyReflectionTable(type, typeTable, instanceBindingFlags); |
| s_instancePropertyCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| private static CacheTable GetInstanceMethodReflectionTable(Type type) |
| { |
| lock (s_instanceMethodCacheTable) |
| { |
| CacheTable typeTable = null; |
| if (s_instanceMethodCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new CacheTable(); |
| PopulateMethodReflectionTable(type, typeTable, instanceBindingFlags); |
| s_instanceMethodCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| internal IEnumerable<object> GetPropertiesAndMethods(Type type, bool @static) |
| { |
| CacheTable propertyTable = @static |
| ? GetStaticPropertyReflectionTable(type) |
| : GetInstancePropertyReflectionTable(type); |
| for (int i = 0; i < propertyTable.memberCollection.Count; i++) |
| { |
| var propertyCacheEntry = propertyTable.memberCollection[i] as PropertyCacheEntry; |
| if (propertyCacheEntry != null) |
| yield return propertyCacheEntry.member; |
| } |
|
|
| CacheTable methodTable = @static |
| ? GetStaticMethodReflectionTable(type) |
| : GetInstanceMethodReflectionTable(type); |
| for (int i = 0; i < methodTable.memberCollection.Count; i++) |
| { |
| var method = methodTable.memberCollection[i] as MethodCacheEntry; |
| if (method != null && !method[0].method.IsSpecialName) |
| { |
| yield return method; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| private static Dictionary<string, EventCacheEntry> GetInstanceEventReflectionTable(Type type) |
| { |
| lock (s_instanceEventCacheTable) |
| { |
| Dictionary<string, EventCacheEntry> typeTable; |
| if (s_instanceEventCacheTable.TryGetValue(type, out typeTable)) |
| { |
| return typeTable; |
| } |
|
|
| typeTable = new Dictionary<string, EventCacheEntry>(StringComparer.OrdinalIgnoreCase); |
| PopulateEventReflectionTable(type, typeTable, instanceBindingFlags); |
| s_instanceEventCacheTable[type] = typeTable; |
| return typeTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static bool IsTypeParameterizedProperty(Type t) |
| { |
| return t == typeof(PSMemberInfo) || t == typeof(PSParameterizedProperty); |
| } |
|
|
| private T GetDotNetPropertyImpl<T>(object obj, string propertyName, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| bool lookingForProperties = typeof(T).IsAssignableFrom(typeof(PSProperty)); |
| bool lookingForParameterizedProperties = IsTypeParameterizedProperty(typeof(T)); |
| if (!lookingForProperties && !lookingForParameterizedProperties) |
| { |
| return null; |
| } |
|
|
| CacheTable typeTable = _isStatic |
| ? GetStaticPropertyReflectionTable((Type)obj) |
| : GetInstancePropertyReflectionTable(obj.GetType()); |
|
|
| object entry = predicate != null |
| ? typeTable.GetFirstOrDefault(predicate) |
| : typeTable[propertyName]; |
| switch (entry) |
| { |
| case null: |
| return null; |
| case PropertyCacheEntry cacheEntry when lookingForProperties: |
| return new PSProperty(cacheEntry.member.Name, this, obj, cacheEntry) { IsHidden = cacheEntry.IsHidden } as T; |
| case ParameterizedPropertyCacheEntry paramCacheEntry when lookingForParameterizedProperties: |
|
|
| |
| |
| |
| return new PSParameterizedProperty(paramCacheEntry.propertyName, this, obj, paramCacheEntry) as T; |
| default: return null; |
| } |
| } |
|
|
| private T GetDotNetMethodImpl<T>(object obj, string methodName, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| if (!typeof(T).IsAssignableFrom(typeof(PSMethod))) |
| { |
| return null; |
| } |
|
|
| CacheTable typeTable = _isStatic |
| ? GetStaticMethodReflectionTable((Type)obj) |
| : GetInstanceMethodReflectionTable(obj.GetType()); |
|
|
| var methods = predicate != null |
| ? (MethodCacheEntry)typeTable.GetFirstOrDefault(predicate) |
| : (MethodCacheEntry)typeTable[methodName]; |
|
|
| if (methods == null) |
| { |
| return null; |
| } |
|
|
| var isCtor = methods[0].method is ConstructorInfo; |
| bool isSpecial = !isCtor && methods[0].method.IsSpecialName; |
|
|
| return PSMethod.Create(methods[0].method.Name, this, obj, methods, isSpecial, methods.IsHidden) as T; |
| } |
|
|
| internal T GetDotNetProperty<T>(object obj, string propertyName) where T : PSMemberInfo |
| { |
| return GetDotNetPropertyImpl<T>(obj, propertyName, predicate: null); |
| } |
|
|
| internal T GetDotNetMethod<T>(object obj, string methodName) where T : PSMemberInfo |
| { |
| return GetDotNetMethodImpl<T>(obj, methodName, predicate: null); |
| } |
|
|
| protected T GetFirstDotNetPropertyOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| return GetDotNetPropertyImpl<T>(obj, propertyName: null, predicate); |
| } |
|
|
| protected T GetFirstDotNetMethodOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| return GetDotNetMethodImpl<T>(obj, methodName: null, predicate); |
| } |
|
|
| protected T GetFirstDotNetEventOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| if (!typeof(T).IsAssignableFrom(typeof(PSEvent))) |
| { |
| return null; |
| } |
|
|
| var table = _isStatic |
| ? GetStaticEventReflectionTable((Type)obj) |
| : GetInstanceEventReflectionTable(obj.GetType()); |
|
|
| foreach (var psEvent in table.Values) |
| { |
| if (predicate(psEvent.events[0].Name)) |
| { |
| return new PSEvent(psEvent.events[0]) as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| protected T GetFirstDynamicMemberOrDefault<T>(object obj, MemberNamePredicate predicate) where T : PSMemberInfo |
| { |
| var idmop = obj as IDynamicMetaObjectProvider; |
| if (idmop == null || obj is PSObject) |
| { |
| return null; |
| } |
|
|
| if (!typeof(T).IsAssignableFrom(typeof(PSDynamicMember))) |
| { |
| return null; |
| } |
|
|
| foreach (var name in idmop.GetMetaObject(Expression.Variable(idmop.GetType())).GetDynamicMemberNames()) |
| { |
| if (predicate(name)) |
| { |
| return new PSDynamicMember(name) as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| internal void AddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members, bool ignoreDuplicates) where T : PSMemberInfo |
| { |
| bool lookingForProperties = typeof(T).IsAssignableFrom(typeof(PSProperty)); |
| bool lookingForParameterizedProperties = IsTypeParameterizedProperty(typeof(T)); |
| if (!lookingForProperties && !lookingForParameterizedProperties) |
| { |
| return; |
| } |
|
|
| CacheTable table = _isStatic |
| ? GetStaticPropertyReflectionTable((Type)obj) |
| : GetInstancePropertyReflectionTable(obj.GetType()); |
|
|
| for (int i = 0; i < table.memberCollection.Count; i++) |
| { |
| if (table.memberCollection[i] is PropertyCacheEntry propertyEntry) |
| { |
| if (lookingForProperties) |
| { |
| if (!ignoreDuplicates || (members[propertyEntry.member.Name] == null)) |
| { |
| members.Add( |
| new PSProperty( |
| name: propertyEntry.member.Name, |
| adapter: this, |
| baseObject: obj, |
| adapterData: propertyEntry) |
| { IsHidden = propertyEntry.IsHidden } as T); |
| } |
| } |
| } |
| else if (lookingForParameterizedProperties) |
| { |
| var parameterizedPropertyEntry = (ParameterizedPropertyCacheEntry)table.memberCollection[i]; |
| if (!ignoreDuplicates || (members[parameterizedPropertyEntry.propertyName] == null)) |
| { |
| |
| |
| |
| members.Add(new PSParameterizedProperty(parameterizedPropertyEntry.propertyName, |
| this, obj, parameterizedPropertyEntry) as T); |
| } |
| } |
| } |
| } |
|
|
| internal void AddAllMethods<T>(object obj, PSMemberInfoInternalCollection<T> members, bool ignoreDuplicates) where T : PSMemberInfo |
| { |
| if (!typeof(T).IsAssignableFrom(typeof(PSMethod))) |
| { |
| return; |
| } |
|
|
| CacheTable table = _isStatic |
| ? GetStaticMethodReflectionTable((Type)obj) |
| : GetInstanceMethodReflectionTable(obj.GetType()); |
|
|
| for (int i = 0; i < table.memberCollection.Count; i++) |
| { |
| var method = (MethodCacheEntry)table.memberCollection[i]; |
| var isCtor = method[0].method is ConstructorInfo; |
| var name = isCtor ? "new" : method[0].method.Name; |
|
|
| if (!ignoreDuplicates || (members[name] == null)) |
| { |
| bool isSpecial = !isCtor && method[0].method.IsSpecialName; |
| members.Add(PSMethod.Create(name, this, obj, method, isSpecial, method.IsHidden) as T); |
| } |
| } |
| } |
|
|
| internal void AddAllEvents<T>(object obj, PSMemberInfoInternalCollection<T> members, bool ignoreDuplicates) where T : PSMemberInfo |
| { |
| if (!typeof(T).IsAssignableFrom(typeof(PSEvent))) |
| { |
| return; |
| } |
|
|
| var table = _isStatic |
| ? GetStaticEventReflectionTable((Type)obj) |
| : GetInstanceEventReflectionTable(obj.GetType()); |
|
|
| foreach (var psEvent in table.Values) |
| { |
| if (!ignoreDuplicates || (members[psEvent.events[0].Name] == null)) |
| { |
| members.Add(new PSEvent(psEvent.events[0]) as T); |
| } |
| } |
| } |
|
|
| internal void AddAllDynamicMembers<T>(object obj, PSMemberInfoInternalCollection<T> members, bool ignoreDuplicates) where T : PSMemberInfo |
| { |
| var idmop = obj as IDynamicMetaObjectProvider; |
| if (idmop == null || obj is PSObject) |
| { |
| return; |
| } |
|
|
| if (!typeof(T).IsAssignableFrom(typeof(PSDynamicMember))) |
| { |
| return; |
| } |
|
|
| foreach (var name in idmop.GetMetaObject(Expression.Variable(idmop.GetType())).GetDynamicMemberNames()) |
| { |
| members.Add(new PSDynamicMember(name) as T); |
| } |
| } |
|
|
| private static bool PropertyIsStatic(PSProperty property) |
| { |
| if (property.adapterData is not PropertyCacheEntry entry) |
| { |
| return false; |
| } |
|
|
| return entry.isStatic; |
| } |
|
|
| |
| |
| |
| |
| |
| private static string GetDefaultValueStringRepresentation(ParameterInfo parameterInfo) |
| { |
| var parameterType = parameterInfo.ParameterType; |
| var parameterDefaultValue = parameterInfo.DefaultValue; |
|
|
| if (parameterDefaultValue == null) |
| { |
| return (parameterType.IsValueType || parameterType.IsGenericMethodParameter) |
| ? "default" |
| : "null"; |
| } |
|
|
| if (parameterType.IsEnum) |
| { |
| return string.Create(CultureInfo.InvariantCulture, $"{parameterType}.{parameterDefaultValue}"); |
| } |
|
|
| return (parameterDefaultValue is string) |
| ? string.Create(CultureInfo.InvariantCulture, $"\"{parameterDefaultValue}\"") |
| : parameterDefaultValue.ToString(); |
| } |
|
|
| #endregion auxiliary methods and classes |
|
|
| #region virtual |
|
|
| #region member |
|
|
| internal override bool CanSiteBinderOptimize(MemberTypes typeToOperateOn) { return true; } |
|
|
| private static readonly ConcurrentDictionary<Type, ConsolidatedString> s_typeToTypeNameDictionary = |
| new ConcurrentDictionary<Type, ConsolidatedString>(); |
|
|
| internal static ConsolidatedString GetInternedTypeNameHierarchy(Type type) |
| { |
| return s_typeToTypeNameDictionary.GetOrAdd(type, |
| t => new ConsolidatedString(GetDotNetTypeNameHierarchy(t), interned: true)); |
| } |
|
|
| protected override ConsolidatedString GetInternedTypeNameHierarchy(object obj) |
| { |
| return GetInternedTypeNameHierarchy(obj.GetType()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override T GetMember<T>(object obj, string memberName) |
| { |
| T returnValue = GetDotNetProperty<T>(obj, memberName); |
| if (returnValue != null) return returnValue; |
| return GetDotNetMethod<T>(obj, memberName); |
| } |
|
|
| |
| |
| |
| protected override T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) |
| { |
| return GetFirstDotNetPropertyOrDefault<T>(obj, predicate) ?? GetFirstDotNetMethodOrDefault<T>(obj, predicate) ?? |
| GetFirstDotNetEventOrDefault<T>(obj, predicate) ?? GetFirstDynamicMemberOrDefault<T>(obj, predicate); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) |
| { |
| PSMemberInfoInternalCollection<T> returnValue = new PSMemberInfoInternalCollection<T>(); |
| AddAllProperties<T>(obj, returnValue, false); |
| AddAllMethods<T>(obj, returnValue, false); |
| AddAllEvents<T>(obj, returnValue, false); |
| AddAllDynamicMembers(obj, returnValue, false); |
|
|
| return returnValue; |
| } |
|
|
| #endregion member |
|
|
| #region property |
|
|
| |
| |
| |
| |
| |
| protected override AttributeCollection PropertyAttributes(PSProperty property) |
| { |
| PropertyCacheEntry adapterData = (PropertyCacheEntry)property.adapterData; |
| return adapterData.Attributes; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override string PropertyToString(PSProperty property) |
| { |
| StringBuilder returnValue = new StringBuilder(); |
| if (PropertyIsStatic(property)) |
| { |
| returnValue.Append("static "); |
| } |
|
|
| returnValue.Append(PropertyType(property, forDisplay: true)); |
| returnValue.Append(' '); |
| returnValue.Append(property.Name); |
| returnValue.Append(" {"); |
| if (PropertyIsGettable(property)) |
| { |
| returnValue.Append("get;"); |
| } |
|
|
| if (PropertyIsSettable(property)) |
| { |
| returnValue.Append("set;"); |
| } |
|
|
| returnValue.Append('}'); |
| return returnValue.ToString(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override object PropertyGet(PSProperty property) |
| { |
| PropertyCacheEntry adapterData = (PropertyCacheEntry)property.adapterData; |
|
|
| if (adapterData.propertyType.IsByRefLike) |
| { |
| throw new GetValueException( |
| nameof(ExtendedTypeSystem.CannotAccessByRefLikePropertyOrField), |
| innerException: null, |
| ExtendedTypeSystem.CannotAccessByRefLikePropertyOrField, |
| adapterData.member.Name, |
| adapterData.propertyType); |
| } |
|
|
| PropertyInfo propertyInfo = adapterData.member as PropertyInfo; |
| if (propertyInfo != null) |
| { |
| if (adapterData.writeOnly) |
| { |
| throw new GetValueException( |
| nameof(ExtendedTypeSystem.WriteOnlyProperty), |
| innerException: null, |
| ExtendedTypeSystem.WriteOnlyProperty, |
| propertyInfo.Name); |
| } |
|
|
| if (adapterData.useReflection) |
| { |
| return propertyInfo.GetValue(property.baseObject, null); |
| } |
| else |
| { |
| return adapterData.getterDelegate(property.baseObject); |
| } |
| } |
|
|
| FieldInfo field = adapterData.member as FieldInfo; |
| if (adapterData.useReflection) |
| { |
| return field?.GetValue(property.baseObject); |
| } |
| else |
| { |
| return adapterData.getterDelegate(property.baseObject); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) |
| { |
| PropertyCacheEntry adapterData = (PropertyCacheEntry)property.adapterData; |
|
|
| if (adapterData.readOnly) |
| { |
| throw new SetValueException( |
| nameof(ExtendedTypeSystem.ReadOnlyProperty), |
| innerException: null, |
| ExtendedTypeSystem.ReadOnlyProperty, |
| adapterData.member.Name); |
| } |
|
|
| if (adapterData.propertyType.IsByRefLike) |
| { |
| throw new SetValueException( |
| nameof(ExtendedTypeSystem.CannotAccessByRefLikePropertyOrField), |
| innerException: null, |
| ExtendedTypeSystem.CannotAccessByRefLikePropertyOrField, |
| adapterData.member.Name, |
| adapterData.propertyType); |
| } |
|
|
| PropertyInfo propertyInfo = adapterData.member as PropertyInfo; |
| if (propertyInfo != null) |
| { |
| if (convertIfPossible) |
| { |
| setValue = PropertySetAndMethodArgumentConvertTo(setValue, propertyInfo.PropertyType, CultureInfo.InvariantCulture); |
| } |
|
|
| if (adapterData.useReflection) |
| { |
| propertyInfo.SetValue(property.baseObject, setValue, null); |
| } |
| else |
| { |
| adapterData.setterDelegate(property.baseObject, setValue); |
| } |
|
|
| return; |
| } |
|
|
| FieldInfo field = adapterData.member as FieldInfo; |
| if (convertIfPossible) |
| { |
| setValue = PropertySetAndMethodArgumentConvertTo(setValue, field.FieldType, CultureInfo.InvariantCulture); |
| } |
|
|
| if (adapterData.useReflection) |
| { |
| field.SetValue(property.baseObject, setValue); |
| } |
| else |
| { |
| adapterData.setterDelegate(property.baseObject, setValue); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsSettable(PSProperty property) |
| { |
| return !((PropertyCacheEntry)property.adapterData).readOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsGettable(PSProperty property) |
| { |
| return !((PropertyCacheEntry)property.adapterData).writeOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override string PropertyType(PSProperty property, bool forDisplay) |
| { |
| var propertyType = ((PropertyCacheEntry)property.adapterData).propertyType; |
| return forDisplay ? ToStringCodeMethods.Type(propertyType) : propertyType.FullName; |
| } |
|
|
| #endregion property |
|
|
| #region method |
|
|
| #region auxiliary to method calling |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| internal static object AuxiliaryConstructorInvoke(MethodInformation methodInformation, object[] arguments, object[] originalArguments) |
| { |
| object returnValue; |
| #pragma warning disable 56500 |
| try |
| { |
| returnValue = methodInformation.Invoke(target: null, arguments); |
| } |
| catch (TargetInvocationException ex) |
| { |
| Exception inner = ex.InnerException ?? ex; |
| throw new MethodInvocationException( |
| "DotNetconstructorTargetInvocation", |
| inner, |
| ExtendedTypeSystem.MethodInvocationException, |
| ".ctor", arguments.Length, inner.Message); |
| } |
| catch (Exception e) |
| { |
| throw new MethodInvocationException( |
| "DotNetconstructorException", |
| e, |
| ExtendedTypeSystem.MethodInvocationException, |
| ".ctor", arguments.Length, e.Message); |
| } |
|
|
| SetReferences(arguments, methodInformation, originalArguments); |
| return returnValue; |
| #pragma warning restore 56500 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static object AuxiliaryMethodInvoke(object target, object[] arguments, MethodInformation methodInformation, object[] originalArguments) |
| { |
| object result; |
|
|
| #pragma warning disable 56500 |
| try |
| { |
| |
| |
| result = methodInformation.Invoke(target, arguments); |
| } |
| catch (TargetInvocationException ex) |
| { |
| |
| |
| if (ex.InnerException is FlowControlException || ex.InnerException is ScriptCallDepthException) |
| throw ex.InnerException; |
| |
| |
| if (ex.InnerException is ParameterBindingException) |
| throw ex.InnerException; |
|
|
| Exception inner = ex.InnerException ?? ex; |
|
|
| throw new MethodInvocationException( |
| "DotNetMethodTargetInvocation", |
| inner, |
| ExtendedTypeSystem.MethodInvocationException, |
| methodInformation.method.Name, arguments.Length, inner.Message); |
| } |
| |
| |
| |
| |
| catch (ParameterBindingException) { throw; } |
| catch (FlowControlException) { throw; } |
| catch (ScriptCallDepthException) { throw; } |
| catch (PipelineStoppedException) { throw; } |
| catch (Exception e) |
| { |
| if (methodInformation.method.DeclaringType == typeof(SteppablePipeline) && |
| (methodInformation.method.Name.Equals("Begin") || |
| methodInformation.method.Name.Equals("Process") || |
| methodInformation.method.Name.Equals("End"))) |
| { |
| |
| |
| throw; |
| } |
|
|
| throw new MethodInvocationException( |
| "DotNetMethodException", |
| e, |
| ExtendedTypeSystem.MethodInvocationException, |
| methodInformation.method.Name, arguments.Length, e.Message); |
| } |
| #pragma warning restore 56500 |
|
|
| SetReferences(arguments, methodInformation, originalArguments); |
| MethodInfo methodInfo = methodInformation.method as MethodInfo; |
| if (methodInfo != null && methodInfo.ReturnType != typeof(void)) |
| return result; |
| return AutomationNull.Value; |
| } |
|
|
| |
| |
| |
| |
| |
| internal static MethodInformation[] GetMethodInformationArray(IList<MethodBase> methods) |
| { |
| var returnValue = new MethodInformation[methods.Count]; |
| for (int i = 0; i < methods.Count; i++) |
| { |
| returnValue[i] = new MethodInformation(methods[i], 0); |
| } |
|
|
| return returnValue; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static object MethodInvokeDotNet( |
| string methodName, |
| object target, |
| MethodInformation[] methodInformation, |
| PSMethodInvocationConstraints invocationConstraints, |
| object[] arguments) |
| { |
| object[] newArguments; |
| MethodInformation bestMethod = GetBestMethodAndArguments(methodName, methodInformation, invocationConstraints, arguments, out newArguments); |
| if (bestMethod.method is ConstructorInfo) |
| { |
| return InvokeResolvedConstructor(bestMethod, newArguments, arguments); |
| } |
|
|
| string methodDefinition = bestMethod.methodDefinition; |
| ScriptTrace.Trace(1, "TraceMethodCall", ParserStrings.TraceMethodCall, methodDefinition); |
| PSObject.MemberResolution.WriteLine("Calling Method: {0}", methodDefinition); |
| return AuxiliaryMethodInvoke(target, newArguments, bestMethod, arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static object ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, object[] arguments) |
| { |
| var newConstructors = GetMethodInformationArray(constructors); |
| object[] newArguments; |
| MethodInformation bestMethod = GetBestMethodAndArguments(type.Name, newConstructors, arguments, out newArguments); |
| return InvokeResolvedConstructor(bestMethod, newArguments, arguments); |
| } |
|
|
| private static object InvokeResolvedConstructor(MethodInformation bestMethod, object[] newArguments, object[] arguments) |
| { |
| if ((PSObject.MemberResolution.Options & PSTraceSourceOptions.WriteLine) != 0) |
| { |
| PSObject.MemberResolution.WriteLine("Calling Constructor: {0}", DotNetAdapter.GetMethodInfoOverloadDefinition(null, |
| bestMethod.method, 0)); |
| } |
|
|
| return AuxiliaryConstructorInvoke(bestMethod, newArguments, arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static void ParameterizedPropertyInvokeSet(string propertyName, object target, object valuetoSet, MethodInformation[] methodInformation, object[] arguments) |
| { |
| |
| |
| |
| object[] newArguments; |
| MethodInformation bestMethod = GetBestMethodAndArguments(propertyName, methodInformation, arguments, out newArguments); |
| PSObject.MemberResolution.WriteLine("Calling Set Method: {0}", bestMethod.methodDefinition); |
| ParameterInfo[] bestMethodParameters = bestMethod.method.GetParameters(); |
| Type propertyType = bestMethodParameters[bestMethodParameters.Length - 1].ParameterType; |
|
|
| |
| |
| object lastArgument; |
| try |
| { |
| lastArgument = PropertySetAndMethodArgumentConvertTo(valuetoSet, propertyType, CultureInfo.InvariantCulture); |
| } |
| catch (InvalidCastException e) |
| { |
| |
| throw new MethodException( |
| "PropertySetterConversionInvalidCastArgument", |
| e, |
| ExtendedTypeSystem.MethodArgumentConversionException, |
| arguments.Length - 1, valuetoSet, propertyName, propertyType, e.Message); |
| } |
|
|
| |
| object[] finalArguments = new object[newArguments.Length + 1]; |
| for (int i = 0; i < newArguments.Length; i++) |
| { |
| finalArguments[i] = newArguments[i]; |
| } |
|
|
| finalArguments[newArguments.Length] = lastArgument; |
|
|
| AuxiliaryMethodInvoke(target, finalArguments, bestMethod, arguments); |
| } |
|
|
| internal static string GetMethodInfoOverloadDefinition(string memberName, MethodBase methodEntry, int parametersToIgnore) |
| { |
| StringBuilder builder = new StringBuilder(); |
| if (methodEntry.IsStatic) |
| { |
| builder.Append("static "); |
| } |
|
|
| MethodInfo method = methodEntry as MethodInfo; |
| if (method != null) |
| { |
| builder.Append(ToStringCodeMethods.Type(method.ReturnType)); |
| builder.Append(' '); |
| } |
| else |
| { |
| ConstructorInfo ctorInfo = methodEntry as ConstructorInfo; |
| if (ctorInfo != null) |
| { |
| builder.Append(ToStringCodeMethods.Type(ctorInfo.DeclaringType)); |
| builder.Append(' '); |
| } |
| } |
|
|
| if (methodEntry.DeclaringType.IsInterface) |
| { |
| builder.Append(ToStringCodeMethods.Type(methodEntry.DeclaringType, dropNamespaces: true)); |
| builder.Append('.'); |
| } |
|
|
| builder.Append(memberName ?? methodEntry.Name); |
| if (methodEntry.IsGenericMethodDefinition || methodEntry.IsGenericMethod) |
| { |
| builder.Append('['); |
|
|
| Type[] genericArgs = methodEntry.GetGenericArguments(); |
| for (int i = 0; i < genericArgs.Length; i++) |
| { |
| if (i > 0) { builder.Append(", "); } |
|
|
| builder.Append(ToStringCodeMethods.Type(genericArgs[i])); |
| } |
|
|
| builder.Append(']'); |
| } |
|
|
| builder.Append('('); |
| System.Reflection.ParameterInfo[] parameters = methodEntry.GetParameters(); |
| int parametersLength = parameters.Length - parametersToIgnore; |
| if (parametersLength > 0) |
| { |
| for (int i = 0; i < parametersLength; i++) |
| { |
| System.Reflection.ParameterInfo parameter = parameters[i]; |
| var parameterType = parameter.ParameterType; |
| if (parameterType.IsByRef) |
| { |
| builder.Append("[ref] "); |
| parameterType = parameterType.GetElementType(); |
| } |
|
|
| if (parameterType.IsArray && (i == parametersLength - 1)) |
| { |
| |
| |
| |
| |
| |
| |
| var paramArrayAttrs = parameter.GetCustomAttributes(typeof(ParamArrayAttribute), false); |
| if (paramArrayAttrs != null && paramArrayAttrs.Length > 0) |
| builder.Append("Params "); |
| } |
|
|
| builder.Append(ToStringCodeMethods.Type(parameterType)); |
| builder.Append(' '); |
| builder.Append(parameter.Name); |
|
|
| if (parameter.HasDefaultValue) |
| { |
| builder.Append(" = "); |
| builder.Append(GetDefaultValueStringRepresentation(parameter)); |
| } |
|
|
| builder.Append(", "); |
| } |
|
|
| builder.Remove(builder.Length - 2, 2); |
| } |
|
|
| builder.Append(')'); |
|
|
| return builder.ToString(); |
| } |
|
|
| #endregion auxiliary to method calling |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override object MethodInvoke(PSMethod method, object[] arguments) |
| { |
| return this.MethodInvoke(method, null, arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| protected override object MethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, object[] arguments) |
| { |
| MethodCacheEntry methodEntry = (MethodCacheEntry)method.adapterData; |
| return DotNetAdapter.MethodInvokeDotNet( |
| method.Name, |
| method.baseObject, |
| methodEntry.methodInformationStructures, |
| invocationConstraints, |
| arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override Collection<string> MethodDefinitions(PSMethod method) |
| { |
| MethodCacheEntry methodEntry = (MethodCacheEntry)method.adapterData; |
| IList<string> uniqueValues = methodEntry |
| .methodInformationStructures |
| .Select(static m => m.methodDefinition) |
| .Distinct(StringComparer.Ordinal) |
| .ToList(); |
| return new Collection<string>(uniqueValues); |
| } |
|
|
| #endregion method |
|
|
| #region parameterized property |
|
|
| |
| |
| |
| |
| |
| protected override string ParameterizedPropertyType(PSParameterizedProperty property) |
| { |
| var adapterData = (ParameterizedPropertyCacheEntry)property.adapterData; |
| return adapterData.propertyType.FullName; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool ParameterizedPropertyIsSettable(PSParameterizedProperty property) |
| { |
| return !((ParameterizedPropertyCacheEntry)property.adapterData).readOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool ParameterizedPropertyIsGettable(PSParameterizedProperty property) |
| { |
| return !((ParameterizedPropertyCacheEntry)property.adapterData).writeOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override object ParameterizedPropertyGet(PSParameterizedProperty property, object[] arguments) |
| { |
| var adapterData = (ParameterizedPropertyCacheEntry)property.adapterData; |
| return DotNetAdapter.MethodInvokeDotNet(property.Name, property.baseObject, |
| adapterData.getterInformation, null, arguments); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override void ParameterizedPropertySet(PSParameterizedProperty property, object setValue, object[] arguments) |
| { |
| var adapterData = (ParameterizedPropertyCacheEntry)property.adapterData; |
| ParameterizedPropertyInvokeSet(adapterData.propertyName, property.baseObject, setValue, |
| adapterData.setterInformation, arguments); |
| } |
|
|
| |
| |
| |
| protected override Collection<string> ParameterizedPropertyDefinitions(PSParameterizedProperty property) |
| { |
| var adapterData = (ParameterizedPropertyCacheEntry)property.adapterData; |
| var returnValue = new Collection<string>(); |
| for (int i = 0; i < adapterData.propertyDefinition.Length; i++) |
| { |
| returnValue.Add(adapterData.propertyDefinition[i]); |
| } |
|
|
| return returnValue; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override string ParameterizedPropertyToString(PSParameterizedProperty property) |
| { |
| StringBuilder returnValue = new StringBuilder(); |
| Collection<string> definitions = ParameterizedPropertyDefinitions(property); |
| for (int i = 0; i < definitions.Count; i++) |
| { |
| returnValue.Append(definitions[i]); |
| returnValue.Append(", "); |
| } |
|
|
| returnValue.Remove(returnValue.Length - 2, 2); |
| return returnValue.ToString(); |
| } |
|
|
| #endregion parameterized property |
|
|
| #endregion virtual |
| } |
|
|
| #region DotNetAdapterWithOnlyPropertyLookup |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal class BaseDotNetAdapterForAdaptedObjects : DotNetAdapter |
| { |
| |
| |
| |
| |
| protected override PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) |
| { |
| PSMemberInfoInternalCollection<T> returnValue = new PSMemberInfoInternalCollection<T>(); |
| AddAllProperties<T>(obj, returnValue, true); |
| AddAllMethods<T>(obj, returnValue, true); |
| AddAllEvents<T>(obj, returnValue, true); |
|
|
| return returnValue; |
| } |
|
|
| |
| |
| |
| protected override T GetMember<T>(object obj, string memberName) |
| { |
| PSProperty property = base.GetDotNetProperty<PSProperty>(obj, memberName); |
| if (typeof(T).IsAssignableFrom(typeof(PSProperty)) && (property != null)) |
| { |
| return property as T; |
| } |
|
|
| |
| |
| |
| |
| if (typeof(T) == typeof(PSMemberInfo)) |
| { |
| T returnValue = base.GetDotNetMethod<T>(obj, memberName); |
| |
| |
| if (returnValue != null && property == null) |
| { |
| return returnValue; |
| } |
| } |
|
|
| if (IsTypeParameterizedProperty(typeof(T))) |
| { |
| PSParameterizedProperty parameterizedProperty = base.GetDotNetProperty<PSParameterizedProperty>(obj, memberName); |
| |
| |
| if (parameterizedProperty != null && property == null) |
| { |
| return parameterizedProperty as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| protected override T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) |
| { |
| PSProperty property = base.GetFirstDotNetPropertyOrDefault<PSProperty>(obj, predicate); |
| if (typeof(T).IsAssignableFrom(typeof(PSProperty)) && property != null) |
| { |
| return property as T; |
| } |
|
|
| |
| |
| |
| |
| if (typeof(T) == typeof(PSMemberInfo)) |
| { |
| T returnValue = base.GetFirstDotNetMethodOrDefault<T>(obj, predicate); |
|
|
| |
| |
| if (returnValue != null && property == null) |
| { |
| return returnValue; |
| } |
| } |
|
|
| if (IsTypeParameterizedProperty(typeof(T))) |
| { |
| var parameterizedProperty = base.GetFirstDotNetPropertyOrDefault<PSParameterizedProperty>(obj, predicate); |
|
|
| |
| |
| if (parameterizedProperty != null && property == null) |
| { |
| return parameterizedProperty as T; |
| } |
| } |
|
|
| return null; |
| } |
| } |
|
|
| #endregion |
|
|
| #if !UNIX |
| |
| |
| |
| internal class DotNetAdapterWithComTypeName : DotNetAdapter |
| { |
| private readonly ComTypeInfo _comTypeInfo; |
|
|
| internal DotNetAdapterWithComTypeName(ComTypeInfo comTypeInfo) |
| { |
| _comTypeInfo = comTypeInfo; |
| } |
|
|
| protected override IEnumerable<string> GetTypeNameHierarchy(object obj) |
| { |
| for (Type type = obj.GetType(); type != null; type = type.BaseType) |
| { |
| if (type.FullName.Equals("System.__ComObject")) |
| { |
| yield return ComAdapter.GetComTypeName(_comTypeInfo.Clsid); |
| } |
|
|
| yield return type.FullName; |
| } |
| } |
|
|
| protected override ConsolidatedString GetInternedTypeNameHierarchy(object obj) |
| { |
| return new ConsolidatedString(GetTypeNameHierarchy(obj), interned: true); |
| } |
| } |
| #endif |
|
|
| |
| |
| |
| |
| internal abstract class MemberRedirectionAdapter : Adapter |
| { |
| #region virtual |
|
|
| #region property specific |
|
|
| |
| |
| |
| |
| |
| protected override AttributeCollection PropertyAttributes(PSProperty property) |
| { |
| return new AttributeCollection(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override object PropertyGet(PSProperty property) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsSettable(PSProperty property) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsGettable(PSProperty property) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override string PropertyType(PSProperty property, bool forDisplay) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override string PropertyToString(PSProperty property) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for properties"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| #endregion property specific |
|
|
| #region method specific |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override object MethodInvoke(PSMethod method, object[] arguments) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for methods"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override Collection<string> MethodDefinitions(PSMethod method) |
| { |
| Diagnostics.Assert(false, "redirection adapter is not called for methods"); |
| throw PSTraceSource.NewNotSupportedException(); |
| } |
|
|
| #endregion method specific |
|
|
| #endregion virtual |
| } |
| |
| |
| |
| internal class PSObjectAdapter : MemberRedirectionAdapter |
| { |
| #region virtual |
|
|
| |
| |
| |
| |
| protected override IEnumerable<string> GetTypeNameHierarchy(object obj) |
| { |
| return ((PSObject)obj).InternalTypeNames; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override T GetMember<T>(object obj, string memberName) |
| { |
| return ((PSObject)obj).Members[memberName] as T; |
| } |
|
|
| |
| |
| |
| |
| protected override T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) |
| { |
| return ((PSObject)obj).GetFirstPropertyOrDefault(predicate) as T; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) |
| { |
| var returnValue = new PSMemberInfoInternalCollection<T>(); |
| PSObject mshObj = (PSObject)obj; |
| foreach (PSMemberInfo member in mshObj.Members) |
| { |
| T memberAsT = member as T; |
| if (memberAsT != null) |
| { |
| returnValue.Add(memberAsT); |
| } |
| } |
|
|
| return returnValue; |
| } |
|
|
| #endregion virtual |
| } |
| |
| |
| |
| internal class PSMemberSetAdapter : MemberRedirectionAdapter |
| { |
| #region virtual |
|
|
| protected override IEnumerable<string> GetTypeNameHierarchy(object obj) |
| { |
| |
| |
| |
| |
| |
| yield return typeof(PSMemberSet).FullName; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override T GetMember<T>(object obj, string memberName) |
| { |
| return ((PSMemberSet)obj).Members[memberName] as T; |
| } |
|
|
| |
| |
| |
| |
| protected override T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) |
| { |
| foreach (var member in ((PSMemberSet)obj).Members) |
| { |
| if (predicate(member.Name)) |
| { |
| return member as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) |
| { |
| var returnValue = new PSMemberInfoInternalCollection<T>(); |
| foreach (PSMemberInfo member in ((PSMemberSet)obj).Members) |
| { |
| T memberAsT = member as T; |
| if (memberAsT != null) |
| { |
| returnValue.Add(memberAsT); |
| } |
| } |
|
|
| return returnValue; |
| } |
|
|
| #endregion virtual |
| } |
| |
| |
| |
| |
| internal abstract class PropertyOnlyAdapter : DotNetAdapter |
| { |
| |
| |
| |
| |
| |
| |
| internal override bool CanSiteBinderOptimize(MemberTypes typeToOperateOn) |
| { |
| switch (typeToOperateOn) |
| { |
| case MemberTypes.Property: |
| return false; |
| case MemberTypes.Method: |
| return true; |
| default: |
| throw new InvalidOperationException("Should be unreachable. Update code if other member types need to be handled here."); |
| } |
| } |
|
|
| protected override ConsolidatedString GetInternedTypeNameHierarchy(object obj) |
| { |
| return new ConsolidatedString(GetTypeNameHierarchy(obj), interned: true); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| protected abstract PSProperty DoGetProperty(object obj, string propertyName); |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected abstract PSProperty DoGetFirstPropertyOrDefault(object obj, MemberNamePredicate predicate); |
|
|
| |
| |
| |
| |
| |
| protected abstract void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members) where T : PSMemberInfo; |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected override T GetMember<T>(object obj, string memberName) |
| { |
| PSProperty property = DoGetProperty(obj, memberName); |
|
|
| if (typeof(T).IsAssignableFrom(typeof(PSProperty)) && property != null) |
| { |
| return property as T; |
| } |
|
|
| if (typeof(T).IsAssignableFrom(typeof(PSMethod))) |
| { |
| T returnValue = base.GetDotNetMethod<T>(obj, memberName); |
| |
| |
| if (returnValue != null && property == null) |
| { |
| return returnValue; |
| } |
| } |
|
|
| if (IsTypeParameterizedProperty(typeof(T))) |
| { |
| PSParameterizedProperty parameterizedProperty = base.GetDotNetProperty<PSParameterizedProperty>(obj, memberName); |
| |
| |
| if (parameterizedProperty != null && property == null) |
| { |
| return parameterizedProperty as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| protected override T GetFirstMemberOrDefault<T>(object obj, MemberNamePredicate predicate) |
| { |
| PSProperty property = DoGetFirstPropertyOrDefault(obj, predicate); |
|
|
| if (typeof(T).IsAssignableFrom(typeof(PSProperty))) |
| { |
| return property as T; |
| } |
|
|
| if (typeof(T).IsAssignableFrom(typeof(PSMethod))) |
| { |
| T returnValue = base.GetFirstDotNetMethodOrDefault<T>(obj, predicate); |
|
|
| |
| |
| if (returnValue != null && property == null) |
| { |
| return returnValue; |
| } |
| } |
|
|
| if (IsTypeParameterizedProperty(typeof(T))) |
| { |
| var parameterizedProperty = base.GetFirstDotNetPropertyOrDefault<PSParameterizedProperty>(obj, predicate); |
|
|
| |
| |
| if (parameterizedProperty != null && property == null) |
| { |
| return parameterizedProperty as T; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSMemberInfoInternalCollection<T> GetMembers<T>(object obj) |
| { |
| var returnValue = new PSMemberInfoInternalCollection<T>(); |
| if (typeof(T).IsAssignableFrom(typeof(PSProperty))) |
| { |
| DoAddAllProperties<T>(obj, returnValue); |
| } |
|
|
| base.AddAllMethods(obj, returnValue, true); |
| if (IsTypeParameterizedProperty(typeof(T))) |
| { |
| var parameterizedProperties = new PSMemberInfoInternalCollection<PSParameterizedProperty>(); |
| base.AddAllProperties(obj, parameterizedProperties, true); |
| foreach (PSParameterizedProperty parameterizedProperty in parameterizedProperties) |
| { |
| try |
| { |
| returnValue.Add(parameterizedProperty as T); |
| } |
| catch (ExtendedTypeSystemException) |
| { |
| |
| } |
| } |
| } |
|
|
| return returnValue; |
| } |
| } |
|
|
| |
| |
| |
| internal class XmlNodeAdapter : PropertyOnlyAdapter |
| { |
| #region virtual |
| |
| |
| |
| |
| protected override IEnumerable<string> GetTypeNameHierarchy(object obj) |
| { |
| XmlNode node = (XmlNode)obj; |
| string nodeNamespace = node.NamespaceURI; |
| IEnumerable<string> baseTypeNames = GetDotNetTypeNameHierarchy(obj); |
| if (string.IsNullOrEmpty(nodeNamespace)) |
| { |
| foreach (string baseType in baseTypeNames) |
| { |
| yield return baseType; |
| } |
| } |
| else |
| { |
| StringBuilder firstType = null; |
| foreach (string baseType in baseTypeNames) |
| { |
| if (firstType == null) |
| { |
| firstType = new StringBuilder(baseType); |
| firstType.Append('#'); |
| firstType.Append(node.NamespaceURI); |
| firstType.Append('#'); |
| firstType.Append(node.LocalName); |
| yield return firstType.ToString(); |
| } |
|
|
| yield return baseType; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members) |
| { |
| XmlNode node = (XmlNode)obj; |
|
|
| Dictionary<string, List<XmlNode>> nodeArrays = new Dictionary<string, List<XmlNode>>(StringComparer.OrdinalIgnoreCase); |
|
|
| if (node.Attributes != null) |
| { |
| foreach (XmlNode attribute in node.Attributes) |
| { |
| List<XmlNode> nodeList; |
| if (!nodeArrays.TryGetValue(attribute.LocalName, out nodeList)) |
| { |
| nodeList = new List<XmlNode>(); |
| nodeArrays[attribute.LocalName] = nodeList; |
| } |
|
|
| nodeList.Add(attribute); |
| } |
| } |
|
|
| if (node.ChildNodes != null) |
| { |
| foreach (XmlNode childNode in node.ChildNodes) |
| { |
| |
| if (childNode is XmlWhitespace) |
| { |
| continue; |
| } |
|
|
| List<XmlNode> nodeList; |
| if (!nodeArrays.TryGetValue(childNode.LocalName, out nodeList)) |
| { |
| nodeList = new List<XmlNode>(); |
| nodeArrays[childNode.LocalName] = nodeList; |
| } |
|
|
| nodeList.Add(childNode); |
| } |
| } |
|
|
| foreach (KeyValuePair<string, List<XmlNode>> nodeArrayEntry in nodeArrays) |
| { |
| members.Add(new PSProperty(nodeArrayEntry.Key, this, obj, nodeArrayEntry.Value.ToArray()) as T); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSProperty DoGetProperty(object obj, string propertyName) |
| { |
| XmlNode[] nodes = FindNodes(obj, propertyName, StringComparison.OrdinalIgnoreCase); |
| if (nodes.Length == 0) |
| { |
| return null; |
| } |
|
|
| return new PSProperty(nodes[0].LocalName, this, obj, nodes); |
| } |
|
|
| protected override PSProperty DoGetFirstPropertyOrDefault(object obj, MemberNamePredicate predicate) |
| { |
| XmlNode node = FindNode(obj, predicate); |
| return node == null ? null : new PSProperty(node.LocalName, this, obj, node); |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsSettable(PSProperty property) |
| { |
| XmlNode[] nodes = (XmlNode[])property.adapterData; |
| Diagnostics.Assert(nodes.Length != 0, "DoGetProperty would not return an empty array, it would return null instead"); |
| if (nodes.Length != 1) |
| { |
| return false; |
| } |
|
|
| XmlNode node = nodes[0]; |
| if (node is XmlText) |
| { |
| return true; |
| } |
|
|
| if (node is XmlAttribute) |
| { |
| return true; |
| } |
|
|
| XmlAttributeCollection nodeAttributes = node.Attributes; |
| if ((nodeAttributes != null) && (nodeAttributes.Count != 0)) |
| { |
| return false; |
| } |
|
|
| XmlNodeList nodeChildren = node.ChildNodes; |
| if ((nodeChildren == null) || (nodeChildren.Count == 0)) |
| { |
| return true; |
| } |
|
|
| if ((nodeChildren.Count == 1) && (nodeChildren[0].NodeType == XmlNodeType.Text)) |
| { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsGettable(PSProperty property) |
| { |
| return true; |
| } |
|
|
| private static object GetNodeObject(XmlNode node) |
| { |
| XmlText text = node as XmlText; |
| if (text != null) |
| { |
| return text.InnerText; |
| } |
|
|
| XmlAttributeCollection nodeAttributes = node.Attributes; |
|
|
| |
| if ((nodeAttributes != null) && (nodeAttributes.Count != 0)) |
| { |
| return node; |
| } |
|
|
| |
| if (!node.HasChildNodes) |
| { |
| return node.InnerText; |
| } |
|
|
| XmlNodeList nodeChildren = node.ChildNodes; |
| |
| if ((nodeChildren.Count == 1) && (nodeChildren[0].NodeType == XmlNodeType.Text)) |
| { |
| return node.InnerText; |
| } |
|
|
| XmlAttribute attribute = node as XmlAttribute; |
| if (attribute != null) |
| { |
| return attribute.Value; |
| } |
|
|
| return node; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override object PropertyGet(PSProperty property) |
| { |
| XmlNode[] nodes = (XmlNode[])property.adapterData; |
|
|
| if (nodes.Length == 1) |
| { |
| return GetNodeObject(nodes[0]); |
| } |
|
|
| object[] returnValue = new object[nodes.Length]; |
| for (int i = 0; i < nodes.Length; i++) |
| { |
| returnValue[i] = GetNodeObject(nodes[i]); |
| } |
|
|
| return returnValue; |
| } |
| |
| |
| |
| |
| |
| |
| protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) |
| { |
| |
| string valueString = LanguagePrimitives.ConvertTo<string>(setValue); |
| XmlNode[] nodes = (XmlNode[])property.adapterData; |
| Diagnostics.Assert(nodes.Length != 0, "DoGetProperty would not return an empty array, it would return null instead"); |
| if (nodes.Length > 1) |
| { |
| throw new SetValueException("XmlNodeSetRestrictionsMoreThanOneNode", |
| null, |
| ExtendedTypeSystem.XmlNodeSetShouldBeAString, |
| property.Name); |
| } |
|
|
| XmlNode node = nodes[0]; |
| XmlText text = node as XmlText; |
| if (text != null) |
| { |
| text.InnerText = valueString; |
| return; |
| } |
|
|
| XmlAttributeCollection nodeAttributes = node.Attributes; |
| |
| if ((nodeAttributes != null) && (nodeAttributes.Count != 0)) |
| { |
| throw new SetValueException("XmlNodeSetRestrictionsNodeWithAttributes", |
| null, |
| ExtendedTypeSystem.XmlNodeSetShouldBeAString, |
| property.Name); |
| } |
|
|
| XmlNodeList nodeChildren = node.ChildNodes; |
| if (nodeChildren == null || nodeChildren.Count == 0) |
| { |
| node.InnerText = valueString; |
| return; |
| } |
|
|
| if ((nodeChildren.Count == 1) && (nodeChildren[0].NodeType == XmlNodeType.Text)) |
| { |
| node.InnerText = valueString; |
| return; |
| } |
|
|
| XmlAttribute attribute = node as XmlAttribute; |
| if (attribute != null) |
| { |
| attribute.Value = valueString; |
| return; |
| } |
|
|
| throw new SetValueException("XmlNodeSetRestrictionsUnknownNodeType", |
| null, |
| ExtendedTypeSystem.XmlNodeSetShouldBeAString, |
| property.Name); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override string PropertyType(PSProperty property, bool forDisplay) |
| { |
| object value = null; |
| try |
| { |
| value = BasePropertyGet(property); |
| } |
| catch (GetValueException) |
| { |
| } |
|
|
| var type = value == null ? typeof(object) : value.GetType(); |
| return forDisplay ? ToStringCodeMethods.Type(type) : type.FullName; |
| } |
| #endregion virtual |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private static XmlNode[] FindNodes(object obj, string propertyName, StringComparison comparisonType) |
| { |
| List<XmlNode> retValue = new List<XmlNode>(); |
| XmlNode node = (XmlNode)obj; |
|
|
| if (node.Attributes != null) |
| { |
| foreach (XmlNode attribute in node.Attributes) |
| { |
| if (attribute.LocalName.Equals(propertyName, comparisonType)) |
| { |
| retValue.Add(attribute); |
| } |
| } |
| } |
|
|
| foreach (XmlNode childNode in node.ChildNodes) |
| { |
| if (childNode is XmlWhitespace) |
| { |
| |
| continue; |
| } |
|
|
| if (childNode.LocalName.Equals(propertyName, comparisonType)) |
| { |
| retValue.Add(childNode); |
| } |
| } |
|
|
| return retValue.ToArray(); |
| } |
|
|
| private static XmlNode FindNode(object obj, MemberNamePredicate predicate) |
| { |
| var node = (XmlNode)obj; |
|
|
| if (node.Attributes != null) |
| { |
| foreach (XmlNode attribute in node.Attributes) |
| { |
| if (predicate(attribute.LocalName)) |
| { |
| return attribute; |
| } |
| } |
| } |
|
|
| foreach (XmlNode childNode in node.ChildNodes) |
| { |
| if (childNode is XmlWhitespace) |
| { |
| |
| continue; |
| } |
|
|
| if (predicate(childNode.LocalName)) |
| { |
| return childNode; |
| } |
| } |
|
|
| return null; |
| } |
| } |
|
|
| |
| |
| |
| internal class DataRowAdapter : PropertyOnlyAdapter |
| { |
| #region virtual |
|
|
| |
| |
| |
| |
| |
| protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members) |
| { |
| DataRow dataRow = (DataRow)obj; |
| if (dataRow.Table == null || dataRow.Table.Columns == null) |
| { |
| return; |
| } |
|
|
| foreach (DataColumn property in dataRow.Table.Columns) |
| { |
| members.Add(new PSProperty(property.ColumnName, this, obj, property.ColumnName) as T); |
| } |
|
|
| return; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSProperty DoGetProperty(object obj, string propertyName) |
| { |
| DataRow dataRow = (DataRow)obj; |
|
|
| if (!dataRow.Table.Columns.Contains(propertyName)) |
| { |
| return null; |
| } |
|
|
| string columnName = dataRow.Table.Columns[propertyName].ColumnName; |
| return new PSProperty(columnName, this, obj, columnName); |
| } |
|
|
| protected override PSProperty DoGetFirstPropertyOrDefault(object obj, MemberNamePredicate predicate) |
| { |
| DataRow dataRow = (DataRow)obj; |
|
|
| foreach (DataColumn property in dataRow.Table.Columns) |
| { |
| if (predicate(property.ColumnName)) |
| { |
| return new PSProperty(property.ColumnName, this, obj, property.ColumnName); |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override string PropertyType(PSProperty property, bool forDisplay) |
| { |
| string columnName = (string)property.adapterData; |
| DataRow dataRow = (DataRow)property.baseObject; |
| var dataType = dataRow.Table.Columns[columnName].DataType; |
| return forDisplay ? ToStringCodeMethods.Type(dataType) : dataType.FullName; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsSettable(PSProperty property) |
| { |
| string columnName = (string)property.adapterData; |
| DataRow dataRow = (DataRow)property.baseObject; |
| return !dataRow.Table.Columns[columnName].ReadOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsGettable(PSProperty property) |
| { |
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override object PropertyGet(PSProperty property) |
| { |
| DataRow dataRow = (DataRow)property.baseObject; |
| return dataRow[(string)property.adapterData]; |
| } |
| |
| |
| |
| |
| |
| |
| protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) |
| { |
| DataRow dataRow = (DataRow)property.baseObject; |
| dataRow[(string)property.adapterData] = setValue; |
| return; |
| } |
| #endregion virtual |
| } |
| |
| |
| |
| internal class DataRowViewAdapter : PropertyOnlyAdapter |
| { |
| #region virtual |
| |
| |
| |
| |
| |
| protected override void DoAddAllProperties<T>(object obj, PSMemberInfoInternalCollection<T> members) |
| { |
| DataRowView dataRowView = (DataRowView)obj; |
| if (dataRowView.Row == null || dataRowView.Row.Table == null || dataRowView.Row.Table.Columns == null) |
| { |
| return; |
| } |
|
|
| foreach (DataColumn property in dataRowView.Row.Table.Columns) |
| { |
| members.Add(new PSProperty(property.ColumnName, this, obj, property.ColumnName) as T); |
| } |
|
|
| return; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected override PSProperty DoGetProperty(object obj, string propertyName) |
| { |
| DataRowView dataRowView = (DataRowView)obj; |
|
|
| if (!dataRowView.Row.Table.Columns.Contains(propertyName)) |
| { |
| return null; |
| } |
|
|
| string columnName = dataRowView.Row.Table.Columns[propertyName].ColumnName; |
| return new PSProperty(columnName, this, obj, columnName); |
| } |
|
|
| protected override PSProperty DoGetFirstPropertyOrDefault(object obj, MemberNamePredicate predicate) |
| { |
| DataRowView dataRowView = (DataRowView)obj; |
|
|
| foreach (DataColumn column in dataRowView.Row.Table.Columns) |
| { |
| string columnName = column.ColumnName; |
| if (predicate(columnName)) |
| { |
| return new PSProperty(columnName, this, obj, columnName); |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected override string PropertyType(PSProperty property, bool forDisplay) |
| { |
| string columnName = (string)property.adapterData; |
| DataRowView dataRowView = (DataRowView)property.baseObject; |
| var dataType = dataRowView.Row.Table.Columns[columnName].DataType; |
| return forDisplay ? ToStringCodeMethods.Type(dataType) : dataType.FullName; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsSettable(PSProperty property) |
| { |
| string columnName = (string)property.adapterData; |
| DataRowView dataRowView = (DataRowView)property.baseObject; |
| return !dataRowView.Row.Table.Columns[columnName].ReadOnly; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override bool PropertyIsGettable(PSProperty property) |
| { |
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| protected override object PropertyGet(PSProperty property) |
| { |
| DataRowView dataRowView = (DataRowView)property.baseObject; |
| return dataRowView[(string)property.adapterData]; |
| } |
| |
| |
| |
| |
| |
| |
| protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) |
| { |
| DataRowView dataRowView = (DataRowView)property.baseObject; |
| dataRowView[(string)property.adapterData] = setValue; |
| return; |
| } |
| #endregion virtual |
| } |
|
|
| internal class TypeInference |
| { |
| [TraceSource("ETS", "Extended Type System")] |
| private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("ETS", "Extended Type System"); |
|
|
| internal static MethodInformation Infer(MethodInformation genericMethod, Type[] argumentTypes) |
| { |
| Dbg.Assert(genericMethod != null, "Caller should verify that genericMethod != null"); |
| Dbg.Assert(argumentTypes != null, "Caller should verify that arguments != null"); |
|
|
| |
| |
| |
| MethodInfo originalMethod = (MethodInfo)genericMethod.method; |
| MethodInfo inferredMethod = TypeInference.Infer(originalMethod, argumentTypes, genericMethod.hasVarArgs); |
|
|
| if (inferredMethod != null) |
| { |
| return new MethodInformation(inferredMethod, 0); |
| } |
| else |
| { |
| return null; |
| } |
| } |
|
|
| private static MethodInfo Infer(MethodInfo genericMethod, Type[] typesOfMethodArguments, bool hasVarArgs) |
| { |
| Dbg.Assert(genericMethod != null, "Caller should verify that genericMethod != null"); |
| Dbg.Assert(typesOfMethodArguments != null, "Caller should verify that arguments != null"); |
|
|
| if (!genericMethod.ContainsGenericParameters) |
| { |
| return genericMethod; |
| } |
|
|
| Type[] typeParameters = genericMethod.GetGenericArguments(); |
| Type[] typesOfMethodParameters = genericMethod.GetParameters().Select(static p => p.ParameterType).ToArray(); |
|
|
| MethodInfo inferredMethod = Infer(genericMethod, typeParameters, typesOfMethodParameters, typesOfMethodArguments); |
|
|
| |
| |
| |
| |
| |
| if (inferredMethod == null && |
| hasVarArgs && |
| typesOfMethodArguments.Length >= (typesOfMethodParameters.Length - 1)) |
| { |
| IEnumerable<Type> typeOfRegularParameters = typesOfMethodParameters.Take(typesOfMethodParameters.Length - 1); |
| IEnumerable<Type> multipliedVarArgsElementType = Enumerable.Repeat( |
| typesOfMethodParameters[typesOfMethodParameters.Length - 1].GetElementType(), |
| typesOfMethodArguments.Length - typesOfMethodParameters.Length + 1); |
|
|
| inferredMethod = Infer( |
| genericMethod, |
| typeParameters, |
| typeOfRegularParameters.Concat(multipliedVarArgsElementType), |
| typesOfMethodArguments); |
| } |
|
|
| return inferredMethod; |
| } |
|
|
| private static MethodInfo Infer(MethodInfo genericMethod, ICollection<Type> typeParameters, IEnumerable<Type> typesOfMethodParameters, IEnumerable<Type> typesOfMethodArguments) |
| { |
| Dbg.Assert(genericMethod != null, "Caller should verify that genericMethod != null"); |
| Dbg.Assert(typeParameters != null, "Caller should verify that typeParameters != null"); |
| Dbg.Assert(typesOfMethodParameters != null, "Caller should verify that typesOfMethodParameters != null"); |
| Dbg.Assert(typesOfMethodArguments != null, "Caller should verify that typesOfMethodArguments != null"); |
|
|
| using (s_tracer.TraceScope("Inferring type parameters for the following method: {0}", genericMethod)) |
| { |
| if ((s_tracer.Options & PSTraceSourceOptions.WriteLine) == PSTraceSourceOptions.WriteLine) |
| { |
| s_tracer.WriteLine( |
| "Types of method arguments: {0}", |
| string.Join(", ", typesOfMethodArguments.Select(static t => t.ToString()).ToArray())); |
| } |
|
|
| var typeInference = new TypeInference(typeParameters); |
| if (!typeInference.UnifyMultipleTerms(typesOfMethodParameters, typesOfMethodArguments)) |
| { |
| return null; |
| } |
|
|
| IEnumerable<Type> inferredTypeParameters = typeParameters.Select(typeInference.GetInferredType); |
| if (inferredTypeParameters.Any(static inferredType => inferredType == null)) |
| { |
| return null; |
| } |
|
|
| try |
| { |
| MethodInfo instantiatedMethod = genericMethod.MakeGenericMethod(inferredTypeParameters.ToArray()); |
| s_tracer.WriteLine("Inference successful: {0}", instantiatedMethod); |
| return instantiatedMethod; |
| } |
| catch (ArgumentException e) |
| { |
| |
| s_tracer.WriteLine("Inference failure: {0}", e.Message); |
| return null; |
| } |
| } |
| } |
|
|
| private readonly HashSet<Type>[] _typeParameterIndexToSetOfInferenceCandidates; |
|
|
| #if DEBUG |
| private readonly HashSet<Type> _typeParametersOfTheMethod; |
| #endif |
|
|
| internal TypeInference(ICollection<Type> typeParameters) |
| { |
| #if DEBUG |
| Dbg.Assert(typeParameters != null, "Caller should verify that typeParameters != null"); |
| Dbg.Assert( |
| typeParameters.All(t => t.IsGenericParameter), |
| "Caller should verify that typeParameters are really generic type parameters"); |
| #endif |
| _typeParameterIndexToSetOfInferenceCandidates = new HashSet<Type>[typeParameters.Count]; |
| #if DEBUG |
| List<int> listOfTypeParameterPositions = typeParameters.Select(static t => t.GenericParameterPosition).ToList(); |
| listOfTypeParameterPositions.Sort(); |
| Dbg.Assert( |
| listOfTypeParameterPositions.Count == listOfTypeParameterPositions.Distinct().Count(), |
| "No type parameters should occupy the same position"); |
| Dbg.Assert( |
| listOfTypeParameterPositions.All(p => p >= 0), |
| "Type parameter positions should be between 0 and #ofParams"); |
| Dbg.Assert( |
| listOfTypeParameterPositions.All(p => p < _typeParameterIndexToSetOfInferenceCandidates.Length), |
| "Type parameter positions should be between 0 and #ofParams"); |
|
|
| _typeParametersOfTheMethod = new HashSet<Type>(); |
| foreach (Type t in typeParameters) |
| { |
| _typeParametersOfTheMethod.Add(t); |
| } |
| #endif |
| } |
|
|
| internal Type GetInferredType(Type typeParameter) |
| { |
| #if DEBUG |
| Dbg.Assert(typeParameter != null, "Caller should verify typeParameter != null"); |
| Dbg.Assert( |
| _typeParametersOfTheMethod.Contains(typeParameter), |
| "Caller should verify that typeParameter is actually a generic type parameter of the method"); |
| #endif |
|
|
| ICollection<Type> inferenceCandidates = |
| _typeParameterIndexToSetOfInferenceCandidates[typeParameter.GenericParameterPosition]; |
|
|
| if ((inferenceCandidates != null) && (inferenceCandidates.Any(static t => t == typeof(LanguagePrimitives.Null)))) |
| { |
| Type firstValueType = inferenceCandidates.FirstOrDefault(static t => t.IsValueType); |
| if (firstValueType != null) |
| { |
| s_tracer.WriteLine("Cannot reconcile null and {0} (a value type)", firstValueType); |
| inferenceCandidates = null; |
| _typeParameterIndexToSetOfInferenceCandidates[typeParameter.GenericParameterPosition] = null; |
| } |
| else |
| { |
| inferenceCandidates = inferenceCandidates.Where(static t => t != typeof(LanguagePrimitives.Null)).ToList(); |
| if (inferenceCandidates.Count == 0) |
| { |
| inferenceCandidates = null; |
| _typeParameterIndexToSetOfInferenceCandidates[typeParameter.GenericParameterPosition] = null; |
| } |
| } |
| } |
|
|
| if ((inferenceCandidates != null) && (inferenceCandidates.Count > 1)) |
| { |
| |
| Type commonBaseClass = inferenceCandidates.FirstOrDefault( |
| potentiallyCommonBaseClass => |
| inferenceCandidates.All( |
| otherCandidate => |
| otherCandidate == potentiallyCommonBaseClass || |
| potentiallyCommonBaseClass.IsAssignableFrom(otherCandidate))); |
|
|
| if (commonBaseClass != null) |
| { |
| inferenceCandidates.Clear(); |
| inferenceCandidates.Add(commonBaseClass); |
| } |
| else |
| { |
| s_tracer.WriteLine("Multiple unreconcilable inferences for type parameter {0}", typeParameter); |
| inferenceCandidates = null; |
| _typeParameterIndexToSetOfInferenceCandidates[typeParameter.GenericParameterPosition] = null; |
| } |
| } |
|
|
| if (inferenceCandidates == null) |
| { |
| s_tracer.WriteLine("Couldn't infer type parameter {0}", typeParameter); |
| return null; |
| } |
| else |
| { |
| Dbg.Assert(inferenceCandidates.Count == 1, "inferenceCandidates should contain exactly 1 element at this point"); |
| return inferenceCandidates.Single(); |
| } |
| } |
|
|
| internal bool UnifyMultipleTerms(IEnumerable<Type> parameterTypes, IEnumerable<Type> argumentTypes) |
| { |
| List<Type> leftList = parameterTypes.ToList(); |
| List<Type> rightList = argumentTypes.ToList(); |
|
|
| if (leftList.Count != rightList.Count) |
| { |
| s_tracer.WriteLine("Mismatch in number of parameters and arguments"); |
| return false; |
| } |
|
|
| for (int i = 0; i < leftList.Count; i++) |
| { |
| if (!this.Unify(leftList[i], rightList[i])) |
| { |
| s_tracer.WriteLine("Couldn't unify {0} with {1}", leftList[i], rightList[i]); |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
|
|
| private bool Unify(Type parameterType, Type argumentType) |
| { |
| if (!parameterType.ContainsGenericParameters) |
| { |
| return true; |
| } |
|
|
| if (parameterType.IsGenericParameter) |
| { |
| #if DEBUG |
| Dbg.Assert( |
| _typeParametersOfTheMethod.Contains(parameterType), |
| "Only uninstantiated generic type parameters encountered in real life, should be the ones coming from the method"); |
| #endif |
|
|
| HashSet<Type> inferenceCandidates = _typeParameterIndexToSetOfInferenceCandidates[parameterType.GenericParameterPosition]; |
| if (inferenceCandidates == null) |
| { |
| inferenceCandidates = new HashSet<Type>(); |
| _typeParameterIndexToSetOfInferenceCandidates[parameterType.GenericParameterPosition] = inferenceCandidates; |
| } |
|
|
| inferenceCandidates.Add(argumentType); |
| s_tracer.WriteLine("Inferred {0} => {1}", parameterType, argumentType); |
| return true; |
| } |
|
|
| if (parameterType.IsArray) |
| { |
| if (argumentType == typeof(LanguagePrimitives.Null)) |
| { |
| return true; |
| } |
|
|
| if (argumentType.IsArray && parameterType.GetArrayRank() == argumentType.GetArrayRank()) |
| { |
| return this.Unify(parameterType.GetElementType(), argumentType.GetElementType()); |
| } |
|
|
| s_tracer.WriteLine("Couldn't unify array {0} with {1}", parameterType, argumentType); |
| return false; |
| } |
|
|
| if (parameterType.IsByRef) |
| { |
| if (argumentType.IsGenericType && argumentType.GetGenericTypeDefinition() == typeof(PSReference<>)) |
| { |
| Type referencedType = argumentType.GetGenericArguments()[0]; |
| if (referencedType == typeof(LanguagePrimitives.Null)) |
| { |
| return true; |
| } |
| else |
| { |
| return this.Unify( |
| parameterType.GetElementType(), |
| referencedType); |
| } |
| } |
| else |
| { |
| s_tracer.WriteLine("Couldn't unify reference type {0} with {1}", parameterType, argumentType); |
| return false; |
| } |
| } |
|
|
| if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| { |
| if (argumentType == typeof(LanguagePrimitives.Null)) |
| { |
| return true; |
| } |
|
|
| return this.Unify(parameterType.GetGenericArguments()[0], argumentType); |
| } |
|
|
| if (parameterType.IsGenericType) |
| { |
| if (argumentType == typeof(LanguagePrimitives.Null)) |
| { |
| return true; |
| } |
|
|
| return this.UnifyConstructedType(parameterType, argumentType); |
| } |
|
|
| Dbg.Assert(false, "Unrecognized kind of type"); |
| s_tracer.WriteLine("Unrecognized kind of type: {0}", parameterType); |
| return false; |
| } |
|
|
| private bool UnifyConstructedType(Type parameterType, Type argumentType) |
| { |
| Dbg.Assert(parameterType.IsGenericType, "Caller should verify parameterType.IsGenericType before calling this method"); |
|
|
| if (IsEqualGenericTypeDefinition(parameterType, argumentType)) |
| { |
| IEnumerable<Type> typeParametersOfParameterType = parameterType.GetGenericArguments(); |
| IEnumerable<Type> typeArgumentsOfArgumentType = argumentType.GetGenericArguments(); |
| return this.UnifyMultipleTerms(typeParametersOfParameterType, typeArgumentsOfArgumentType); |
| } |
|
|
| Type[] interfaces = argumentType.GetInterfaces(); |
| for (int i = 0; i < interfaces.Length; i++) |
| { |
| if (IsEqualGenericTypeDefinition(parameterType, interfaces[i])) |
| { |
| return UnifyConstructedType(parameterType, interfaces[i]); |
| } |
| } |
|
|
| Type baseType = argumentType.BaseType; |
| while (baseType != null) |
| { |
| if (IsEqualGenericTypeDefinition(parameterType, baseType)) |
| { |
| return UnifyConstructedType(parameterType, baseType); |
| } |
|
|
| baseType = baseType.BaseType; |
| } |
|
|
| s_tracer.WriteLine("Attempt to unify different constructed types: {0} and {1}", parameterType, argumentType); |
| return false; |
| } |
|
|
| private static bool IsEqualGenericTypeDefinition(Type parameterType, Type argumentType) |
| { |
| Dbg.Assert(parameterType.IsGenericType, "Caller should verify parameterType.IsGenericType before calling this method"); |
|
|
| if (!argumentType.IsGenericType) |
| { |
| return false; |
| } |
|
|
| return parameterType.GetGenericTypeDefinition() == argumentType.GetGenericTypeDefinition(); |
| } |
| } |
| } |
|
|