示例#1
0
文件: MethodInfo.cs 项目: niik/RxSpy
        string GetArguments(bcl.MethodBase method)
        {
            var arguments = new List<string>();

            foreach (var arg in method.GetParameters())
            {
                arguments.Add(GetArgument(arg));
            }

            return string.Join(", ", arguments);
        }
示例#2
0
 XmlNode FindMatch (XmlNodeList nodes, SR.MethodInfo methodBase)
 {
     SR.ParameterInfo[] p = methodBase.GetParameters ();
     string s = "";
     foreach (XmlNode node in nodes) {
         XmlNodeList paramList = node.SelectNodes ("Parameters/*");
         s += paramList.Count + " - " + p.Length + "\n";
         if (p.Length == 0 && paramList.Count == 0) return node;
         if (p.Length != paramList.Count) continue;
         bool matched = true;
         for (int i = 0; i < p.Length; i++) {
             if (p[i].ParameterType.ToString () != paramList[i].Attributes["Type"].Value) {
                 matched = false;
             }
         }
         if (matched)
             return node;
     }
     return null;
 }
示例#3
0
		MethodReference ImportMethodBase (SR.MethodBase mb, Type retType, ImportContext context)
		{
			if (IsGenericMethod (mb) && !IsGenericMethodDefinition (mb))
				return ImportGenericInstanceMethod ((SR.MethodInfo) mb, context);

			Type originalDecType = mb.DeclaringType;
			Type declaringTypeDef = originalDecType;
			while (IsGenericTypeSpec (declaringTypeDef))
				declaringTypeDef = GetGenericTypeDefinition (declaringTypeDef);

			if (mb.DeclaringType != declaringTypeDef && mb is SR.MethodInfo) {
				int mt = GetMetadataToken (mb as SR.MethodInfo);
				// hack to get the generic method definition from the constructed method
				foreach (SR.MethodInfo mi in declaringTypeDef.GetMethods ()) {
					if (GetMetadataToken (mi) == mt) {
						mb = mi;
						retType = mi.ReturnType;
						break;
					}
				}
			}

			string sig = GetMethodBaseSignature (mb, originalDecType, retType);
			MethodReference meth = (MethodReference) GetMemberReference (sig);
			if (meth != null)
				return meth;

			meth = new MethodReference (
				mb.Name,
				(mb.CallingConvention & SR.CallingConventions.HasThis) > 0,
				(mb.CallingConvention & SR.CallingConventions.ExplicitThis) > 0,
				MethodCallingConvention.Default); // TODO: get the real callconv
			meth.DeclaringType = ImportSystemType (originalDecType, context);

			if (IsGenericMethod (mb))
				foreach (Type genParam in GetGenericArguments (mb as SR.MethodInfo))
					meth.GenericParameters.Add (new GenericParameter (genParam.Name, meth));

			TypeReference contextType = context.GenericContext.Type;
			MethodReference contextMethod = context.GenericContext.Method;

			context.GenericContext.Method = meth;
			context.GenericContext.Type = ImportSystemType (declaringTypeDef, context);

			meth.ReturnType.ReturnType = ImportSystemType (retType, context);

			SR.ParameterInfo [] parameters = mb.GetParameters ();
			for (int i = 0; i < parameters.Length; i++)
				meth.Parameters.Add (new ParameterDefinition (
					ImportSystemType (parameters [i].ParameterType, context)));

			context.GenericContext.Type = contextType;
			context.GenericContext.Method = contextMethod;

			m_module.MemberReferences.Add (meth);
			return meth;
		}
示例#4
0
		static string GetMethodBaseSignature (SR.MethodBase meth, Type declaringType, Type retType)
		{
			StringBuilder sb = new StringBuilder ();
			sb.Append (GetTypeSignature (retType));
			sb.Append (' ');
			sb.Append (GetTypeSignature (declaringType));
			sb.Append ("::");
			sb.Append (meth.Name);
			if (IsGenericMethodSpec (meth)) {
				sb.Append ("<");
				Type [] genArgs = GetGenericArguments (meth as SR.MethodInfo);
				for (int i = 0; i < genArgs.Length; i++) {
					if (i > 0)
						sb.Append (",");
					sb.Append (GetTypeSignature (genArgs [i]));
				}
				sb.Append (">");
			}
			sb.Append ("(");
			SR.ParameterInfo [] parameters = meth.GetParameters ();
			for (int i = 0; i < parameters.Length; i++) {
				if (i > 0)
					sb.Append (",");
				sb.Append (GetTypeSignature (parameters [i].ParameterType));
			}
			sb.Append (")");
			return sb.ToString ();
		}
