コード例 #1
0
        //解析接口或类
        void parse_body(EnvDTE.CodeClass codeClass)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            var children = codeClass.Members;

            foreach (CodeElement codeElement in children)
            {
                //解析方法定义,只解析公有方法
                if (codeElement.Kind == vsCMElement.vsCMElementFunction)
                {
                    CodeFunction codeFunction = codeElement as CodeFunction;
                    if (codeFunction.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodeFunctionInfo func_info = new CodeFunctionInfo();
                        //解析返回值
                        var returnType = codeFunction.Type.AsFullName;
                        func_info.ReturnTypeName = returnType;
                        func_info.IsStatic       = codeFunction.IsShared;

                        //解析参数
                        //string parms = "";
                        string parms_for_invoke = "";

                        foreach (CodeParameter param in codeFunction.Parameters)
                        {
                            //TextPoint start = param.GetStartPoint();
                            //TextPoint finish = param.GetEndPoint();
                            //parms += start.CreateEditPoint().GetText(finish) + ",";
                            func_info.ParamList.Add(new CodeParamInfo()
                            {
                                Name = param.Name, TypeName = param.Type.AsFullName
                            });
                            parms_for_invoke += param.Name + ",";
                        }
                        //if (parms.Length > 0) parms = parms.Remove(parms.Length - 1, 1);
                        if (parms_for_invoke.Length > 0)
                        {
                            parms_for_invoke = parms_for_invoke.Remove(parms_for_invoke.Length - 1, 1);
                        }
                        //func_info.ParamString = parms;
                        func_info.InvokeParamString = parms_for_invoke;
                        func_info.ShortName         = codeFunction.Name;
                        var tmps = codeFunction.FullName.Split('.');
                        func_info.Name = tmps[tmps.Length - 1];
                        if (func_info.Name.IndexOf("<") != -1)
                        {
                            var s = func_info.Name.IndexOf("<") + 1;
                            var l = func_info.Name.Length - s - 1;
                            func_info.GenericTypeArguments = func_info.Name.Substring(s, l).Split(',').Select(it => it.Trim()).ToList();
                        }
                        if (func_info.ShortName == this.ShortClassName)
                        {
                            CodeConstructorList.Add(func_info);
                        }
                        else if (func_info.ShortName != codeClass.Name)
                        {
                            CodeFunctionList.Add(func_info);
                        }
                    }
                }
                ////解析属性定义,只解析公有属性
                else if (codeElement.Kind == vsCMElement.vsCMElementProperty)
                {
                    CodeProperty codeProperty = codeElement as CodeProperty;
                    if (codeProperty.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodePropertyInfo property_info = new CodePropertyInfo();
                        property_info.Name     = codeProperty.Name;
                        property_info.TypeName = codeProperty.Type.AsFullName;
                        try
                        {
                            var getter = codeProperty.Getter;
                            property_info.HasGet   = getter != null && getter.Access == vsCMAccess.vsCMAccessPublic;
                            property_info.IsStatic = codeProperty.Getter.IsShared;
                        }
                        catch
                        {
                            property_info.HasGet = false;
                        }
                        try
                        {
                            var setter = codeProperty.Setter;
                            property_info.HasSet   = setter != null && setter.Access == vsCMAccess.vsCMAccessPublic;
                            property_info.IsStatic = codeProperty.Setter.IsShared;
                            foreach (CodeParameter param in setter.Parameters)
                            {
                                //TextPoint start = param.GetStartPoint();
                                //TextPoint finish = param.GetEndPoint();
                                //parms += start.CreateEditPoint().GetText(finish) + ",";
                                property_info.ParamList.Add(new CodeParamInfo()
                                {
                                    Name = param.Name, TypeName = param.Type.AsFullName
                                });
                            }
                        }
                        catch
                        {
                            property_info.HasSet = false;
                        }
                        CodePropertyList.Add(property_info);
                    }
                }
                else if (codeElement.Kind == vsCMElement.vsCMElementEvent)
                {
                    CodeEvent codeEvent = codeElement as CodeEvent;
                    if (codeEvent.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodeEventInfo codeEventInfo = new CodeEventInfo();
                        codeEventInfo.Name     = codeEvent.Name;
                        codeEventInfo.TypeName = codeEvent.Type.AsFullName;
                        codeEventInfo.IsStatic = codeEvent.IsShared;
                        CodeEventList.Add(codeEventInfo);
                    }
                }
            }
            foreach (CodeClass baseClass in codeClass.Bases)
            {
                if (baseClass.Kind == vsCMElement.vsCMElementClass)
                {
                    parse_body(baseClass);
                    break;
                }
            }
        }
コード例 #2
0
        public CodeModel(string theType)
        {
            Type type = Type.GetType(theType);

            NamespaceName = type.Namespace;
            ClassName     = type.Name;
            IsAbstract    = type.IsAbstract;
            if (type.IsGenericType)
            {
                var gas = type.GetGenericArguments();
                for (var i = 0; i < gas.Length; i++)
                {
                    GenericTypeArguments.Add(gas[i].Name);
                }
                ClassName = $"{ClassName.Split('`')[0]}<{string.Join(",",GenericTypeArguments.ToArray())}>";
            }
            var cons = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            foreach (var con in cons)
            {
                CodeFunctionInfo func_info = new CodeFunctionInfo();
                var pars = con.GetParameters();
                func_info.ParamList.AddRange(pars.Select(it => new CodeParamInfo()
                {
                    Name = it.Name, TypeName = GetTypeName(it.ParameterType)
                }));
                CodeConstructorList.Add(func_info);
            }
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

            foreach (var method in methods)
            {
                if (method.IsSpecialName)
                {
                    continue;
                }
                CodeFunctionInfo func_info = new CodeFunctionInfo();
                func_info.ReturnTypeName = GetTypeName(method.ReturnType);
                func_info.Name           = method.Name;
                if (method.IsGenericMethod)
                {
                    var gas = method.GetGenericArguments().Select(it => it.Name).ToArray();
                    func_info.Name = $"{method.Name}<{string.Join(",",gas)}>";
                }
                func_info.ShortName  = method.Name;
                func_info.IsStatic   = method.IsStatic;
                func_info.IsAbstract = method.IsAbstract;
                func_info.IsVirtual  = method.IsVirtual;
                var ps = method.GetParameters();
                func_info.ParamList.AddRange(ps.Select(it => new CodeParamInfo()
                {
                    Name = it.Name, TypeName = GetTypeName(it.ParameterType)
                }));
                CodeFunctionList.Add(func_info);
            }
            var pros = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            CodePropertyList.AddRange(pros.Select(it => new CodePropertyInfo()
            {
                Name = it.Name, TypeName = GetTypeName(it.PropertyType), HasGet = it.GetGetMethod(false) != null, HasSet = it.GetSetMethod(false) != null, IsStatic = it.GetGetMethod().IsStatic, IsVirtual = it.GetGetMethod().IsVirtual, IsAbstract = it.GetGetMethod().IsAbstract
            }));
            var events = type.GetEvents(BindingFlags.Public | BindingFlags.Static);

            CodeEventList.AddRange(events.Select(it => new CodeEventInfo()
            {
                Name = it.Name, TypeName = GetTypeName(it.EventHandlerType), IsStatic = true
            }));
            events = type.GetEvents(BindingFlags.Public | BindingFlags.Instance);
            CodeEventList.AddRange(events.Select(it => new CodeEventInfo()
            {
                Name = it.Name, TypeName = GetTypeName(it.EventHandlerType), IsStatic = false
            }));
        }