Пример #1
0
        public static MethodDefinition Method(this TypeDefinition typeDefinition, string name,
                                              bool?isStatic = null,
                                              ParameterTypeCollection parameters = null,
                                              int skipMethodParameters           = 0,
                                              int skipInputParameters            = 0,
                                              bool acceptParamObjectTypes        = false,
                                              bool substituteByRefs = false
                                              )
        {
            IEnumerable <ParameterType> parametersClone = null;

            if (parameters != null)
            {
                if (skipInputParameters > 0)
                {
                    parametersClone = parameters.ToArray().Skip(skipInputParameters);
                }
                else
                {
                    parametersClone = parameters.ToArray();
                }
            }

            var matches = typeDefinition.Methods.Where(
                x => x.Name == name &&
                (isStatic == null || x.IsStatic == isStatic.Value)
                );

            if (parameters != null)
            {
                matches = matches.Where(x =>
                                        (
                                            skipMethodParameters > 0 ?
                                            x.Parameters.Skip(skipMethodParameters).ToParameters() :
                                            x.Parameters.ToParameters()
                                        ).CompareParameterTypes(parametersClone, acceptParamObjectTypes, substituteByRefs)
                                        );
            }

            if (matches.Count() == 0)
            {
                throw new Exception($"Method `{name}` is not found in {typeDefinition.FullName}. Expected {parametersClone.ToParamString()}.");
            }
            else if (matches.Count() > 1)
            {
                throw new Exception($"Too many methods named `{name}` found in {typeDefinition.FullName}");
            }

            return(matches.Single());
        }
Пример #2
0
		public static MethodDefinition Method(this TypeDefinition typeDefinition, string name,
			bool? isStatic = null,
			ParameterTypeCollection parameters = null,
			int skipMethodParameters = 0,
			int skipInputParameters = 0,
			bool acceptParamObjectTypes = false,
			bool substituteByRefs = false
		)
		{
			IEnumerable<ParameterType> parametersClone = null;
			if (parameters != null)
			{
				if (skipInputParameters > 0)
				{
					parametersClone = parameters.ToArray().Skip(skipInputParameters);
				}
				else
				{
					parametersClone = parameters.ToArray();
				}
			}

			var matches = typeDefinition.Methods.Where(
				 x => x.Name == name
				 && (isStatic == null || x.IsStatic == isStatic.Value)
			);

			if (parameters != null)
			{
				matches = matches.Where(x =>
					(
						skipMethodParameters > 0 ? 
							x.Parameters.Skip(skipMethodParameters).ToParameters() : 
							x.Parameters.ToParameters()
					).CompareParameterTypes(parametersClone, acceptParamObjectTypes, substituteByRefs)
				);
			}

			if (matches.Count() == 0)
				throw new Exception($"Method `{name}` is not found in {typeDefinition.FullName}. Expected {parametersClone.ToParamString()}.");
			else if (matches.Count() > 1)
				throw new Exception($"Too many methods named `{name}` found in {typeDefinition.FullName}");

			return matches.Single();
		}