示例#5
0
		public MethodReference ImportMethod (SR.MethodBase method, IGenericContext context, ImportGenericKind import_kind)
		{
			if (IsMethodSpecification (method) || ImportOpenGenericMethod (method, import_kind))
				return ImportMethodSpecification (method, context);

			var declaring_type = ImportType (method.DeclaringType, context);

			if (IsGenericInstance (method.DeclaringType))
				method = method.Module.ResolveMethod (method.MetadataToken);

			var reference = new MethodReference {
				Name = method.Name,
				HasThis = HasCallingConvention (method, SR.CallingConventions.HasThis),
				ExplicitThis = HasCallingConvention (method, SR.CallingConventions.ExplicitThis),
				DeclaringType = ImportType (method.DeclaringType, context, ImportGenericKind.Definition),
			};

			if (HasCallingConvention (method, SR.CallingConventions.VarArgs))
				reference.CallingConvention &= MethodCallingConvention.VarArg;

			if (method.IsGenericMethod)
				ImportGenericParameters (reference, method.GetGenericArguments ());

			var method_info = method as SR.MethodInfo;
			reference.ReturnType = method_info != null
				? ImportType (method_info.ReturnType, context ?? reference)
				: ImportType (typeof (void), null);

			var parameters = method.GetParameters ();
			var reference_parameters = reference.Parameters;

			for (int i = 0; i < parameters.Length; i++)
				reference_parameters.Add (
					new ParameterDefinition (ImportType (parameters [i].ParameterType, context ?? reference)));

			reference.DeclaringType = declaring_type;

			return reference;
		}
示例#6
0
文件: Newobj.cs 项目: Orvid/Cosmos
        private static void PushAlignedParameterSize(SysReflection.MethodBase aMethod)
        {
            SysReflection.ParameterInfo[] xParams = aMethod.GetParameters();

            uint xSize;
            new Comment("[ Newobj.PushAlignedParameterSize start count = " + xParams.Length.ToString() + " ]");
            for (int i = 0; i < xParams.Length; i++)
            {
                xSize = SizeOfType(xParams[i].ParameterType);
                new CPUx86.Add { DestinationReg = CPUx86.Registers.ESP, SourceValue = Align(xSize, 4) };
            }
            new Comment("[ Newobj.PushAlignedParameterSize end ]");
        }
示例#7
0
        public string GetServiceMethodRequestSample(rfl.MethodInfo mi, string apiType)
        {
            if (mi == null)
                return "No such service method";

            if (mi.GetParameters().Length != 1)
                return "A service request method should have only one parameter";

            Type t = getServiceRequestType(mi.GetParameters()[0].ParameterType);
            object req = Activator.CreateInstance(t);
            req.SetMemberValue("APIKey", "SAMPLE_API_KEY");
            req.SetMemberValue("ResellerId", ConfigurationManager.AppSettings["ResellerId"]);
            object data;
            if (mi.GetParameters()[0].ParameterType == typeof(string))
                data = "";
            else
                data = Activator.CreateInstance(mi.GetParameters()[0].ParameterType);
            req.SetMemberValue("Data", data);

            foreach (rfl.PropertyInfo pi in data.GetType().GetProperties(rfl.BindingFlags.DeclaredOnly | rfl.BindingFlags.Public | rfl.BindingFlags.Instance))
                if (!(pi.PropertyType.IsPrimitive || pi.PropertyType == typeof(string)) && pi.GetSetMethod() != null)
                    pi.SetValue(data, pi.PropertyType.IsArray ? Array.CreateInstance(pi.PropertyType.GetElementType(), 0) : Activator.CreateInstance(pi.PropertyType), null);

            return serialize(req, apiType);
        }
