public TypeStructionAnalyzer(Type parameter_Handler) : base()
        {
            PrewCallOption    = LinkCallOption.Default;
            CurrentCallOption = LinkCallOption.Default;
            if (parameter_Handler == null)
            {
                return;
            }
            TypeHandler   = parameter_Handler;
            MethodHandler = parameter_Handler;
            //判断是否为结构体

            if (TypeHandler.IsValueType && !TypeHandler.IsPrimitive && !TypeHandler.IsEnum)
            {
                IsStruct = true;
            }
            else
            {
                IsStruct = false;
            }
            ElementType = TypeHandler.GetElementType();
            if (ElementType != null)
            {
                if (ElementType.IsValueType && !ElementType.IsPrimitive && !ElementType.IsEnum)
                {
                    IsStruct = true;
                }
                else
                {
                    IsStruct = false;
                }
            }
            if (ElementType == null)
            {
                ElementType = TypeHandler;
            }
            //填充类结构缓冲
            if (ClassCache.ClassInfoDict.ContainsKey(TypeHandler.Name))
            {
                Struction = ClassCache.ClassInfoDict[TypeHandler.Name];
            }
            else
            {
                //创建类结构
                Struction             = new ClassStruction();
                Struction.Name        = TypeHandler.Name;
                Struction.TypeHandler = TypeHandler;
                Struction.IsStruct    = IsStruct;
                ClassCache.ClassInfoDict[TypeHandler.Name] = Struction;

                Type[]         types     = TypeHandler.GetInterfaces();
                HashSet <Type> hashTypes = new HashSet <Type>(types);

                #region 此处保留,因为具体操作的时候有些接口是没必要用的,可以在这里做过滤
                Type type = TypeHandler.GetInterface("IDictionary");
                if (type != null)
                {
                    for (int i = 0; i < types.Length; i += 1)
                    {
                        if (types[i].Name.Contains("ICollection") || !types[i].IsGenericType)
                        {
                            hashTypes.Remove(types[i]);
                        }
                    }
                }
                #endregion

                hashTypes.Add(TypeHandler);
                foreach (var item in hashTypes)
                {
                    Type tempType = item;

                    //获取方法结构
                    MethodInfo[] methods = tempType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < methods.Length; j += 1)
                    {
                        string methodName = methods[j].Name;
                        if (methodName.Length > 4)
                        {
                            if (methodName[0] == 103 || methodName[0] == 115)
                            {
                                if (methodName[1] == 101)
                                {
                                    if (methodName[2] == 116)
                                    {
                                        if (methodName[3] == 95)
                                        {
                                            continue;
                                        }
                                    }
                                }
                            }
                        }

                        Struction.Methods[methods[j].Name] = methods[j];
                        //处理方法标签
                        object[] attributes = methods[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[methods[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[methods[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                    }
                    Type currentType = null;
                    //获取属性结构
                    PropertyInfo[] properties = tempType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < properties.Length; j += 1)
                    {
                        currentType = properties[j].PropertyType;
                        //内部私有类的数组不做操作
                        if (currentType.IsArray)
                        {
                            if (currentType.GetElementType().GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (currentType.GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        //处理属性标签
                        object[] attributes = properties[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[properties[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[properties[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                        Struction.Properties[properties[j].Name] = properties[j];

                        //if (currentType.IsClass && ((currentType.BaseType == typeof(MulticastDelegate) || currentType.BaseType == typeof(Delegate))))
                        //{
                        //    Struction.DelegateMethods[properties[j].Name] = currentType.GetMethod("Method");
                        //}
                    }

                    //获取字段结构
                    FieldInfo[] fields = tempType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < fields.Length; j += 1)
                    {
                        currentType = fields[j].FieldType;
                        //内部私有类的数组不做操作
                        if (currentType.IsArray)
                        {
                            if (currentType.GetElementType().GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            //readonly const 内部私有类 不做操作
                            if (fields[j].IsInitOnly || (fields[j].IsLiteral && fields[j].IsStatic) || currentType.GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        //处理字段标签
                        object[] attributes = fields[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[fields[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[fields[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                        Struction.Fields[fields[j].Name] = fields[j];

                        //if (currentType.IsClass && (currentType.BaseType == typeof(MulticastDelegate) || currentType.BaseType == typeof(Delegate)))
                        //{
                        //    //PropertyInfo info = currentType.GetProperty("Method");
                        //    //info.GetValue();
                        //    //Struction.DelegateMethods[fields[j].Name] =
                        //}
                    }
                }
            }
            if (this is IDelayOperator)
            {
                ((IDelayOperator)this).Initialize();
            }
        }
Beispiel #2
0
 private LinkCallOption GetCurrentOption(string memberName)
 {
     if (Struction.Fields.ContainsKey(memberName))
     {
         FieldInfo info = Struction.Fields[memberName];
         if (info.IsPublic)
         {
             if (info.IsStatic)
             {
                 if (info.FieldType.IsValueType)
                 {
                     TempOption = LinkCallOption.Public_Value_Call;
                     return(LinkCallOption.Public_Static_Field_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Public_Ref_Call;
                     return(LinkCallOption.Public_Static_Field_Ref_Call);
                 }
             }
             else
             {
                 if (info.FieldType.IsValueType)
                 {
                     TempOption = LinkCallOption.Public_Value_Call;
                     return(LinkCallOption.Public_Field_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Public_Ref_Call;
                     return(LinkCallOption.Public_Field_Ref_Call);
                 }
             }
         }
         else
         {
             if (info.IsStatic)
             {
                 if (info.FieldType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Private_Static_Field_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Private_Ref_Call;
                     return(LinkCallOption.Private_Static_Field_Ref_Call);
                 }
             }
             else
             {
                 if (info.FieldType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Private_Field_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Private_Ref_Call;
                     return(LinkCallOption.Private_Field_Ref_Call);
                 }
             }
         }
     }
     else if (Struction.Properties.ContainsKey(memberName))
     {
         PropertyInfo pInfo = Struction.Properties[memberName];
         MethodInfo   info  = pInfo.GetGetMethod(true);
         if (info == null)
         {
             info = pInfo.GetSetMethod(true);
         }
         if (info.IsPublic)
         {
             if (info.IsStatic)
             {
                 if (pInfo.DeclaringType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Public_Static_Property_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Public_Ref_Call;
                     return(LinkCallOption.Public_Static_Property_Ref_Call);
                 }
             }
             else
             {
                 if (pInfo.DeclaringType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Public_Property_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Public_Ref_Call;
                     return(LinkCallOption.Public_Property_Ref_Call);
                 }
             }
         }
         else
         {
             if (info.IsStatic)
             {
                 if (pInfo.DeclaringType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Private_Static_Property_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Private_Ref_Call;
                     return(LinkCallOption.Private_Static_Property_Ref_Call);
                 }
             }
             else
             {
                 if (pInfo.DeclaringType.IsValueType)
                 {
                     TempOption = LinkCallOption.Private_Value_Call;
                     return(LinkCallOption.Private_Property_Value_Call);
                 }
                 else
                 {
                     TempOption = LinkCallOption.Private_Ref_Call;
                     return(LinkCallOption.Private_Property_Ref_Call);
                 }
             }
         }
     }
     throw new Exception("找不到属性或者字段");
 }