Пример #1
0
        public void TestIsClrMethodForProperty()
        {
            var    testInput = "get_MyProperty";
            string testOutput;
            var    testResult = NfReflect.IsClrMethodForProperty(testInput, out testOutput);

            Assert.IsTrue(testResult);
            Assert.AreEqual("MyProperty", testOutput);

            testInput  = "set_MyProperty";
            testResult = NfReflect.IsClrMethodForProperty(testInput, out testOutput);

            Assert.IsTrue(testResult);
            Assert.AreEqual("MyProperty", testOutput);

            testInput  = "add_MyProperty";
            testResult = NfReflect.IsClrMethodForProperty(testInput, out testOutput);

            Assert.IsTrue(testResult);
            Assert.AreEqual("MyProperty", testOutput);

            testInput  = "remove_MyProperty";
            testResult = NfReflect.IsClrMethodForProperty(testInput, out testOutput);

            Assert.IsTrue(testResult);
            Assert.AreEqual("MyProperty", testOutput);

            testInput  = "GetMyValues";
            testResult = NfReflect.IsClrMethodForProperty(testInput, out testOutput);

            Assert.IsFalse(testResult);
            Assert.IsNull(testOutput);
        }
Пример #2
0
        /// <summary>
        /// Locates the <see cref="CgMember"/> in <see cref="Methods"/> who matches the given name and args
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="argNames"></param>
        /// <returns></returns>
        public CgMember FindCgMember(string methodName, string[] argNames)
        {
            if (string.IsNullOrWhiteSpace(methodName))
            {
                return(null);
            }
            string isPropName;

            if (NfReflect.IsClrMethodForProperty(methodName, out isPropName))
            {
                methodName = isPropName;
            }

            var fldMatch = Fields.FirstOrDefault(f => string.Equals(f.Name, methodName));

            if (fldMatch != null)
            {
                return(fldMatch);
            }

            var propMatch = Properties.FirstOrDefault(p => string.Equals(p.Name, methodName));

            if (propMatch != null)
            {
                return(propMatch);
            }

            var matches = Methods.Where(x => string.Equals(x.Name, methodName)).ToArray();

            if (matches.Length <= 0)
            {
                return(null);
            }
            if (matches.Length == 1)
            {
                return(matches.First());
            }

            //attempt to match on arg count first
            var argCount = argNames == null ? 0 : argNames.Length;

            matches = matches.Where(x => x.Args.Count == argCount).ToArray();
            if (matches.Length <= 0)
            {
                return(null);
            }
            if (matches.Length == 1)
            {
                return(matches.First());
            }
            if (argNames == null)
            {
                return(null);
            }

            //attempt to match by args names
            var argNamesLikeThese = argNames.Select(x => Settings.LangStyle.TransformClrTypeSyntax(x));

            foreach (var match in matches)
            {
                if (match.Args.All(nfArg => argNamesLikeThese.Any(token => string.Equals(nfArg.ArgName, token))))
                {
                    return(match);
                }
            }
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Gets a method as a code gen member type
        /// </summary>
        /// <param name="mti"></param>
        /// <param name="getCallvirtMetadataTokens"></param>
        /// <returns></returns>
        public static CgMember GetAsCgMember(MethodInfo mti, bool getCallvirtMetadataTokens = false)
        {
            if (mti == null)
            {
                return(null);
            }
            if (NfReflect.IsClrMethodForProperty(mti.Name, out _))
            {
                return(null);
            }
            if (NfReflect.IsClrGeneratedType(mti.Name))
            {
                //these appear as '<SomeNameOfMethodAlreadyAdded>b__kifkj(...)'
                return(null);
            }

            var miReturnType = mti.NfReturnType();

            var cgMem = new CgMember
            {
                Name             = mti.Name,
                TypeName         = Settings.LangStyle.TransformClrTypeSyntax(miReturnType),
                IsStatic         = mti.IsStatic,
                IsGeneric        = mti.IsGenericMethod,
                IsEnumerableType = NfReflect.IsEnumerableReturnType(miReturnType),
                IsMethod         = true,
                MetadataToken    = mti.MetadataToken
            };

            if (mti.IsAssembly)
            {
                cgMem.AccessModifier = CgAccessModifier.Assembly;
            }
            if (mti.IsFamily)
            {
                cgMem.AccessModifier = CgAccessModifier.Family;
            }
            if (mti.IsFamilyAndAssembly)
            {
                cgMem.AccessModifier = CgAccessModifier.FamilyAssembly;
            }
            if (mti.IsPrivate)
            {
                cgMem.AccessModifier = CgAccessModifier.Private;
            }
            if (mti.IsPublic)
            {
                cgMem.AccessModifier = CgAccessModifier.Public;
            }

            foreach (var parameterInfo in mti.GetParameters())
            {
                var paramType = parameterInfo.NfParameterType();
                var cgArg     = new CgArg
                {
                    ArgName = parameterInfo.Name,
                    ArgType = Settings.LangStyle.TransformClrTypeSyntax(paramType)
                };

                cgMem.Args.Add(cgArg);
            }
            cgMem.MethodBodyIL = Convert.ToBase64String(Asm.GetMethodBody(mti));

            if (!getCallvirtMetadataTokens)
            {
                return(cgMem);
            }

            cgMem.opCodeCallsAndCallvirtsMetadatTokens.AddRange(Asm.GetCallsMetadataTokens(mti));
            return(cgMem);
        }