示例#8
0
        public string GetServiceMethodDescription(rfl.MethodInfo mi)
        {
            DescriptionAttribute desc = mi.GetAttribute<DescriptionAttribute>();

            string description = "<table><tr><td colspan=\"2\"><i>" + (desc == null ? "No description." : desc.Description) + "</i></td></tr>";
            description += "<tr><td colspan=\"2\">&nbsp;</td></tr>";

            foreach (rfl.PropertyInfo pi in mi.GetParameters()[0].ParameterType.GetProperties())
            {
                DescriptionAttribute desc2 = pi.GetAttribute<DescriptionAttribute>();
                description += string.Format("<tr><td><b>{0}</b></td><td>{1}</td></tr>", pi.Name, (desc2 == null ? "" : desc2.Description));
            }

            description += "</table>";

            return description;
        }
示例#9
0
        private SR.MethodBase FindMatchingMethodByName(Type targetType, SR.MethodInfo injectionMethod, string methodName)
        {
            var pars = injectionMethod.GetParameters();
            var genArgs = injectionMethod.GetGenericArguments();

            // Handle generic parameters
            Func<Type, Type> rMapper = (injParamType) =>
            {
                var mappedType = TryMapTypeToTargetAssemblyType(targetType.Assembly, injParamType);
                return mappedType != null ? mappedType : injParamType;
            };

            foreach (var m in targetType.GetMethodsAndCtors(BINDING_FLAGS))
            {
                if (m.Name != methodName)
                    continue;

                var mParams = m.GetParameters();
                if (mParams.Length != pars.Length)
                    continue;

                if (!mParams.Zip(pars, (p1, p2) => new { P1 = p1, P2 = p2 }).All(a => a.P1.ParameterMatches(a.P2, rMapper)))
                    continue;

                if (!m.IsConstructor)
                {
                    var mGenArgs = m.GetGenericArguments();
                    if (mGenArgs.Length != genArgs.Length)
                        continue;
                }

                return m;
            }

            return null;
        }
示例#10
0
		List<TypeSig> GetParameters(SR.MethodBase delMethod) {
			var pms = new List<TypeSig>();
			foreach (var param in delMethod.GetParameters())
				pms.Add(importer.ImportAsTypeSig(param.ParameterType));
			return pms;
		}
示例#11
0
        public static bool MethodMatches(this MethodReference method, SR.MethodBase matches)
        {
            if (method.Name != matches.Name)
                return false;

            if (matches is SR.MethodInfo)
            {
                if (!method.ReturnType.TypeMatches(((SR.MethodInfo)matches).ReturnType))
                    return false;
            }

            var m1Params = method.Parameters;
            var m2Params = matches.GetParameters();

            if (m1Params.Count != m2Params.Length)
                return false;

            for (var i = 0; i < m1Params.Count; i++)
            {
                if (!m1Params[i].ParameterMatches(m2Params[i]))
                    return false;
            }

            if (!matches.IsConstructor)
            {
                var m1GenericArgs = method.Resolve().GenericParameters;
                var m2GenericArgs = matches.GetGenericArguments();

                if (m1GenericArgs.Count != m2GenericArgs.Length) // No need to check types or constraints, no overloading for these in C#
                    return false;
            }

            return true;
        }
