示例#1
0
        public static MethodName FromMethodInfo(System.Reflection.MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
            {
                return(new GenericInstanceMethodName(
                           FromMethodInfo(method.GetGenericMethodDefinition()),
                           method.GetGenericArguments().Select(TypeName.FromType).ToArray()
                           ));
            }

            var generics = DefaultMethodName.SetGenericMangle(0);

            if (method.IsGenericMethodDefinition)
            {
                generics = DefaultMethodName.SetGenericArguments(method.GetGenericArguments());
            }

            var result = new DefaultMethodName(
                TypeName.FromType(method.DeclaringType),
                method.Name,
                generics,
                DefaultMethodName.SetParameters(method.GetParameters()),
                DefaultMethodName.SetReturnType(TypeName.FromType(method.ReturnType))
                );

            return(result);
        }
示例#2
0
        public MethodName GetMethod(string name, params TypeName[] parameters)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (name.Length == 0)
            {
                throw Failure.EmptyString(nameof(name));
            }

            parameters = parameters ?? Array.Empty <TypeName>();
            if (parameters.Any(t => t == null))
            {
                throw Failure.CollectionContainsNullElement("parameters");
            }

            int mangle;
            var method = TypeName.StripMangle(name, out mangle);

            return(new DefaultMethodName(
                       this,
                       method,
                       DefaultMethodName.SetGenericMangle(mangle),
                       DefaultMethodName.SetParameters(parameters)
                       ));
        }
示例#3
0
        public static MethodName FromConstructorInfo(System.Reflection.ConstructorInfo constructor)
        {
            if (constructor == null)
            {
                throw new ArgumentNullException("constructor");
            }

            return(new DefaultMethodName(
                       TypeName.FromType(constructor.DeclaringType),
                       constructor.Name,
                       DefaultMethodName.SetGenericMangle(0),
                       DefaultMethodName.SetParameters(
                           ParameterData.ConvertAll(constructor.GetParameters())
                           )
                       ));
        }
示例#4
0
        public MethodName ParseMethod()
        {
            _s.RequireMoveNext();

            string   name          = null;
            int      sz            = 0; // of generics
            TypeName declaringType = ParseDeclaringTypeOpt(out name);

            ParameterData[] pms        = null;
            TypeName        returnType = null;
            AssemblyName    asm        = null;

            if (_s.Type == TokenType.DoubleColon)
            {
                throw new NotImplementedException();
            }

            name = MethodName.StripMangle(name, out sz);

            bool hasGenericParams = _s.Type == TokenType.LessThan;
            var  generics         = ParseTypeParametersOpt();

            if (hasGenericParams)
            {
                MoveNext();
            }

            if (_s.Type == TokenType.LeftParen)
            {
                pms = ParseParameters(TokenType.RightParen).ToArray();
                _s.MoveToContent();
            }

            returnType = ParseReturnTypeOpt();
            asm        = ParseAssemblyNameOpt();
            ParseEOF();

            if (declaringType != null)
            {
                declaringType = declaringType.WithAssembly(asm);
            }

            if (!hasGenericParams && sz > 0)
            {
                // Method``2(A,B) -- mangled but no type parameter syntax
                return(new DefaultMethodName(
                           declaringType,
                           name,
                           DefaultMethodName.SetGenericMangle(sz),
                           DefaultMethodName.SetParameters(pms),
                           DefaultMethodName.SetReturnType(returnType)
                           ));
            }
            else if (generics.MustBeParameters || (generics.CouldBeParameters && _preferGenericParamsInMethods))
            {
                return(new DefaultMethodName(
                           declaringType,
                           name,
                           DefaultMethodName.SetGenerics(generics.ConvertToGenerics(true).ToArray()),
                           DefaultMethodName.SetParameters(pms),
                           DefaultMethodName.SetReturnType(returnType)
                           ));
                // TODO Could have discrepancy between generics declared with position and
                // named in type parameter list (sz != generics.Raw.Count)
            }
            else
            {
                var elementName = new DefaultMethodName(
                    declaringType,
                    name,
                    DefaultMethodName.SetGenericMangle(generics.Raw.Count),
                    DefaultMethodName.SetParameters(pms),
                    DefaultMethodName.SetReturnType(returnType)
                    );
                return(new GenericInstanceMethodName(elementName, generics.Raw.ToArray()));
            }
        }