Exemple #1
0
        internal static object AsOperator(object left, Type type)
        {
            bool flag;

            if (type == null)
            {
                throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "AsOperatorRequiresType", ParserStrings.AsOperatorRequiresType, new object[0]);
            }
            LanguagePrimitives.ConversionData data = LanguagePrimitives.FigureConversion(left, type, out flag);
            if (data.Rank == ConversionRank.None)
            {
                return(null);
            }
            try
            {
                if (flag)
                {
                    return(data.Invoke(PSObject.Base(left), type, false, (PSObject)left, NumberFormatInfo.InvariantInfo, null));
                }
                return(data.Invoke(left, type, false, null, NumberFormatInfo.InvariantInfo, null));
            }
            catch (PSInvalidCastException)
            {
                return(null);
            }
        }
Exemple #2
0
        internal bool IsMatchingType(PSTypeName psTypeName)
        {
            Type fromType = psTypeName.Type;

            if (fromType != null)
            {
                bool flag = LanguagePrimitives.FigureConversion(typeof(object), this.ParameterType).Rank >= ConversionRank.AssignableS2A;
                if (fromType.Equals(typeof(object)))
                {
                    return(flag);
                }
                if (flag)
                {
                    return((psTypeName.Type != null) && psTypeName.Type.Equals(typeof(object)));
                }
                LanguagePrimitives.ConversionData data = LanguagePrimitives.FigureConversion(fromType, this.ParameterType);
                return((data != null) && (data.Rank >= ConversionRank.NumericImplicitS2A));
            }
            WildcardPattern pattern = new WildcardPattern("*" + (psTypeName.Name ?? ""), WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);

            if (pattern.IsMatch(this.ParameterType.FullName))
            {
                return(true);
            }
            if (this.ParameterType.IsArray && pattern.IsMatch(this.ParameterType.GetElementType().FullName))
            {
                return(true);
            }
            if (this.Attributes != null)
            {
                PSTypeNameAttribute attribute = this.Attributes.OfType <PSTypeNameAttribute>().FirstOrDefault <PSTypeNameAttribute>();
                if ((attribute != null) && pattern.IsMatch(attribute.PSTypeName))
                {
                    return(true);
                }
            }
            return(false);
        }