示例#12
0
 public Method (Class declaringType, SR.MethodInfo tinfo)
 {
     this.declaringType = declaringType;
 
     ModifierEnum mod = (ModifierEnum)0;
     if (tinfo.IsPrivate)
         mod |= ModifierEnum.Private;
     if (tinfo.IsAssembly)
         mod |= ModifierEnum.Internal;
     if (tinfo.IsFamily)
         mod |= ModifierEnum.Protected;
     if (tinfo.IsPublic)
         mod |= ModifierEnum.Public;
     if (tinfo.IsAbstract)
         mod |= ModifierEnum.Abstract;
     if (tinfo.IsFinal)
         mod |= ModifierEnum.Sealed;
     if (tinfo.IsStatic)
         mod |= ModifierEnum.Static;
     if (tinfo.IsVirtual)
         mod |= ModifierEnum.Virtual;
         
     modifiers = mod;
     
     this.FullyQualifiedName = tinfo.Name;
     
     if (tinfo.Name == "op_Addition") this.FullyQualifiedName = "@+";
     else if (tinfo.Name == "op_Subtraction") this.FullyQualifiedName = "@-";
     else if (tinfo.Name == "op_Multiply") this.FullyQualifiedName = "@*";
     else if (tinfo.Name == "op_Division") this.FullyQualifiedName = "@/";
     else if (tinfo.Name == "op_Modulus") this.FullyQualifiedName = "@%";
     else if (tinfo.Name == "op_ExclusiveOr") this.FullyQualifiedName = "@^";
     else if (tinfo.Name == "op_BitwiseAnd") this.FullyQualifiedName = "@&";
     else if (tinfo.Name == "op_BitwiseOr") this.FullyQualifiedName = "@|";
     else if (tinfo.Name == "op_LogicalAnd") this.FullyQualifiedName = "@&&";
     else if (tinfo.Name == "op_LogicalOr") this.FullyQualifiedName = "@||";
     else if (tinfo.Name == "op_Assign") this.FullyQualifiedName = "@=";
     else if (tinfo.Name == "op_LeftShift") this.FullyQualifiedName = "@<<";
     else if (tinfo.Name == "op_RightShift") this.FullyQualifiedName = "@>>";
     else if (tinfo.Name == "op_Equality") this.FullyQualifiedName = "@==";
     else if (tinfo.Name == "op_GreaterThan") this.FullyQualifiedName = "@>";
     else if (tinfo.Name == "op_LessThan") this.FullyQualifiedName = "@<";
     else if (tinfo.Name == "op_Inequality") this.FullyQualifiedName = "@!=";
     else if (tinfo.Name == "op_GreaterThanOrEqual") this.FullyQualifiedName = "@>=";
     else if (tinfo.Name == "op_LessThanOrEqual") this.FullyQualifiedName = "@<=";
     else if (tinfo.Name == "op_MultiplicationAssignment") this.FullyQualifiedName = "@*=";
     else if (tinfo.Name == "op_SubtractionAssignment") this.FullyQualifiedName = "@-=";
     else if (tinfo.Name == "op_ExclusiveOrAssignment") this.FullyQualifiedName = "@^=";
     else if (tinfo.Name == "op_LeftShiftAssignment") this.FullyQualifiedName = "@<<=";
     else if (tinfo.Name == "op_ModulusAssignment") this.FullyQualifiedName = "@%=";
     else if (tinfo.Name == "op_AdditionAssignment") this.FullyQualifiedName = "@+=";
     else if (tinfo.Name == "op_BitwiseAndAssignment") this.FullyQualifiedName = "@&=";
     else if (tinfo.Name == "op_BitwiseOrAssignment") this.FullyQualifiedName = "@|=";
     else if (tinfo.Name == "op_Comma") this.FullyQualifiedName = "@,";
     else if (tinfo.Name == "op_DivisionAssignment") this.FullyQualifiedName = "@/=";
     else if (tinfo.Name == "op_Implicit") this.FullyQualifiedName = "@:";
     else if (tinfo.Name == "op_Explicit") this.FullyQualifiedName = "@:>";
     else if (tinfo.Name == "op_UnaryPlus") this.FullyQualifiedName = "@+";
     else if (tinfo.Name == "op_UnaryNegation") this.FullyQualifiedName = "@-";
     else if (tinfo.Name == "op_Decrement") this.FullyQualifiedName = "@--";
     else if (tinfo.Name == "op_Increment") this.FullyQualifiedName = "@++";
     else if (tinfo.Name == "op_OnesComplement") this.FullyQualifiedName = "@~";
     else if (tinfo.Name == "op_LogicalNot") this.FullyQualifiedName = "@!";        
     
     returnType = new ReturnType(tinfo.ReturnType);
     this.region = Class.GetRegion();
     this.bodyRegion = Class.GetRegion();
     this._member = null;
     
     LoadXml (declaringType, tinfo);
         
     // Add parameters
     foreach (SR.ParameterInfo pinfo in tinfo.GetParameters())
         parameters.Add(new Parameter(this, pinfo, node));
 }