Пример #1
0
        public static Dictionary <string, MethodInfo> GetOrCollectAllStaticMethods()
        {
            if (ms_AllStaticMethods == null)
            {
                ms_AllStaticMethods = new Dictionary <string, MethodInfo>();

                List <ReflectionUtility.MethodAndAttributeData> staticMethodDatas = new List <ReflectionUtility.MethodAndAttributeData>();
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                for (int iAssembly = 0; iAssembly < assemblies.Length; iAssembly++)
                {
                    ReflectionUtility.CollectionMethodWithAttribute(staticMethodDatas
                                                                    , assemblies[iAssembly]
                                                                    , BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public
                                                                    , typeof(StaticMethodAttribute)
                                                                    , false);
                }

                StringBuilder methodSignBuilder = StringUtility.AllocStringBuilder();
                for (int iMethod = 0; iMethod < staticMethodDatas.Count; iMethod++)
                {
                    ReflectionUtility.MethodAndAttributeData iterStaticMethodData = staticMethodDatas[iMethod];
                    StaticMethodAttribute iterAttribute = iterStaticMethodData.Attribute as StaticMethodAttribute;
                    methodSignBuilder.Append(string.IsNullOrEmpty(iterAttribute.Alias)
                        ? iterStaticMethodData.Method.Name
                        : iterAttribute.Alias);

                    ParameterInfo[] parameterInfos = iterStaticMethodData.Method.GetParameters();
                    for (int iParameter = 0; iParameter < parameterInfos.Length; iParameter++)
                    {
                        ParameterInfo iterParameter     = parameterInfos[iParameter];
                        Type          iterParameterType = iterParameter.ParameterType;
                        if (iterParameterType.IsValueType)
                        {
                            if (iterParameterType == typeof(byte))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Byte);
                            }
                            else if (iterParameterType == typeof(int))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Int);
                            }
                            else if (iterParameterType == typeof(short))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Short);
                            }
                            else if (iterParameterType == typeof(long))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Long);
                            }
                            else if (iterParameterType == typeof(float))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Float);
                            }
                            else if (iterParameterType == typeof(double))
                            {
                                AppendValueType(methodSignBuilder, ValueType.Double);
                            }
                            // struct
                            else if (!iterParameterType.IsPrimitive)
                            {
                                MDebug.Assert(false
                                              , "Rpc"
                                              , $"Not support ParameterType {iterParameterType}");
                            }
                            else
                            {
                                MDebug.LogWarning("Rpc"
                                                  , $"Not support ParameterType: {iterParameterType} (IsValueType). But maybe we can support it.");
                            }
                        }
                        else if (iterParameterType.IsArray)
                        {
                            AppendValueType(methodSignBuilder, ValueType.FixedValueTypeArray);
                        }
                        else
                        {
                            MDebug.Assert(false
                                          , "Rpc"
                                          , $"Not support ParameterType {iterParameterType}");
                        }
                    }

                    string methodSign = methodSignBuilder.ToString();
                    methodSignBuilder.Clear();
#if GF_DEBUG
                    MDebug.Assert(!ms_AllStaticMethods.ContainsKey(methodSign)
                                  , "Rpc"
                                  , "!ms_AllStaticMethods.ContainsKey(methodSign)");
#endif
                    ms_AllStaticMethods[methodSign] = iterStaticMethodData.Method;
                }

                methodSignBuilder.Clear();
                foreach (KeyValuePair <string, MethodInfo> kv in ms_AllStaticMethods)
                {
                    methodSignBuilder.AppendLine(kv.Key);
                }
                MDebug.Log("Rpc"
                           , "All static methods:\n" + methodSignBuilder);
            }

            return(ms_AllStaticMethods);
        }