コード例 #1
0
        private static MethodInfo GetMethodInfo(Type type, string methodName, IEnumerable <object> parameters)
        {
            var parametersType = parameters.Select(p => p.GetType()).ToArray();

            var parametersTypeName = parametersType.Select(p => p.Name).Join("/");

            var key = new MemberKey(type, string.Concat(methodName, parametersTypeName));

            lock (lockInstance)
            {
                if (!typeMethodInfoCache.ContainsKey(key))
                {
                    var methodInfo =
                        type.GetMethod(
                            methodName,
                            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                            null,
                            new[] { typeof(string) },
                            null);

                    if (methodInfo == null)
                    {
                        var message = $"Cannot find the method '{methodName}' into the type '{type}'.";

                        throw new ArgumentException(message);
                    }

                    typeMethodInfoCache.Add(key, methodInfo);
                }
            }

            var method = typeMethodInfoCache[key];

            return(method);
        }
コード例 #2
0
ファイル: ReflectionTool.cs プロジェクト: figloalds/Figlotech
 public static MemberInfo[] FieldsAndPropertiesOf(Type type)
 {
     if (type == null)
     {
         throw new ArgumentNullException("Cannot get fields and properties of null type!");
     }
     lock (String.Intern($"ACCESS_MEMBER_CACHE_{type.Name}")) {
         if (!MembersCache.ContainsKey(type))
         {
             MembersCache[type] = CollectMembers(type).ToArray();
         }
         // It is not clear rather the .Net Runtime caches or not
         // the member info or if they do lookups all the time
         // It could be good to cache this, but I'm not sure yet.
         return(MembersCache[type]);
     }
 }
コード例 #3
0
        public static FieldInfo Field(this Type type, string propertyName)
        {
            var key = new MemberKey(type, propertyName);

            lock (lockInstance)
            {
                if (!typeFieldInfoCache.ContainsKey(key))
                {
                    var cachedField = GetFieldByPropertyName(type, propertyName);

                    typeFieldInfoCache.Add(key, cachedField);
                }
            }

            var field = typeFieldInfoCache[key];

            return(field);
        }